[英]Why we have both jagged array and multidimensional array?
交錯數組和多維數組有什么區別。 一個對另一個有好處嗎?
為什么 Visual Studio 不允許我做一個
MyClass[][] abc = new MyClass[10][20];
(我們曾經在 C++ 中這樣做,但在 C# 中,它用紅色波紋線在 [20] 下划線。表示無效的排名說明符)
但很高興
MyClass[,] abc = new MyClass[10,20];
最后我怎樣才能在一行中初始化它(就像我們在簡單數組中做的那樣{new xxx...}{new xxx....}
)
MyClass[][,][,] itemscollection;
鋸齒狀數組是一個數組數組,所以int[][]
是一個int[]
數組,每個數組可以有不同的長度並在內存中占據自己的塊。 多維數組 ( int[,]
) 是單個內存塊(本質上是一個矩陣)。
您不能創建MyClass[10][20]
因為每個子數組都必須單獨初始化,因為它們是單獨的對象:
MyClass[][] abc = new MyClass[10][]; for (int i=0; i<abc.Length; i++) { abc[i] = new MyClass[20]; }
MyClass[10,20]
,因為它將單個對象初始化為具有 10 行和 20 列的矩陣。
MyClass[][,][,]
可以像這樣初始化(雖然沒有編譯測試):
MyClass[][,][,] abc = new MyClass[10][,][,]; for (int i=0; i<abc.Length; i++) { abc[i] = new MyClass[20,30][,]; for (int j=0; j<abc[i].GetLength(0); j++) { for (int k=0; k<abc[i].GetLength(1); k++) { abc[i][j,k] = new MyClass[40,50]; } } }
請記住,CLR 已針對單維數組訪問進行了大量優化,因此使用鋸齒狀數組可能比相同大小的多維數組更快。
鋸齒狀數組是數組的數組。 不保證每個數組的大小相同。 你可以有
int[][] jaggedArray = new int[5][];
jaggedArray[0] = new[] {1, 2, 3}; // 3 item array
jaggedArray[1] = new int[10]; // 10 item array
// etc.
它是一組相關的數組。
另一方面,多維數組更像是一個有凝聚力的分組,如盒子、桌子、立方體等,其中沒有不規則的長度。 也就是說
int i = array[1,10];
int j = array[2,10]; // 10 will be available at 2 if available at 1
矩形陣列的每一行總是具有相同數量的列。
MyClass[,] x = new MyClass[10,30]
每行有 30 列,而在鋸齒狀數組中,這不是必需的。 因此,我認為您必須分別初始化鋸齒狀數組中的每個“行”:
MyClass[][] x = new MyClass[10][];
for(int i = 0; i < 10; i++)
{
x[i] = new MyClass[30];
}
實際上,這意味着並非鋸齒狀數組中的每一行都必須包含相同數量的元素。 (在我的示例中,它確實具有相同數量的元素,但這不是必需的)。
你可以完美地做到這一點,例如:
MyClass[][] x = new MyClass[10][];
for(int i = 0; i < 10; i++)
{
x[i] = new MyClass[(30 + i)];
}
這對你來說可能是一篇有趣的文章。
廣告 3) 要初始化像[][,][,]
這樣的怪物,您可以執行以下操作:
int [,][,] multiArr1 = { { new int[,] { { 2, 2 }, { 1, 1 } },
new int[,] { { 2, 2 }, { 1, 1 } } },
{ new int[,] { { 2, 2 }, { 1, 1 } },
new int[,] { { 2, 2 }, { 1, 1 } } } };
int [,][,] multiArr2 = { { new int[,] { { 2, 2 }, { 1, 1 } },
new int[,] { { 2, 2 }, { 1, 1 } } },
{ new int[,] { { 2, 2 }, { 1, 1 } },
new int[,] { { 2, 2 }, { 1, 1 } } } };
int [][,][,] superMultiArray = { multiArr1, multiArr2 };
多維數組是一個C#的矩形數組,每一維只能有固定數量的元素。 下面的代碼示例向我們展示了如何在 C# 中聲明一個多維數組。
int[,] multiArray = new[3,3]
鋸齒狀數組是C#中arrays的數組,可以在其中構成不同大小的arrays。 下面的代碼示例向我們展示了如何在 C# 中聲明一個鋸齒狀數組。
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int [1];
jaggedArray[1] = new int[2];
jaggedArray[2] = new int[3];
在上面的代碼中,我們創建了大小為3的鋸齒狀數組jaggedArray,也就是說jaggedArray是一個3個arrays的數組。這3個arrays分別位於jaggedArray的索引0、1、2處。 從示例中可以清楚地看出,所有這些 arrays 的大小都不同。
鋸齒狀 arrays 應該優於傳統的多維 arrays,因為它們在 C# 中具有靈活性。例如,如果我們必須存儲一個人的興趣愛好,則首選方法是使用鋸齒狀數組,因為每個人的愛好數量並不相同愛好。 利益和許多其他事情也是如此。
如果您正在尋找設置了邊界的多維數組,請始終使用[,]
樣式語法。 這將確保每個部分的大小相同。
當您使用[][]
,實際上您正在創建一個數組數組。 這意味着每個數組的大小可以不同。 例如:
int[][] jaggedArray = new int[5][]
for(int index = 0; index < jaggedArray.Length ; ++index)
{
jaggedArray[index] = new int[index + 1];
}
內聯聲明看起來像這樣:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
對於 #1,請參閱此 SO 問題
對於鋸齒狀或多維內聯數組,請參閱此編程指南:
// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// Same array with dimensions specified at declaration.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
您不必指定維度 (array3D),但如果您知道它們永遠不會改變,那么了解您使用的維度 (array3Da) 會很有幫助。
對於多維數組,可以考慮一個盒子或矩形。 每行長度相同,每列長度相同。
在鋸齒狀數組中,行和列的大小可能不同。 例如,列或行的大小可能不同。 這將導致形狀可能不像矩形那樣是一條直線。 相反,兩側可能是鋸齒狀的。
現在我在這個例子中使用了 2 維/2 數組,但這適用於更多。
您需要了解數組的內部工作原理,除了將雙索引轉換為單個索引外,多維數組充當一維數組。
C# 中的 Jagged 數組是一個對象數組,這些對象依次是數組。
我認為 C# 中的 2d 鋸齒狀數組內存分配就像 C++ 和 C 中的 2d 數組。因為 2d 鋸齒狀數組有指向指針數組的指針,每個指針都指向一個元素數組(例如整數元素); 就像 C++ 中的這段代碼,
int** 2DArr {new int* [number1]};
for (int i = 0; i < number1; i++)
{
2DArr[i] = new int[number2];
}
下面代碼的內存分配與 C# 中的 2d 鋸齒狀數組相同。 但我對此表示懷疑,如果我的想法有誤,請您解釋一下。
這篇文章很舊,但這是我對此的看法。
鋸齒狀數組是多維數組。 多維數組有兩種類型:矩形和鋸齒狀。 矩形數組表示一個 n 維的內存塊,而鋸齒狀數組是數組的數組。
矩形陣列
矩形數組使用逗號分隔每個維度聲明。 以下語句聲明了一個矩形二維數組,其中維度為 3 × 3:
int[,] matrix = new int [3, 3];
鋸齒狀數組
鋸齒狀數組使用連續的方括號聲明來表示每個維度。 下面是一個聲明鋸齒狀二維數組的例子,其中最外面的維度是 3:
int[][] matrix = new int[3][];
public class ArrayExamples
{
//Multi-Dimensional Array are of 2 types
//1. Jagged Array: Array of Arrays
//2. rectangular Array: Array having more than one dimension
public void JaggedArray()
{
//Declaring an array with 3 element. Each element containing single dimension array.
//Benefit: Each single dimension array defined can be of different length.
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2, 3, 4, 5 };//single dimension array lengh:5
jaggedArray[1] = new int[] { 6,7,8};//single dimension array lengh:3
jaggedArray[2] = new int[] { 9, 10 };//single dimension array lengh:2
foreach (var array in jaggedArray)
foreach (var element in array)
{
Console.WriteLine(element);
}
}
public void RectangularArray()
{
//Declaring a 2 dimensional array with 5 rows and 2 columns.
//Benefits: When we want to declare an array with multiple dimension
//and we know the length as length should be predefined.
//
int[,] array2Dimensional = new int[5,2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 } };
//This loop will go through all the elements
//This will display all the elements i.e. 1,2,3,4,5,6,7,8,9,10
foreach (var element in array2Dimensional)
{
//Console.WriteLine(element);
}
//Accessing specific element in the 2 dimensional array.
//i.e. will display 1 which is the first element of first row and first column.
Console.WriteLine(array2Dimensional[0, 0]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.