Results 1 to 10 of 10
- 10-20-2008, 07:12 AM #1
Member
- 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?
This is what the compiler says: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; }
I would appreciate any help :(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
- 10-20-2008, 08:11 AM #2
You mention a 2-dimensional array but the method you show accepts a parameter that is a simple (1-dimensional) array.
array[i] in your code is an int, not int[]
db
- 10-20-2008, 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.
- 10-20-2008, 02:09 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
gives the i th element of the array.Java Code:array[i];
gives the length of the arrayJava Code:array.length;
Last edited by Eranga; 10-20-2008 at 02:16 PM.
- 10-20-2008, 04:43 PM #5
Member
- 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?
- 10-20-2008, 05:02 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 58
- Rep Power
- 0
array[0].length
- 10-20-2008, 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; 10-20-2008 at 05:09 PM.
- 10-20-2008, 07:01 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In sense 2D array is an arrays of array, that means each array is an element of an array. See the following.
if you are think about parenthesis elements should arranged as follows.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.
- 10-21-2008, 01:17 AM #9
Member
- 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!
- 10-21-2008, 05:27 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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: 11-03-2010, 06:49 AM -
Two diM aRRay and add rows and columns....
By filly444 in forum New To JavaReplies: 2Last Post: 08-30-2008, 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: 06-10-2008, 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: 04-22-2008, 03:21 AM -
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11-29-2007, 05:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks