什么是锯齿状的arrays?

什么是锯齿arrays(在C#中)? 任何示例和何时应该使用它….

锯齿形数组是数组的数组。

string[][] arrays = new string[5][]; 

这是五个不同的string数组的集合,每个数组可以是不同的长度(他们也可以是相同的长度,但重点是不能保证它们是)。

 arrays[0] = new string[5]; arrays[1] = new string[100]; ... 

这与矩形的二维数组不同,这意味着每行都有相同的列数。

 string[,] array = new string[3,5]; 

一个锯齿状的数组在任何语言中都是一样的,但是在第二个数组和第二个数组中,它是具有不同数组长度的2+维数组的地方。

 [0] - 0, 1, 2, 3, 4 [1] - 1, 2, 3 [2] - 5, 6, 7, 8, 9, 10 [3] - 1 [4] - [5] - 23, 4, 7, 8, 9, 12, 15, 14, 17, 18 

你可以在这里find更多的信息: http : //msdn.microsoft.com/en-us/library/2s05feca.aspx

另外:

锯齿状数组是元素为数组的数组。 锯齿状arrays的元素可以具有不同的尺寸和大小。 锯齿状的数组有时被称为“数组arrays”。 以下示例显示如何声明,初始化和访问锯齿状数组。

以下是一个具有三个元素的单维数组的声明,每个元素都是一个整数的一维数组:

 jaggedArray[0] = new int[5]; jaggedArray[1] = new int[4]; jaggedArray[2] = new int[2]; 

要么

 jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 }; jaggedArray[1] = new int[] { 0, 2, 4, 6 }; jaggedArray[2] = new int[] { 11, 22 }; 

虽然最好的答案是由问题所有者select的,但是我仍然想提出下面的代码来使锯齿状的数组更清楚。

 using System; class Program { static void Main() { // Declare local jagged array with 3 rows. int[][] jagged = new int[3][]; // Create a new array in the jagged array, and assign it. jagged[0] = new int[2]; jagged[0][0] = 1; jagged[0][1] = 2; // Set second row, initialized to zero. jagged[1] = new int[1]; // Set third row, using array initializer. jagged[2] = new int[3] { 3, 4, 5 }; // Print out all elements in the jagged array. for (int i = 0; i < jagged.Length; i++) { int[] innerArray = jagged[i]; for (int a = 0; a < innerArray.Length; a++) { Console.Write(innerArray[a] + " "); } Console.WriteLine(); } } } 

输出将是

 1 2 0 3 4 5 

锯齿形数组用于存储不同长度行的数据。

有关更多信息,请在MSDN博客上查看此帖 。

锯齿状数组是一种在声明中声明行数但在运行时声明列数的函数,也可以是用户select,在每个JAGGED数组中,列的数量不同时,

 int[][] a = new int[6][];//its mean num of row is 6 int choice;//thats i left on user choice that how many number of column in each row he wanna to declare for (int row = 0; row < a.Length; row++) { Console.WriteLine("pls enter number of colo in row {0}", row); choice = int.Parse(Console.ReadLine()); a[row] = new int[choice]; for (int col = 0; col < a[row].Length; col++) { a[row][col] = int.Parse(Console.ReadLine()); } } 

Jagged数组是其中包含其他数组的数组。

锯齿状数组是行数固定但列数不固定的数组。

C#中用于锯齿形数组的窗口表单应用程序的代码

 int[][] a = new int[3][]; a[0]=new int[5]; a[1]=new int[3]; a[2]=new int[1]; int i; for(i = 0; i < 5; i++) { a[0][i] = i; ListBox1.Items.Add(a[0][i].ToString()); } for(i = 0; i < 3; i++) { a[0][i] = i; ListBox1.Items.Add(a[0][i].ToString()); } for(i = 0; i < 1; i++) { a[0][i] = i; ListBox1.Items.Add(a[0][i].ToString()); } 

正如你在上面的程序中看到的,行的数目固定为3,但列数不固定。 所以我们已经取得了三个不同的列值,即ListBox1和1.在这个代码中使用的ListBox1关键字是用于我们将在窗口窗体中使用的列表框,通过点击button来查看结果,这也将被用于窗口窗体。 所有在这里完成的编程都在button上。