【问题标题】:What is a jagged array?什么是锯齿状阵列?
【发布时间】:2011-02-04 07:51:19
【问题描述】:

什么是锯齿状数组(在 c# 中)?任何示例以及何时应该使用它....

【问题讨论】:

  • 锯齿状数组存在于几乎所有使用数组的语言中
  • 另请注意,其他内容可能是锯齿状的,例如 XML 路径(某些元素可能有子子元素而其他元素没有)、列表(列表的)、字典等。只是想我会帮助您将不同的概念关联为相似或具有相似的基础结构。
  • Eric Lippert here 对锯齿状数组进行了很好的阅读。

标签: c# jagged-arrays


【解决方案1】:

锯齿状数组是数组的数组。

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

这是五个不同字符串数组的集合,每个数组的长度可能不同(它们也可能是相同的长度,但关键是它们没有保证)。

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

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

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

【讨论】:

  • 确切地说,内部数组并非必然都具有相同的长度;他们很可能是。将多维数组实现为锯齿状数组实际上很常见。
  • @Asik,我同意。第一句话中关于长度的词是由另一个用户编辑的。会更新。
【解决方案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

【讨论】:

    【解决方案3】:

    您可以在这里找到更多信息: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 };
    

    【讨论】:

      【解决方案4】:

      锯齿状数组是一个包含其他数组的数组。

      交错数组是行数固定但列数不固定的数组。

      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,但列数不固定。因此,我们采用了三个不同的列值,即 5、3 和 1。此代码中使用的 ListBox1 关键字用于我们将在窗口形式中使用的列表框,通过单击按钮查看结果,该按钮也将在窗体中使用。这里完成的所有编程都在按钮上。

      【讨论】:

      • 您不应将上述代码仅限于表单。你真的明白有多少操作只需要ListBox1.Items.Add(...)这一行就可以完成吗?这对算法来说是一个巨大的减速。所有操作都应该在处理器和内存之间进行,而不涉及用户界面。
      【解决方案5】:

      虽然问题所有者选择了最佳答案,但我仍然想提供以下代码以使锯齿状数组更加清晰。

      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
      

      锯齿状数组用于将数据存储在不同长度的行中。

      更多信息请查看this post at MSDN blog

      【讨论】:

        【解决方案6】:

        锯齿状数组是一种在声明期间声明行数但在运行时声明列数或由用户选择的数组,当您希望每个 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());
                    }
                }
        

        【讨论】:

          【解决方案7】:

          锯齿状数组是具有不同行数的多维数组。

          【讨论】:

          • Yoru 的答案与自 2010 年以来其他答案的陈述相同 - 所以你没有增加任何价值。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-09
          • 2017-03-21
          相关资源
          最近更新 更多