什么是锯齿状数组?

发布于 2024-08-27 16:01:35 字数 43 浏览 4 评论 0原文

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

What is a jagged array (in c#)? Any examples and when should one use it....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

泛滥成性 2024-09-03 16:01:35

交错数组是数组的数组。

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

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

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

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

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

A jagged array is an array of arrays.

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

That's a collection of five different string arrays, each could be a different length (they could also be the same length, but the point is there is no guarantee that they are).

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

This is different from a 2D array where it is rectangular, meaning each row has the same number of columns.

string[,] array = new string[3,5];
乞讨 2024-09-03 16:01:35

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

[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

A jagged array is the same in any language, but it's where you have a 2+ dimensional array with different array lengths in the second and beyond array.

[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
瞳孔里扚悲伤 2024-09-03 16:01:35

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

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 博客上的 这篇文章

Although the best answer is chosen by the question owner but still I want to present the following code to make jagged array more clear.

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

The output will be

1 2

0

3 4 5

Jagged arrays are used to store data in rows of varying length.

For more information check this post at MSDN blog.

一张白纸 2024-09-03 16:01:35

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

You can find more information here : http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Also :

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.

The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:

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

or

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
顾挽 2024-09-03 16:01:35

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

A jagged array is one in which you declare the number of rows during declaration but you declare number of columns during run time or also by user choice,simply its mean when you want different number of columns in each JAGGED array is suitable in that case

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());
            }
        }
简单 2024-09-03 16:01:35

交错数组是其中包含其他数组的数组。

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

用于窗口窗体应用程序的 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 关键字用于我们将在窗口窗体中使用的列表框,以便通过单击来查看结果按钮,该按钮也将在窗口窗体中使用。这里完成的所有编程都在按钮上完成。

Jagged array is an array with other arrays contained within.

A jagged array is a array in which the number of rows is fixed but the number of column is not fixed.

Code for jagged array in C# for window form application

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

As you can see in the above program no of rows is fixed to 3, but the number of columns is not fixed. So we have taken three different value of columns i.e. 5, 3 and 1. The ListBox1 keyword used in this code is for the listbox that we will use in the window form to see the result by the click of button which will be also used in the window form. All the programming done here is on the button.

衣神在巴黎 2024-09-03 16:01:35

交错数组是具有不同行数的多维数组。

Jagged Array is multidimensional array with different different number of rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文