多维数组[] []与[,] [重复]

本文翻译自:Multidimensional Array [][] vs [,] [duplicate]

This question already has answers here : 这个问题已经在这里有了答案
Closed 6 years ago . 6年前关闭。
double[][] ServicePoint = new double[10][9]; // <-- gives an error (1)
double[,] ServicePoint = new double[10,9]; // <-- ok (2)

What's their difference? 他们有什么区别? (1) yields an error, what's the reason? (1)产生错误,原因是什么?

And

double d = new double[9]
ServicePoint[0] = d;

using (2) will prompt an error. 使用(2)将提示错误。 Why? 为什么?


#1楼

参考:https://stackoom.com/question/qjKr/多维数组-与-重复


#2楼

One is an array of arrays, and one is a 2d array. 一个是数组数组,一个是2d数组。 The former can be jagged, the latter is uniform. 前者可以锯齿状,后者是统一的。

That is, a double[][] can validly be: 也就是说, double[][]可以有效地是:

double[][] x = new double[5][];

x[0] = new double[10];
x[1] = new double[5];
x[2] = new double[3];
x[3] = new double[100];
x[4] = new double[1];

Because each entry in the array is a reference to an array of double . 因为数组中的每个条目都是对double数组的引用。 With a jagged array, you can do an assignment to an array like you want in your second example: 对于锯齿状数组,可以在第二个示例中对数组进行分配:

x[0] = new double[13];

On the second item, because it is a uniform 2d array, you can't assign a 1d array to a row or column, because you must index both the row and column, which gets you down to a single double : 在第二项中,因为它是统一的2d数组,所以不能将1d数组分配给行或列,因为必须同时对行和列进行索引,这会使您下降为一个double

double[,] ServicePoint = new double[10,9];

ServicePoint[0]... // <-- meaningless, a 2d array can't use just one index.

UPDATE : 更新

To clarify based on your question, the reason your #1 had a syntax error is because you had this: 根据您的问题进行澄清,您的#1出现语法错误的原因是因为您有以下原因:

double[][] ServicePoint = new double[10][9];

And you can't specify the second index at the time of construction. 并且您不能在构造时指定第二个索引。 The key is that ServicePoint is not a 2d array, but an 1d array (of arrays) and thus since you are creating a 1d array (of arrays), you specify only one index: 关键在于ServicePoint 不是 2d数组,而是1d数组(数组),因此,由于要创建1d数组(数组),因此只能指定一个索引:

double[][] ServicePoint = new double[10][];

Then, when you create each item in the array, each of those are also arrays, so then you can specify their dimensions (which can be different, hence the term jagged array): 然后,当在阵列中创建的每个项目,每个这些也都是阵列,所以可以指定其尺寸(其可以是不同的,因此,长期交错数组):

ServicePoint[0] = new double[13];
ServicePoint[1] = new double[20];

Hope that helps! 希望有帮助!


#3楼

double[][] are called jagged arrays , The inner dimensions aren't specified in the declaration. double[][]被称为锯齿状数组 ,内部尺寸未在声明中指定。 Unlike a rectangular array , each inner array can be an arbitrary length. 矩形数组不同,每个内部数组可以是任意长度。 Each inner array is implicitly initialized to null rather than an empty array. 每个内部数组都隐式初始化为null,而不是一个空数组。 Each inner array must be created manually: Reference [C# 4.0 in nutshell The definitive Reference] 每个内部数组必须手动创建:参考[简而言之C#4.0权威参考]

for (int i = 0; i < matrix.Length; i++)
{
    matrix[i] = new int [3]; // Create inner array
    for (int j = 0; j < matrix[i].Length; j++)
        matrix[i][j] = i * 3 + j;
}

double[,] are called rectangular arrays , which are declared using commas to separate each dimension. double[,]称为rectangular arrays ,使用逗号分隔每个rectangular arrays来声明它们。 The following piece of code declares a rectangular 3-by-3 two-dimensional array, initializing it with numbers from 0 to 8: 下面的代码声明一个矩形的3×3二维数组,并使用从0到8的数字对其进行初始化:

int [,] matrix = new int [3, 3];
for (int i = 0; i < matrix.GetLength(0); i++)
    for (int j = 0; j < matrix.GetLength(1); j++)
        matrix [i, j] = i * 3 + j;

#4楼

double[,]是2D数组(矩阵),而double[][]是数组( 锯齿数组 ),语法为:

double[][] ServicePoint = new double[10][];

#5楼

double[][] is an array of arrays and double[,] is a matrix. double[][]是一个数组数组,而double[,]是一个矩阵。 If you want to initialize an array of array, you will need to do this: 如果要初始化一个数组数组,则需要执行以下操作:

double[][] ServicePoint = new double[10][]
for(var i=0;i<ServicePoint.Length;i++)
    ServicePoint[i] = new double[9];

Take in account that using arrays of arrays will let you have arrays of different lengths: 考虑到使用数组数组将使您拥有不同长度的数组:

ServicePoint[0] = new double[10];
ServicePoint[1] = new double[3];
ServicePoint[2] = new double[5];
//and so on...

#6楼

In the first instance you are trying to create what is called a jagged array. 在第一个实例中,您尝试创建一个称为锯齿状数组的东西。

double[][] ServicePoint = new double[10][9].

The above statement would have worked if it was defined like below. 如果上面的声明定义如下,则它会起作用。

double[][] ServicePoint = new double[10][]

what this means is you are creating an array of size 10 ,that can store 10 differently sized arrays inside it.In simple terms an Array of arrays.see the below image,which signifies a jagged array. 这意味着您正在创建一个大小为10的数组,可以在其中存储10个大小不同的数组。简单地说,是一个数组数组。请参见下图,它表示锯齿状数组。

在此处输入图片说明

http://msdn.microsoft.com/en-us/library/2s05feca(v=vs.80).aspx http://msdn.microsoft.com/zh-CN/library/2s05feca(v=vs.80).aspx

The second one is basically a two dimensional array and the syntax is correct and acceptable. 第二个基本上是二维数组,语法是正确的并且可以接受。

  double[,] ServicePoint = new double[10,9];//<-ok (2)

And to access or modify a two dimensional array you have to pass both the dimensions,but in your case you are passing just a single dimension,thats why the error 要访问或修改二维数组,您必须同时传递两个维,但是在您的情况下,您只传递一个维,这就是为什么错误

Correct usage would be 正确的用法是

ServicePoint[0][2] ,Refers to an item on the first row ,third column. ServicePoint[0][2] ,指的是第一行第三列上的项目。

Pictorial rep of your two dimensional array 二维阵列的图形表示

在此处输入图片说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值