• Post Reply Bookmark Topic Watch Topic
  • New Topic

looping through arrays.  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to understand what exactly is going on on the loops , the part im not understanding is the inner loop. Allow me to explain what i think its happening here.


i have an array consisting of a table with 0 1 2 3 columns and 0 1 2 rows.
the first bracket references the columns and the second bracket the rows.
the outter for loop looks through the lenght of the array  i think the length means the columns 0 1 2 3, so  the loop can be thought as

the second loop confuses me when the [i].length is introduced. I know that 'i' would become 0 1 2 3 as it increases and so the second for loop
would be

the system.out. is printing the actual positions of the array, and that is why we print the whole table, because the for loops are iterating through the columns and the rows.
so the display would take on the values of [0][0] [1][1] [2][2] etc until the loop conditions are satisfied.
I dont understand what  j<numbers[i].length is doing. if i remove the [i] the print out misses the last column.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
numbers is an array of arrays.  This is an important concept because Java doesn't have true "tables" or "matrices" or "2D arrays".  numbers has exactly three elements, all three are arrays.  You can think of them as rows, but they are in fact arrays.  So the code:

...is looping through these three elements, 0 through 2.  For each element you have an array: numbers[0], numbers[1] and numbers[2].  Since each element is an array, you need to check its length, because there's no guarantee that each array will be the same length.  This is called a ragged array. So:

starts j at 0 and goes to the length of the array numbers[i].  Make sense?
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so there is not really a table there is 3 arrays. the first loop is going through the length of the array 0 1 2 if i think of it like a table, it goes through the number of total rows. so now the second loop is saying go through the length of array[0] array[1] array[2]. I understand what is going on now. but this brings me to a new question, i think i have the answer in my head. I was going to ask why do i need two for loops to go through the entire array, why cant i say for(int i = 0; i<array.length ; i++)
sout(array[i]) , my own response its that i have an array of arrays and not just a single array. is this correct?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:. . . why cant i say for(int i = 0; i<array.length ; i++) . . .
You can. That is the problem. You can callwrite array[0] and get an array. Then you use array[1] and get a second array. Then you use array[2] and get a third array. Then you run out of array indices and your loop terminates.
If you try that, you get something basic like [I@abcd1234 which means that arrays don't override the toString() method. If you want the individual elements of the inner arrays, you will need to iterate those. The mmemory locations of the individual arrays inside the outer arrays are not necessarily adjacent, so you cannot simply iterate the nine numbers. You can cheat by using a method of the Arrays class:-There are at least two alternatives:-
  • 1: Use a for-each loop. Since you are not assigning anything you don't need a classical for loop, and you want to go through the whole length of every array. Look in the Java Tutorials.
  • 2: Use another Arrays class method. System.out.println(Arrays.deepToString(array));
  • Don't write System.out.println(""); Write System.out.println();
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!