Multidimensional Array And an Array of Arrays

本文详细解析了二维数组(double[,])与锯齿数组(double[][])的区别。二维数组为均匀数组,每个元素都必须拥有相同的维度;而锯齿数组为非均匀数组,允许每个子数组有不同的长度。文章通过实例对比了这两种数组类型的使用场景和限制。

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

That is, a double[][] can validly be:

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.

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

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

Quote From: Multidimensional Array [][] vs [,]

转载于:https://www.cnblogs.com/liao-hua/p/6280388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值