本文翻译自:Syntax for creating a two-dimensional array
Consider: 考虑:
int[][] multD = new int[5][];
multD[0] = new int[10];
Is this how you create a two-dimensional array with 5 rows and 10 columns? 这是如何创建具有5行10列的二维数组吗?
I saw this code online, but the syntax didn't make sense. 我在网上看到了这段代码,但是语法没有意义。
参考:https://stackoom.com/question/pJxV/用于创建二维数组的语法
The most common idiom to create a two-dimensional array with 5 rows and 10 columns is: 创建具有5行10列的二维数组的最常见惯用法是:
int[][] multD = new int[5][10];
Alternatively, you could use the following, which is more similar to what you have, though you need to explicitly initialize each row: 另外,您可以使用以下内容,尽管您需要显式初始化每一行,但它与您所拥有的内容更相似:
int[][] multD = new int[5][];
for (int i = 0; i < 5; i++) {
multD[i] = new int[10];
}
Try: 尝试:
int[][] multD = new int[5][10];
Note that in your code only the first line of the 2D array is initialized to 0. Line 2 to 5 don't even exist. 请注意,在您的代码中,仅2D数组的第一行被初始化为0。第2至5行甚至不存在。 If you try to print them you'll get null
for everyone of them. 如果尝试打印它们,则每个人都将得到null
。
Try the following: 请尝试以下操作:
int[][] multi = new int[5][10];
... which is a short hand for something like this: ...这是类似这样的缩写:
int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];
Note that every element will be initialized to the default value for int
, 0
, so the above are also equivalent to: 请注意,每个元素都将初始化为int
, 0
的默认值,因此上述内容也等同于:
int[][] multi = new int[][]{
{ 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 }
};
You can create them just the way others have mentioned. 您可以按照其他人提到的方式创建它们。 One more point to add: You can even create a skewed two-dimensional array with each row, not necessarily having the same number of collumns, like this: 还有一点要补充:您甚至可以为每行创建一个倾斜的二维数组,不一定具有相同的列数,如下所示:
int array[][] = new int[3][];
array[0] = new int[3];
array[1] = new int[2];
array[2] = new int[5];
We can declare a two dimensional array and directly store elements at the time of its declaration as: 我们可以声明一个二维数组,并在声明时将元素直接存储为:
int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}};
Here int represents integer type elements stored into the array and the array name is 'marks'. 这里的int表示存储在数组中的整数类型元素,并且数组名称为'marks'。 int is the datatype for all the elements represented inside the "{" and "}" braces because an array is a collection of elements having the same data type. int是括号“ {”和“}”内表示的所有元素的数据类型,因为数组是具有相同数据类型的元素的集合。
Coming back to our statement written above: each row of elements should be written inside the curly braces. 回到上面的声明:元素的每一行都应该写在花括号内。 The rows and the elements in each row should be separated by a commas. 行和每行中的元素应以逗号分隔。
Now observe the statement: you can get there are 3 rows and 5 columns, so the JVM creates 3 * 5 = 15 blocks of memory. 现在观察以下语句:您可以获得3行5列,因此JVM创建了3 * 5 = 15个内存块。 These blocks can be individually referred ta as: 这些块可以分别称为:
marks[0][0] marks[0][1] marks[0][2] marks[0][3] marks[0][4]
marks[1][0] marks[1][1] marks[1][2] marks[1][3] marks[1][4]
marks[2][0] marks[2][1] marks[2][2] marks[2][3] marks[2][4]
NOTE: 注意:
If you want to store n elements then the array index starts from zero and ends at n-1 . 如果要存储n个元素,则数组索引从零开始,以n-1结束。 Another way of creating a two dimensional array is by declaring the array first and then allotting memory for it by using new operator. 创建二维数组的另一种方法是先声明该数组,然后使用new运算符为其分配内存。
int marks[][]; // declare marks array
marks = new int[3][5]; // allocate memory for storing 15 elements
By combining the above two we can write: 通过结合以上两个,我们可以编写:
int marks[][] = new int[3][5];