多維數組[] []與[,] [重複]

本文翻譯自: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 二維陣列的圖形表示

在此處輸入圖片說明

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章