String[][][] arr =
{{ { "a", "b" , "c"}, { "d", "e", null } },{ {"x"}, null },{{"y"}},{ { "z","p"}, {} }};
我有理由相信这样的事情最终会出现在考试中,但是在编码的10年里,我从未见过如此恐怖。我肯定可以使用一些技巧来解析这个问题,这样我就可以弄清楚它在我脑海中的真正含义。当然,我可以通过IntelliJ运行它,但我不会有它。
为了记录,这对我来说非常有意义:
int[][] twoD = { { 1, 2, 3} , { 4, 5, 6, 7}, { 8, 9, 10 } };
答案 0 :(得分:4)
我建议添加一些缩进,这将帮助您跟踪此嵌套数组的级别:
String[][][] arr = {
{
{ "a", "b", "c" },
{ "d", "e", null }
},
{
{ "x" },
null
},
{
{ "y" }
},
{
{ "z", "p" },
{}
}
};
这有帮助吗?我发现这样可以更容易地将数据结构可视化并回答诸如“arr[1][0][0]
的价值是什么?”之类的问题。
您可以将类型String[][][]
读作“字符串数组数组”,或者可能是“三维字符串数组”。
答案 1 :(得分:2)
也许你会从中了解更多,我可以看到你的阵列:
String[] arr0 = {"a", "b", "c"};
String[] arr1 = {"d", "e", null};
String[] arr2 = {"x"};
String[] arr3 = null;
String[] arr4 = {"y"};
String[] arr5 = {"z", "p"};
String[] arr6 = {};
String[][] arr0_1 = {arr0, arr1};//-->> {{"a", "b", "c"}, {"d", "e", null}}
String[][] arr2_3 = {arr2, arr3};//-->> {{"x"}, null}
String[][] arr4_4 = {arr4}; //-->> {{"y"}}
String[][] arr5_6 = {arr5, arr6};//-->> {{"z", "p"}, {}}
String[][][] arr = {arr0_1, arr2_3, arr4_4, arr5_6};
相当于
String[][][] arr
= {
{//----------------------------
{"a", "b", "c"},//arr0 |
//|---->arr0_1
{"d", "e", null}//arr1 |
},//---------------------------
{//----------------------------
{"x"},//arr2 |
//|---->arr2_3
null //arr3 |
},//---------------------------
{//----------------------------
{"y"}//arr4 |---->arr4_4
},//---------------------------
{//----------------------------
{"z", "p"},//arr5 |
//|---->arr5_6
{}//arr6 |
}//---------------------------
};
答案 2 :(得分:0)
格式化数组将有助于找出行和列的矩阵
String[][][] arr =
{
{{"a", "b" , "c"}, {"d", "e", null} },
{{"x" }, null },
{{"y" } },
{{"z", "p" }, { }}
};
2个Cols,4行
<0>元素0,0:{"a", "b" , "c"}
元素在0,1:
{"d", "e", null}
元素在1,0:
{"x"}
元素1,1:
null
等等
答案 3 :(得分:0)
MutationObserver
答案 4 :(得分:0)
以@smarx为基础回答:
// [][][] is an array of 2D arrays
String[][][] arr = { // begin declaration of arr
// first 2D array
{
{"[0][0][0]", "[0][0][1]", "[0][0][2]"},
{"[0][1][0]", "[0][1][1]", null, "[0][1][3]"}
},
// second 2D array
{
{"[1][0][0]", "[1][0][1]"},
null // we should check for this when reading
},
// third 2D array
{
{"[2][0][0]", "[2][0][1]", "[2][0][2]"}
},
// fourth 2D array
{
{"[3][0][0]", "[3][0][1]"},
{}
},
// fifth 2D array
{
{"[4][0][0]", "[4][0][1]", "[4][0][2]"},
{"[4][1][0]", "[4][1][1]"},
{"[4][2][0]", "[4][2][1]", "[4][2][2]"}
}
};
// Reading the 3D array, as: [x][y][z]
for (int x = 0; x < arr.length; x++) {
for (int y = 0; y < arr[x].length; y++) {
// one of our 2D arrays has a null array,
// declared in the line: {"[0][1][0]", "[0][1][1]", null, "[0][1][3]"}
if (arr[x][y] != null) {
for (int z = 0; z < arr[x][y].length; z++) {
System.out.println(arr[x][y][z]);
}
} else {
System.out.println("null 2D array");
}
}
}
我们必须检查空数组,因为我们正在尝试访问某些内容并且它不存在。不需要检查空字符串(用于打印),因为Java在打印时已经进行了此检查,请参阅why null reference prints as "null"?