Results 1 to 9 of 9
- 06-06-2011, 07:35 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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(); } } }
- 06-06-2011, 07:36 PM #2
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.
- 06-06-2011, 07:43 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
Thank you Toll. Does table.length refer to 5 elements always in this example please?
- 06-06-2011, 07:48 PM #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.
- 06-06-2011, 08:10 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
Great. Just one more question I think. I don't understand this code part.
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 ....Java Code:table[row][col] = row * 10 + col;
- 06-06-2011, 08:14 PM #6
Step through the loop one step at a time.
The first time it sets the value, what are the values of row and col? What is the resulting value?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;
- 06-06-2011, 11:41 PM #7
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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!
- 06-06-2011, 11:45 PM #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.
- 06-06-2011, 11:56 PM #9
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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! DerekLast edited by silverglade; 06-07-2011 at 12:04 AM.
Similar Threads
-
Question about array
By hei1233212000 in forum New To JavaReplies: 2Last Post: 09-18-2010, 03:55 PM -
Array question
By TaxpayersMoney in forum New To JavaReplies: 5Last Post: 06-11-2010, 01:41 AM -
Array Question
By sc001 in forum New To JavaReplies: 1Last Post: 02-14-2010, 04:57 AM -
array question
By dazednconfused in forum New To JavaReplies: 4Last Post: 09-15-2009, 05:44 AM -
Array question
By McChill in forum New To JavaReplies: 5Last Post: 02-20-2009, 02:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks