我需要产生一个4行4列的矩阵。问题是某些单元格需要保存多个值。我不知道该怎么做。我应该制作另一个数组,array [] value = {1,13};
然后将其索引放入我的2d数组中吗?
Array [][] matrix = new Array [4][4];
matrix [][] = {{ a, b, c, d},
{{1,13}, 10, 6, 8},
{{2,14}, 11, 7, 9},
{{5,13,15}, {12,10}, 4, 4} };
您可以改用3D阵列。
我不确定是否要在数组中存储对象或基元,但让我们假设基元(它实际上并没有什么不同):
int[][][] matrix = new int [4][4][];
matrix[1][1] = new int [] {10};
matrix[2][0] = new int [] {2,14};
matrix[3][0] = new int [] {5,13,15};
或者您可以直接将其初始化:
int [][][] mat =
{{{0}, {0}, {0}, {0}},
{{1,13}, {10}, {6}, {8}},
{{2,14}, {11}, {7}, {9}},
{{5,13,15}, {12,10}, {4}, {4}}};
System.out.println (Arrays.deepToString (mat));
请注意,我将单个值更改为长度为1的数组,并将第一行的字符更改为{0},因为这是一个int
数组。
输出(为了便于阅读,我添加了一些换行符和空格):
[[[0], [0], [0], [0]],
[[1, 13], [10], [6], [8]],
[[2, 14], [11], [7], [9]],
[[5, 13, 15], [12, 10], [4], [4]]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句