This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multi Dimension Array Storing

 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a doubt with multi dimension arrays,
I know if we create an array like this
String name[][]= new String [3][4];
It will store array in 3 Rows and 4 Columns.
I want to know how we will store more than two dimension array in java
eg for
String Game[][][]= new String [3][4][5];
How this will be stored 3Rows, 4 Columns then 5 ?
thanks in advance
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your two dimensional array example, referring to the first dimension as the row, and the second as the column is merely a human way to conceptualize such a datastructure - these aren't Java terms or notions.
So, you can conceptualize a three dimensional array any way you'd like. Some people might envision a 3D cube like structure. Some people might prefer to think in terms of multiple images of 2D charts - one new chart image for each component in the third dimension.
Personally, I usually try not to envision multi-dimensional array data, and just work with it as a more abstract structure with various indexes to access the correct components.
 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for ur quick reply
Here I have attached a small code which i was trying to do
Please check the marked line in code as
//Line Marked Pls Check for this**********
and suggest me how to access third dimensions Elements Dynamically
as here i in for Loop i m providing k<5 (Hard Coded) ....
public class Array2D{
public static void main(String args[]){
String name[][][]= new String [3][4][5];
String val = "Anu";

for (int i = 0; i<name.length; i++){
for (int j = 0; j<name[i].length; j++) {
for (int k = 0; k<5; k++) {//Line Marked Pls Check for this**********
name[i][j][k]=val;
val=val+1;
System.out.println ("Value is--->"+name[i][j][k]);
}
}
}
}
}
thanks
 
Gillian Bladen-Clark
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multi-dimensional arrays remind me of the old riddle:
As I was going to St. Ives,
I met a man with seven wives.
Each wife had seven sacks,
Each sack had seven cats,
Each cat had seven kits.
Kits, cats, sacks, and wives,
How many were going to St. Ives?
These names of the kittens could be held in an array :
String[7][7][7][7] kittens;
The first dimension is the 7 wives. Each wife has 7 sacks (2nd dimension), each sack has 7 cats (3rd dimension), each cat has 7 kittens (4th dimension). e.g. kittens[1][2][3][4] holds the name of his second wife's third sack's fourth cat's fifth kitten.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (int k = 0; k<5; k++) {//Line Marked Pls Check for this**********

The thing to remember is that multi-dimensional arrays are simply arrays of arrays. In your case above using a 3D array, name[i] is a 2D array and name[i][j] is a simple array and name[i][j][k] is a 0-dimensional array or just an element of the array.
 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Gillian(for your Riddle) and Michael for ur suggestions.
my doubts are clear.
regards
Anurag
 
A lanipekun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
all i know is that a multidimensional array is an array of array i.e.
a bag of arrays holding other bags of array.
aurevoir
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic