更新时间:2023-01-23 16:40:08
以这说明.NET数组这很好:
Take .NET arrays which illustrate this nicely:
C#有 交错数组 被定义的区别在嵌套方式:
C# has a distinction between jagged arrays which are defined in a nested fashion:
int[][] jagged = new int[3][];
每个嵌套阵列可以具有不同的长度:
Each nested array can have a different length:
jagged[0] = new int[3];
jagged[1] = new int[4];
(请注意,嵌套阵列中的一个完全不初始化,即空
)
相反, 多维数组 定义如下:
By contrast, a multidimensional array is defined as follows:
int[,] multidim = new int[3, 4];
下面,它没有意义谈论嵌套数组的,确实试图访问 multidim [0]
将是一个编译时错误 - 你需要访问它提供的所有维度,即 multidim [0,1]
。
Here, it doesn’t make sense to talk of nested arrays, and indeed trying to access multidim[0]
would be a compile-time error – you need to access it providing all dimensions, i.e. multidim[0, 1]
.
它们的类型也不同,如上面的声明显示出来。
Their types are different too, as the declarations above reveal.
此外,它们的处理是完全不同的。例如,你可以在上面交错数组上迭代与类型的对象 INT []
:
Furthermore, their handling is totally different. For instance, you can iterate over the above jagged array with an object of type int[]
:
foreach (int[] x in jagged) …
但迭代多维数组与类型的项目做了 INT
:
foreach (int x in multidim) …
概念,交错数组是的数组的数组的(中...阵列......循环往复的阵列)的 T
的同时,多维数组的 T
的与一组访问模式的数组(即指数是一个元组)。
Conceptually, a jagged array is an array of arrays (… of arrays of arrays … ad infinitum) of T
while a multidimensional array is an array of T
with a set access pattern (i.e. the index is a tuple).