簡體   English   中英

什么是鋸齒狀陣列?

[英]What is a jagged array?

什么是鋸齒狀數組(在c#中)? 任何例子,什么時候應該使用它....

鋸齒狀數組是一個數組數組。

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

這是五個不同的字符串數組的集合,每個字符串數組可以是不同的長度(它們也可以是相同的長度,但重點是它們不能保證它們)。

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

這與2D數組不同,它是矩形的,意味着每行具有相同的列數。

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

盡管問題所有者選擇了最佳答案,但我仍然希望提供以下代碼以使鋸齒狀數組更清晰。

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博客上查看此帖子

您可以在此處找到更多信息: http//msdn.microsoft.com/en-us/library/2s05feca.aspx

另外:

鋸齒狀數組是一個數組,其元素是數組。 鋸齒狀陣列的元素可以具有不同的尺寸和大小。 鋸齒狀數組有時被稱為“數組數組”。 以下示例顯示如何聲明,初始化和訪問鋸齒狀數組。

以下是具有三個元素的一維數組的聲明,每個元素都是一個整數的一維數組:

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

鋸齒狀數組是在聲明期間聲明行數但在運行時或用戶選擇時聲明列數的數組,只是在每個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關鍵字用於我們將在窗口表單中使用的列表框,通過單擊按鈕查看結果,該按鈕也將用於窗口形式。 這里完成的所有編程都在按鈕上。

Jagged Array是具有不同行數的多維數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM