4x6的简单2d数组
public class arraytest {
public static void main(String[] args) {
int array[][] = new int[4][6];
int row = array.length;
int col = array[0].length;
System.out.println("Row length " + row);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++) {
array[1][1] = 5;
System.out.println("Array elements " + array[i][j]);
}
}
}
array [1] [1] = 5函数如何: 输出如下:
Row length 4
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 5
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
怎么来&#34;数组元素5&#34;在第8位,我无法理解逻辑。 &gt;任何人都可以解释这个逻辑。
答案 0 :(得分:1)
使用数组时,您必须知道第一个元素始终存储在0索引中。我假设通过键入array [1] [1],您希望将值5存储在第一行第一列中。然而,由于上述原因,[1] [1]实际上是指第二行第二列。
因此你的数组看起来像这样:
0 0 0 0 0 0
0 5 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
最后,因为你的for循环将列作为最里面的循环,所以它会在移动到第二行之前打印出第一行中的每一列。因此,在您最终达到设置为第八个输出的值之前,第一行输出六个0,第二行输出另一个0。
答案 1 :(得分:0)
array [1] [1]表示第二行和第二列的交集。因此,循环首先打印第一行(有6个元素),在第二行打印数组[1] [0]和数组[1] [1]。因此,数组[1] [1]位于第8位。
答案 2 :(得分:0)
您正在将元素1,1设置为5 ......其余元素将保持相同的值...
Row length 4
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 0
Array elements 5 <--- here is he!!
Array elements 0
Array elements ...
答案 3 :(得分:0)
我认为您忘记了数组索引从 0 开始!
这意味着一个4乘6的多维数组看起来像这样:
[0] [0] [0] [1] [0] [2] [0] [3] [0] [4] [0] [5] [1] [0] [1] [1] [1] [2] [1] [3] [1] [4] [1] [5] [2] [0] [2] [1] [2] [2] [2] [3] [2] [4] [2] [5] [3] [0] [3] [1] [3] [2] [3] [3] [3] [4] [3] [5]
正如你所看到的那样[1] [1]位于8位。
当你声明一个像这个数组[4] [6]这样的多维数组时,它会从0到3以及从0到5的那些行,因为数组索引从0开始而不是1。
答案 4 :(得分:0)
如果您是Java新手,数组将以索引0开头;就像String的第一个char是0.考虑长度,或者数组或String的最终索引为n-1。这个0索引逻辑也将应用于2D数组的列/行。
答案 5 :(得分:0)
请参阅此示例的输出,您的疑问将会明确:
public class arraytest {
public static void main(String[] args) {
int array[][] = new int[4][6];
int row = array.length;
int col = array[0].length;
System.out.println("Row length " + row);
System.out.println("Col length " + col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++) {
array[1][1] = 5;
System.out.println("Array elements at row "+i+" and column "+j+" is "+ array[i][j]);
}
}
}
<强>输出:强>
Row length 4
Col length 6
Array elements at row 0 and column 0 is 0
Array elements at row 0 and column 1 is 0
Array elements at row 0 and column 2 is 0
Array elements at row 0 and column 3 is 0
Array elements at row 0 and column 4 is 0
Array elements at row 0 and column 5 is 0
Array elements at row 1 and column 0 is 0
Array elements at row 1 and column 1 is 5
Array elements at row 1 and column 2 is 0
Array elements at row 1 and column 3 is 0
Array elements at row 1 and column 4 is 0
Array elements at row 1 and column 5 is 0
Array elements at row 2 and column 0 is 0
Array elements at row 2 and column 1 is 0
Array elements at row 2 and column 2 is 0
Array elements at row 2 and column 3 is 0
Array elements at row 2 and column 4 is 0
Array elements at row 2 and column 5 is 0
Array elements at row 3 and column 0 is 0
Array elements at row 3 and column 1 is 0
Array elements at row 3 and column 2 is 0
Array elements at row 3 and column 3 is 0
Array elements at row 3 and column 4 is 0
Array elements at row 3 and column 5 is 0