Results 1 to 10 of 10
 10202008, 07:12 AM #1Member
 Join Date
 Oct 2008
 Posts
 7
 Rep Power
 0
Counting the number of columns in a 2D array,
I am trying to count the number of columns in a 2d array. I get an error with the method I wrote. Any help would be appreciated. This is what my instructor wrote on the board, and I can swear I copied it letter for letter. So what am I doing wrong?
Java Code:public static int sumLengths(int[] array) { int count = 0; // keep count of number of columns per row for (int i = 0; i < array.length; i++) { count += array[i].getLength(); // array[i].length; does not work either } return count; }
Java Code:C:\arrayStuff.java:62: int cannot be dereferenced count += array[i].getLength(); ^ C:\arrayStuff.java:62: inconvertible types found : <nulltype> required: int count += array[i].getLength(); ^ 2 errors
 10202008, 08:11 AM #2
You mention a 2dimensional array but the method you show accepts a parameter that is a simple (1dimensional) array.
array[i] in your code is an int, not int[]
db
 10202008, 01:51 PM #3
Error
Like Daryl mentioned, you are working with a one dimension array. The array[i].getLength is not the proper use of the getLength method. sTry:
Array: getLength(Object array) : Array*«*java.lang.reflect*«*Java by API
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
 10202008, 02:09 PM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 20
Java Code:array[i];
Java Code:array.length;
Last edited by Eranga; 10202008 at 02:16 PM.
 10202008, 04:43 PM #5Member
 Join Date
 Oct 2008
 Posts
 7
 Rep Power
 0
Thanks a bunch for your help guys, but I'm still lost. How exactly then do I count the number of columns? Array.length will only return the number of rows, correct?
 10202008, 05:02 PM #6Member
 Join Date
 Oct 2008
 Posts
 58
 Rep Power
 0
array[0].length
 10202008, 05:05 PM #7
Java arrays are wierd. For a 2 dim array, you can access the first "row" as a one dim array.
array.length returns the length of the array you are looking at.
How would you define rows and columns for an array?
array[rows][columns]
then array[0] would be the first row which is an array of columns length
Also the number of columns in each row can be different.
int anArray[][] = new int[3][];
anArray[0] = new int[2];
// leaves [1] empty
anArray[2] = new int[5];
Write a small test program with the above. Assign values to elements and use a loop to print out the results.Last edited by Norm; 10202008 at 05:09 PM.
 10202008, 07:01 PM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 20
In sense 2D array is an arrays of array, that means each array is an element of an array. See the following.
Java Code:int arr[][]; // Declaration arr = new int[2][3]; // Instantiation
{ {1, 2, 3 }, {23, 56, 0 } }
have two arrays
{1, 2, 3} and {23, 56, 0 }
and each array has three elements.
Just an additional information.
 10212008, 01:17 AM #9Member
 Join Date
 Oct 2008
 Posts
 7
 Rep Power
 0
Thanks a bunch for all the help guys! I understand now what I did wrong. The parameter variable in the sumLengths method should say int[][] array opposed to int[] array. I got it working!
Thanks again!
 10212008, 05:27 AM #10
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 20
Yes, you have to pick the right parameter. With length and elements in correct type.
Similar Threads

Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11032010, 06:49 AM 
Two diM aRRay and add rows and columns....
By filly444 in forum New To JavaReplies: 2Last Post: 08302008, 05:24 PM 
initialize a number, which is read in from a file, into an array
By little_polarbear in forum New To JavaReplies: 19Last Post: 06102008, 03:53 AM 
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04222008, 03:21 AM 
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11292007, 05:51 PM
Bookmarks