• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

How to access element of Multi Dimensional int array using for loop  RSS feed

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have written following code.

I want to print all elements in Array to output here is my code




but I am getting follwing output


12
12
14
1223




how to print all elements of int array?


Thanks
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java we don't have multi-dimensional arrays. What we have is arrays of things. Things can be primitives or objects.

An array is a valid object and hence you can have arrays of arrays.

What that means is


is an array of 5 elements containing 5 ints.


is an array of 5 elements containing 5 String(s).


is an array of size 2 containing two Employee objects.


is not a two dimensional array.
It is an array of size 3 containing three arrays. These three arrays are of type int. The first array is of size 2, the second one is of size 3 and the third one is of size 4. So a[0] is the first element, i.e the first array, i.e a[0] = {1,2}.
To get the size of an array we can use the length attribute. So for the a array, we can say that a.length = 3.
We can also say that a[0].length = 2. This is also a hint for your problem statement.

And a[0][1] = 2. Of course you already know that.


 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chan

Here is few changes I made and it works

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!