多维数组[] [] VS [,] [英] Multidimensional Array [][] vs [,]

查看:89
本文介绍了多维数组[] [] VS [,]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

有什么自己的区别?的(1)的产生错误,是什么原因?

What's their difference? (1) yields an error, what's the reason?

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

使用的(2)的会提示错误。为什么呢?

using (2) will prompt an error. Why?

推荐答案

一个是一个数组的数组,一个是一个二维数组。前者可以是锯齿状,后者是均匀的。

One is an array of arrays, and one is a 2d array. The former can be jagged, the latter is uniform.

这是一个双击[] [] 可有效地是:

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. 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:

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

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

更新

要阐明根据你的问题,你的#1有一个语法错误的原因是因为你有这样的:

To clarify based on your question, the reason your #1 had a syntax error is because you had this:

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

和您不能指定在施工时第二个索引。关键是,的ServicePoint是的的二维数组,但一维数组(阵列),因此,因为要创建一维数组(阵列),您只需指定一个索引:

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:

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];

希望帮助!

这篇关于多维数组[] [] VS [,]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆