File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use Enhanced for loop with 2D array

 
Davie Lin
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need two enhanced for loops for your iteration. Try something like this:

 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why wait for someone else ?
Why dont you try it yourself ?
There is no other better way to learn it,I think.
 
Davie Lin
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the ideas provided.
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pascal / Delphi supports full multi-dimensional arrays, but also supports arrays of arrays.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob.
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic