Results 1 to 9 of 9
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default Question about a 2d array example

    Hi, I have a question about this little code example. Its always these little nested for loop tricks that mess me up! LOL. Hopefully I will get used to them. But below, I have the following questions about it. The code outputs a table of 0-9 in the top row, then the next row 10-19, etc, there are 5 rows total. My questions are commented on the code lines. Any help greatly appreciated. Thank you!

    Java Code:
    //********************************************************************
    //  TwoDArray.java       Author: Lewis/Loftus
    //
    //  Demonstrates the use of a two-dimensional array.
    //********************************************************************
    
    public class TwoDArray
    {
       //-----------------------------------------------------------------
       //  Creates a 2D array of integers, fills it with increasing
       //  integer values, then prints them out.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          int[][] table = new int[5][10];
    
          // Load the table with values
          for (int row=0; row < table.length; row++) // table array is 2D, how do get a length for it?
             for (int col=0; col < table[row].length; col++)//I don't understand table[row].length.
                table[row][col] = row * 10 + col; //I don't understand this.
    
          // Print the table
          for (int row=0; row < table.length; row++) 
          {
             for (int col=0; col < table[row].length; col++)
                System.out.print (table[row][col] + "\t");
             System.out.println();
          }
       }
    }

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Look at it as an array of arrays. So table[0] is itself an array, and any array methods can be used on it. That should hopefully make it easier to understand.

  3. #3
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Thank you Toll. Does table.length refer to 5 elements always in this example please?

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Correct. table is an array with length 5; the only special thing about it is that the elements in the array are arrays as well.

  5. #5
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Great. Just one more question I think. I don't understand this code part.


    Java Code:
    table[row][col] = row * 10 + col;
    It looks like it multiplies row times 10, so it would be counting by 10's or something, but the code outputs in regular sequence. 1 2 3 4 ....

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Step through the loop one step at a time.
    Java Code:
          for (int row=0; row < table.length; row++)
             for (int col=0; col < table[row].length; col++)
                table[row][col] = row * 10 + col;
    The first time it sets the value, what are the values of row and col? What is the resulting value?

  7. #7
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    I took a stab at it. ok. the value of row and table.length on the first iteration of the loop is while 0(row)is less than 5(table.length), then the inner for loop says while 0< ??(table[row].length,
    then make table [0](row) and [0](col) = 0 * 10 + 0. So that is 0 from the right calculation, put into table [0][0]. So the first loop prints out zero. On the second iteration, the value of row is 1. The value of col is 1. so the next iteration should be table[1][1]= 1 * 10 + 1, which is the value 11. So at position 1, 1, print the value of 11. That doesn't make sense to me because I thought the second value printed is 2. Please any more advice greatly appreciated. thank you!

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Not quite. The first time it assigns a value, row is 0 and col is 0. That far you've got it right. However, the inner loop runs inside the outer loop; thus, it loops from 0 to 9 (the length of the "inner" array) for every row. So the second iteration has row still as 0 and col as 1. After enough loops, row is 0 and col is 9, and the inner loop ends. The outer loop takes another step, and the inner loop starts over, meaning the values are now row=1 and col=0. Repeat until the outer loop is done.

  9. #9
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Thanks again Toll. Does that mean that the value of table[row].length means "loop this while col is less than 10?
    EDIT: hahahahah! I GOT IT!!! thank you so much Toll. That little sob of a code (the little nested iterating for loops) almost ruined my day as they are the hardest to understand to me, go figure. THANK YOU!!! you made my afternoon! Derek
    Last edited by silverglade; 06-07-2011 at 01:04 AM.

Similar Threads

  1. Question about array
    By hei1233212000 in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 04:55 PM
  2. Array question
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 06-11-2010, 02:41 AM
  3. Array Question
    By sc001 in forum New To Java
    Replies: 1
    Last Post: 02-14-2010, 05:57 AM
  4. array question
    By dazednconfused in forum New To Java
    Replies: 4
    Last Post: 09-15-2009, 06:44 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 03:18 AM

Posting Permissions

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