my dog learned polymorphism
The moose likes Beginning Java and the fly likes Multi Dimension Array Storing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multi Dimension Array Storing " Watch "Multi Dimension Array Storing " New topic

Multi Dimension Array Storing

Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
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

Joined: Dec 10, 2001
Posts: 7023
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.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
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**********
System.out.println ("Value is--->"+name[i][j][k]);
Gillian Bladen-Clark

Joined: Dec 13, 2003
Posts: 18
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

Joined: Jan 30, 2002
Posts: 3451
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.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
Thanks a lot Gillian(for your Riddle) and Michael for ur suggestions.
my doubts are clear.
A lanipekun

Joined: Jan 05, 2004
Posts: 3
all i know is that a multidimensional array is an array of array i.e.
a bag of arrays holding other bags of array.
I agree. Here's the link:
subject: Multi Dimension Array Storing
It's not a secret anymore!