【发布时间】:2017-10-22 20:32:54
【问题描述】:
我有一个由两个独立数组组成的多维数组。
// slot1 = new int[][] { {Array1}, {Array2}}
slot1 = new int[][] { {1, 2 ,3}, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
我正在尝试做两件不同的事情。
- 我正在尝试打印数组 slot1 的内容,但每个数组之间有一个空格。例如,我希望我的输出是这样的:
1 2 3 ----- 0 0 0 0 0 0 0 0 0 0 0 0 0 0
注意:在我有破折号的地方,我实际上会添加一个空格
- 我想知道如何分别修改每个内部数组的值?
例如,如何更改 Array2 中索引 5 的值?并增加价值1?所以如果我打印 slot1 数组,我的值应该是这样的?
1 2 3 ----- 0 0 0 0 0 1 0 0 0 0 0 0 0 0
我已经查看了与此相关的其他问题,但是在处理两个由其他数组组成的二维数组时,我还没有找到明确的答案? ArrayList 最适合这种情况吗?
这是我的完整代码
public class Sandbox {
static int[][] slot1;
public static void main(String[] args) {
Sandbox.setCache();
Sandbox.displayCache();
}
public static void setCache() {
slot1 = new int[][] { { 1, 2 ,3}, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
}
public static void displayCache() {
for (int i = 0; i < slot1.length; i++) {
for (int j = 0; j < slot1[i].length; j++) {
System.out.print(slot1[i][j] + " "); // How to add a space between i and j?
}
}
}
}
【问题讨论】:
-
我对这个问题投了反对票,因为没有任何研究证据。请edit 您的问题包括您所做的研究以及准确您遇到困难的地方。如果你能做到这一点,我可能会撤回我的反对票。
标签: java arrays multidimensional-array