Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two Dimensional Array Question

 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Putting this out there for anyone brave enough to try to nearly draw me a picture, cause I'm just not getting part of this:



Some of the comments are my own..please correct them as needed. I'm not getting the action of the two sets of nested for loops. The way I'm reasoning this is that the "boxes" (from a visual representation) is being counted and thus outputs 0-9 and in an uneven dimension. I understand the lines that initialize the second dimension. I just don't understand *precisely* what the for loops are doing other than incrementing from 0-9. Can someone comment the for loops for me to clarify? Thank you.>
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The loops just iterate through the entire array and assign a value to each array element which is incremented each time.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I just need to see this visually or something, because I'm not understanding why it's using two sets of nested for loops. Could this example possibly use only 1 nested for loop to get the same result? Everything web site, or video I've watched on 2d arrays use only 1 set of for loops.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12081
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think of it this way...

a 1 dimensional array is an egg carton. Eggs are what I care about, but an egg carton holds them, and I can make an egg carton of any size.

a 2 dimensional array is a crate of egg cartons. the crate can hold any number of egg cartons, as long as I make it big enough. Each egg carton can then be a different size. One egg carton in my crate may hold 6 eggs, and the next may hold 12. So, the first loop has me look at each egg carton in the crate. The inner loop looks at each egg in the carton I'm currently looking at. Each time I move to a new egg carton, I need to start over and look at each egg. That's why I need the loop inside the loop.

The analogy can continue to higher dimensions:

A palate of crates of cartons of eggs

A truck full of palates full off crates of cartons of eggs

a fleet of trucks full of trucks full of palates full of crates full of cartons full of eggs...

etc.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like that analogy! It had crossed my mind that the reason for the second set of loops was because of the fact that it's two dim. Thanks for giving me the visual..sometimes it just takes a picture
 
Eder Suarez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you have "j" columns and "i" rows. The first iteration tells you the row you are in. The second tells you the column:

The data is filled this way (let's say 2 rows and 3 columns)

1

12

123

123
4

123
45

123
456

You start in "i=0" (first row) and it has to wait until "j"(columns) reaches its limit (3 in this case) so "i" can move to the next row.

So i would put it this way



Hope it helps.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for breaking it down. I think I've finally got it.

Now, every time I see an array I'm going to see eggs.

All of the comments were very helpful and I'm sure I'll be posting other questions as they come along.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic