1.定义三维数组的方法
声明格式并分配空间:数据类型 数组名[ ][ ][ ] =new 数据类型[ ][ ][ ] 例如:int a[][][]=new int [][][]
初始化
1)使用for循环进行初始化
例如:
for(int i=0;i<3;i++)
for (int j=0;j<4;j++)
for(int k=0;k<5;k++)
a[i][j][k]=i*j*k;
2)直接初始化
例如、
int a[][][]=new int[][][]{
{ { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } }, { { 13, 14, 15 }, { 16, 17, 18 } }
}
2.利用foreach循环遍历数组
foreach循环的语法
for(元素类型type 元素变量value : 遍历对象obj){
//遍历体
}
利用foreach遍历三维数组的方法
for(int b[][]:a)
for(int c[]: b)
for (int d:c)
System.out.print(" "+d);
需要注意的是需要使用三层for循环