• Post Reply Bookmark Topic Watch Topic
  • New Topic

2D Array  RSS feed

 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First print is [0][0] which is 3 and then depending on how you count you should have [1][0] for 2 or [0][1] for 5 etc. I assume grid.length is 9 since there are 9 numbers. But what is grid[row].length? I think next print should be [1][0] because after the first loop, row gets increased by 1 and col stays the same since 0 is not larger than 0. So third number is 1 = [2][0] etc.. Problem is if array.length is 9 row will increase later to 4,5,6 etc. And there are only 3 rows in the grid..
What am I doing wrong or what am I missing ? :/



 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Technically, Java doesn't have a concept of a 2D array. It is simply an array of int arrays. The length of the grid array is 3, as it hold three int arrays. And as for the int array elements, they all have different sizes. The first hold 3 int elements. The second hold 2 int elements. And the third int array hold 4 int elements.

Henry
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so the array length is 3.

I now get it that it prints in the order 3, 5, 2343 etc. So first number [0][0] second [0][1] third [0][2] etc. Well what's confusing me now is that we have 3 numbers that requires row [0] but in the for loop, row will increase after the first iteration from row 0 to row 1.

(int row = 0; row < 3; row++) // 3 = grid.length

So how is it possible for row to stay as 0 for 3 numbers. I dont see how, since it will increase from 0<3 to 1<3 then 2<3.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:Ok so the array length is 3.

I now get it that it prints in the order 3, 5, 2343 etc. So first number [0][0] second [0][1] third [0][2] etc. Well what's confusing me now is that we have 3 numbers that requires row [0] but in the for loop, row will increase after the first iteration from row 0 to row 1.

(int row = 0; row < 3; row++) // 3 = grid.length

So how is it possible for row to stay as 0 for 3 numbers. I dont see how, since it will increase from 0<3 to 1<3 then 2<3.

Row will stay 0 while column is 0, 1, 2. (for 3 column numbers)
Row will stay 1 while column is 0, 1.
Row will stay 2 while column is 0, 1, 2, 3.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:So how is it possible for row to stay as 0 for 3 numbers. I dont see how, since it will increase from 0<3 to 1<3 then 2<3.

Because that's how nested loops work. The control variable for the outer loop (col) is not changed until ALL values for the inner loop (row) have been processed.

And if you think about it, you wouldn't really want it to work any other way, would you?

Perhaps if you tell us how you expected it to work, we might be able to explain where your mistake is.

Winston
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Because that's how nested loops work. The control variable for the outer loop (col) is not changed until ALL values for the inner loop (row) have been processed.


Isn't row the outer loop and column the inner one? Since Row is the first loop and column is indented under it, to me that looks like column should be the inner one. And since the numbers have to print from left to right e.g. 3, 5, 2343 that means that row has to stay unchanged and column increases 0,1,2 in order to get the three first numbers.

But later on I'm totally lost..So lets say outer loop(row) stays unchanged as 0 until inner loop(column) is completely processed.
So we have
0 0
0 1
0 2
Now when the column loop has been processed its time for the row loop. Problem here is will it increment immediately or will it start as 0? And one more thing.. What happens with the column loop when its reached 2<3. It cant display 2 for every other number and it also cant reset because there doesn't say anywhere that it will reset after the condition col<grid[row].length is met.

 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I get it.. But the last number is [2][3.]. That doesnt compute in my head since column cant be 3 when

for(col=0; col < 3; col++) The 3 is the grid length. So column can max be 2.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:Isn't row the outer loop and column the inner one?

Oops. You spotted the deliberate mistake (not). Well done.

Now when the column loop has been processed its time for the row loop. Problem here is will it increment immediately or will it start as 0?

And one more thing.. What happens with the column loop when its reached 2<3. It cant display 2 for every other number and it also cant reset because there doesn't say anywhere that it will reset after the condition col<grid[row].length is met.

Have a look at that inner loop again carefully. What does it say? You might want to read it out aloud to yourself.

Alternatively, have a look at the tutorial page. That should explain it.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!