| Author |
How to use Enhanced for loop with 2D array
|
Davie Lin
Ranch Hand
Joined: Aug 05, 2007
Posts: 294
|
|
Hi everyone, Has anyone try to use the Enhanced for loop with a 2D array? How do you set it up? Much thanks for anyone providing hints
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
You need two enhanced for loops for your iteration. Try something like this:
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2547
|
|
Why wait for someone else ? Why dont you try it yourself ? There is no other better way to learn it,I think.
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
Davie Lin
Ranch Hand
Joined: Aug 05, 2007
Posts: 294
|
|
|
Thanks for the ideas provided.
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2547
|
|
Looks like Garrett beat me on this by a few seconds I had a similar piece of code on my mind as posted by Garrett. But its the ranch policy to help people learn on their own so I refrained from posting it.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
There's no such things as a 2D array; in Java all arrays are one-dimensional. When you declare int[][] you are actually telling the JVM you want an array and its members are each arrays. Look for the short[][] example in Java Language Specification �10.2.
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2547
|
|
|
You are right Campbell, there is no such thing as 2D array. But the term is widely used to mean an array whose individual elements are arrays.I think the reason behind this is the use of such arrays in matrices and 2D transforms.
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
Originally posted by Amit Ghorpade: Looks like Garrett beat me on this by a few seconds I had a similar piece of code on my mind as posted by Garrett. But its the ranch policy to help people learn on their own so I refrained from posting it.
You're right of course. It's always a judgment call as to how much to actually hand to a poster, and how much to let them figure out on their own. In this case I made the judgment that the code was trivial enough that giving away the answer wasn't too harmful.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
Originally posted by Amit Ghorpade: You are right Campbell, there is no such thing as 2D array. But the term is widely used to mean an array whose individual elements are arrays.I think the reason behind this is the use of such arrays in matrices and 2D transforms.
Aren't there languages which really use 2D arrays? Then it might be that people forget Java is different in this respect. Note the Sun publications never say an array can be 2D.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Pascal / Delphi supports full multi-dimensional arrays, but also supports arrays of arrays.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
Thank you, Rob.
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2547
|
|
Originally posted by Garrett Rowe: You're right of course. It's always a judgment call as to how much to actually hand to a poster, and how much to let them figure out on their own. In this case I made the judgment that the code was trivial enough that giving away the answer wasn't too harmful.
Hmm thats true
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
As Garrett says, it is always a nice decision about how much to show a poster, but I think I would have done the same.
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2547
|
|
Even I would have done the same but after another post. Anyways I think Garrett's view is justified.
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: How to use Enhanced for loop with 2D array
|
|
|