简体   繁体   English

这两种声明数组的方式有什么区别?

[英]What is the difference between these two ways of declaring an array?

What is the difference between: 有什么区别:

int [][] myArray;

and

int [,] myOtherArray;

The first is a jagged array: an array where each item in the array is another array 第一个是锯齿状数组:数组中的每个项目都是另一个数组

int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

The second is a multidimensional array, aka a matrix. 第二个是多维数组,也就是矩阵。

int[,] array = new int[4, 2]; // create a 4 by 2 matrix

myArray is a jagged array , or an array of arrays. myArray是一个锯齿状数组或数组数组。 Each element of myArray is itself an int[] . myArray每个元素本身都是一个int[]

myOtherArray is a rectangular (or multidimensional ) array - a single object containing all the data directly. myOtherArray是一个矩形 (或多维 )数组 - 一个直接包含所有数据的对象。

Which you should use really depends on the situation. 你应该使用哪种取决于具体情况。 Sometimes it can be handy to have an array for each "row" of data (with the ability to replace whole rows, and have rows with different lengths), whereas at other times it makes sense to force a uniform layout. 有时,为每个“行”数据创建一个数组(能够替换整行,并且具有不同长度的行)可能很方便,而在其他时候强制统一布局是有意义的。

I found the best way to understand it was to see a graphical representation of it =) 我发现理解它的最好方法是看到它的图形表示=)

int[][] jagged = new int[3][];
jagged[0] = new int[1];
jagged[1] = new int[2];
jagged[2] = new int[4];

will look like this 会是这样的

[0] - [0]
[1] - [0][1]
[2] - [0][1][2][3]

while a two+ dimensional 而两个维度

int[,] twodimensional = new int[3,4]

will look like this 会是这样的

[0,0][0,1][0,2][0,3]
[1,0][1,1][1,2][1,3]
[2,0][2,1][2,2][2,3]

第一个是锯齿状阵列,另一个是多维 - 不同之处在于锯齿状阵列的元素可以具有不同的尺寸和大小。

Jagged array: 锯齿状阵列:

int [][] myArray;

Rectangular array: 矩形阵列:

int [,] myOtherArray;

Quote Comparing Rectangular and Jagged Arrays : 引用比较矩形和锯齿状数组

The structure of rectangular and jagged arrays is significantly different. 矩形和锯齿状阵列的结构明显不同。

One-dimensional arrays have specific instructions in the CIL that allow them to be optimized for performance. 一维数组在CIL中具有特定指令,允许它们针对性能进行优化。 Rectangular arrays do not have these instructions, and are not optimized to the same level. 矩形数组没有这些指令,也没有针对同一级别进行优化。 Because of this, it can sometimes be more efficient to use jagged arrays of one-dimensional arrays—which can be optimized—than rectangular arrays, which cannot. 因此,使用一维数组的锯齿状阵列有时会更有效 - 可以优化矩形阵列,但不能。 On the other hand, the programming complexity can be less for a rectangular array because it can be treated as a single unit, rather than an array of arrays. 另一方面,对于矩形阵列,编程复杂度可以更小,因为它可以被视为单个单元,而不是阵列阵列。

This has to be a duplicate. 这必须是重复的。 One is a jagged array, one is a two-dimensional array. 一个是锯齿状阵列,一个是二维阵列。 You should be able to take it from there. 你应该可以从那里拿走它。

Both statements declare uninitialized multi-dimensional arrays of ints. 这两个语句都声明了未初始化的多维int数组。 The first is a jagged array and the second is 2-dimensional. 第一个是锯齿状阵列,第二个是二维。

You can initialize the 2-dimensional array at the same time as you declare it as follows: 您可以在声明它的同时初始化二维数组,如下所示:

int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

OR, 要么,

int[,] array = new int[4, 2];

Refer to the official documentation : 请参阅官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM