Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Enhanced for Loop(for Arrays)

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote this piece of code to print the values of a two dimensional array (after i reffered from K&B book page no.339)
class test{
public static void main(String[] args){
int [][] a = {{1,2,3},{4,5,6},{7,8,9}};
for(int[] n : a)
System.out.println(n);

}
}
But the result i get are not the elements of the array,
it looks like [I@187c6c7
Can anyone explain why am i getting these kind of errors?
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have a bidimensional Array, or either, an Array inside of another Array, entao it prints the value of the object and nao the elements.
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in the book it says
int [][] twoDee ={{1,2,3},{3,4,5},{5,6,7}};
for(int[] n :twoDee);//loop thru the array of arrays

now how do i print the elements of the array twoDee?
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you to want yes.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If you want to print out the entries of -

int [][] twoDee ={{1,2,3},{3,4,5},{5,6,7}};

you would have to do this;



John
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh ok i got it..
thanks a lot, so if i have to print the full array then i can say
System.out.println(n[0]+" "+n[1]+" "+n[2]);
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by asha ganapathy:
...the result i get are not the elements of the array, it looks like [I@187c6c7...


Actually, you are getting the elements of the array.

Remember, in Java, multi-dimensional arrays are really just arrays that contain other arrays. When you say for(int[] n : a), this means for each int array (referenced by n) that is an element of the array a. So when you print n, you get a String representation of the array itself (which is an object) -- not the contents of that array.

(In this case, the String representation uses the left bracket "[" to indicate an array, "I" to indicate the type of array as int, followed by a memory address.)

If you want to print the contents of this array (n), you will need to iterate through the elements of n. And since this is a multi-dimensional array (arrays within arrays), that's where nested loops come in. In psuedo code (lacking the iteration details)...
 
reply
    Bookmark Topic Watch Topic
  • New Topic