あなたはあなたのコード内に3つの誤りがあります。
int[][][2] table = new int[7][4][2]; // <== Remove 2
for (int i = 0; i < 7; i++) {
for (int j = 0; i < 4; j++) { // <== Change i to j
table[i][j][1] = 2; // <== Arrays are zero-based
table[i][j][2] = 5; // <== -
}
}
修正したコードは次のとおりです。System.out.println(Arrays.deepToString(table))
と
int[][][] table = new int[7][4][2];
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 4; j++) {
table[i][j][0] = 2;
table[i][j][1] = 5;
}
}
テスト生成:指定された値で更新するには
[[[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]]]
、あなたを3番目の配列を置き換えることも、値を直接更新することもできます。ここで私は最初の2を更新するには、両方の方法を示しています。
int[][][] table = new int[7][4][2];
table[0][0] = new int[] { 1, 8 };
table[0][1][0] = 2;
table[0][1][1] = 7;
[[[1, 8], [2, 7], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]]]
あなたも、一度の操作で全体の最初の行を置き換えることができます。
int[][][] table = new int[7][4][2];
table[0] = new int[][] { {1, 8}, {2, 7}, {3, 6}, {4, 5} };
[[[1, 8], [2, 7], [3, 6], [4, 5]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]]]
を使用すると、各要素に2つの数字をしたい場合配列の 'Pair'のようなオブジェクト型が必要です。 –
ArrayListを使用せずに同様のことを行うことはできますか?たぶん3次元アレイですか? – bov25
'Pair [] []'を作ることができます。 –