Thread: Question about a 2d array example

1. Senior Member Join Date
Feb 2009
Posts
182
Rep Power
11 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;

// 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();
}
}
}  Reply With Quote

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

3. Senior Member Join Date
Feb 2009
Posts
182
Rep Power
11 Thank you Toll. Does table.length refer to 5 elements always in this example please?  Reply With Quote

4. 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.  Reply With Quote

5. Senior Member Join Date
Feb 2009
Posts
182
Rep Power
11 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 ....  Reply With Quote

6. 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?  Reply With Quote

7. Senior Member Join Date
Feb 2009
Posts
182
Rep Power
11 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 (row) and (col) = 0 * 10 + 0. So that is 0 from the right calculation, put into table . 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 * 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!  Reply With Quote

8. 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.  Reply With Quote

9. Senior Member Join Date
Feb 2009
Posts
182
Rep Power
11 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 12:04 AM.  Reply With Quote Posting Permissions

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