Results 1 to 10 of 10
  1. #1
    KalEl is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default 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;
        	
        }
    This is what the compiler says:

    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
    I would appreciate any help :(

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
    array[i];
    gives the i th element of the array.

    Java Code:
    array.length;
    gives the length of the array
    Last edited by Eranga; 10-20-2008 at 03:16 PM.

  5. #5
    KalEl is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    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?

  6. #6
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    array[0].length

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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 06:09 PM.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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
    if you are think about parenthesis elements should arranged as follows.

    { {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.

  9. #9
    KalEl is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    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. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 07:49 AM
  2. Two diM aRRay and add rows and columns....
    By filly444 in forum New To Java
    Replies: 2
    Last Post: 08-30-2008, 06:24 PM
  3. initialize a number, which is read in from a file, into an array
    By little_polarbear in forum New To Java
    Replies: 19
    Last Post: 06-10-2008, 04:53 AM
  4. Replies: 8
    Last Post: 04-22-2008, 04:21 AM
  5. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •