• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Kishor Joshi
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
 
Chan Ag
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

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!