• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

2D Array

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ? :/



 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 9710
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic