可能是我第一次使用 3D 数组。我只是尝试遵循 1D 和 2D 语法来声明 3D,但编译器说类型不匹配。谁能告诉我这背后的原因?
Integer[] _1D = new Integer[]{2,4,6,5,6};
Integer[][] _2D = new Integer[][]{{2,3},{4,6},{5,6}};
Integer[][][] _3D = new Integer[][][]{{1,2,3},{4,5,6},{7,8,9},{2,4,5}};
谢谢,
您只是再次创建一个二维数组,但有4 rows 和 3 columns。这是创建 3-d 数组的正确方法。
Integer[][][] _3D = new Integer[][][]{{{1,2,3},{4,5,6},{7,8,9},{2,4,5}},{{1,2,3},{4,5,6},{7,8,9},{2,4,5}}}; // it should be like this.
您的 3d 数组实际上是 2d 数组。添加一个支撑对以使其成为 3d。
Integer[][][] _3D = new Integer[][][]{{ {1,2,3},{4,5,6},{7,8,9} }};
{{1,2,3},{4,5,6},{7,8,9},{2,4,5}};
这只是一个二维数组。3D 数组的一个示例是:
Integer[][][] _3D = new Integer[][][]{{{1,2,3},{4,5,6}},{{7,8,9},{2,4,5}}};
3D 数组是一个包含数组的数组,它们包含数组,因此您可以在这个前提下看到,第一个数组只是一个包含数字数组的数组。
添加到@RJ 的答案,更容易看到这样的三个维度
Integer[][][] _3D =
new Integer[][][]{ // dimension 1
{ // dimension 2
{1,2,3}, // dimension 3
{4,5,6},
{7,8,9},
{2,4,5}
},
{ // dimension 2
{1,2,3}, // dimension 3
{4,5,6},
{7,8,9},
{2,4,5}
}
}; // right way
Size of array [2][4][3]
Max indices [1][3][2]