m1 方法以 3 维数组作为参数,同时打印 b (对象引用)给出 [[[I@15db9742
,这意味着 a 是 3 维数组,但在 for 循环中(粗体) a 表现为二维数组,如果我将其写为 int x[][][]:a 那么它就是
giving error " Type mismatch: cannot convert from element type int[][] to
int[][][]"
此行为的原因是什么?
public class Arr {
public static void main(String[] args) {
int[][][] aa2=new int[2][1][];
m1(aa2);
}
public static void m1(int[][][] b)
{
**for(int[][] x:b)**
System.out.println(b);
}
}
最佳答案
你的for循环不正确。您正在做的是对数组 b
进行迭代,该数组是 int[][][]
(您将其称为 3D 数组)以获取下一个较低层 int[][]
.
但是你使用了System.out.println(b);
,它没有打印出你的int[][]
,而是打印出int[][ ][]
已传递给您的方法,基本上从未使用过 for 循环。
这可以完成工作:
public static void m1(int[][][] b)
{
for(int[][] x : b)
System.out.println(x);
}
结果:
[[I@15db9742
[[I@6d06d69c
关于for 循环中的 Java 三维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45398336/