4 Replies Latest reply: Jun 13, 2008 2:21 AM by 807601

# Tridimensional array.

How do you declare and initialize a tridimensional array?

I need an array of [81][16][16].

int [][][]mat;

Inside the constructor you write:

for(t=0;t<mat.length;t++){

mat[t]=new int[81];

for(r=0;r<mat.length;r++){

mat[t][r]=new int[16];

for(c=0;c<mat.length;c++){

mat[t][r][c]=new int[16];

}

}

}
• ###### 1. Re: Tridimensional array.
Is it right to do this?

`````` int[][][] threeD = new int[5][4][3];

for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 3; ++k) {
threeD[i][j][k] = i + j + k;
}
}
}
}``````
• ###### 2. Re: Tridimensional array.
javnik400 wrote:
Is it right to do this?
Did it work?
Did it compile and run?
• ###### 3. Re: Tridimensional array.
``````int [][][] mat = new int[81][16][16];
System.out.println(mat[80][15][15]);

--output:--
0``````
• ###### 4. Re: Tridimensional array.
javnik400 wrote:
Is it right to do this?
Both ways are allright but the second is neater.