This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!