如何在C#中创建不规则数组?

3

我熟悉C语言,可以像这样编写代码:

uint array[0xFFFF][20];

但是我不知道如何在C#中实现这一点。我尝试了所有微软教程和其他教程,但都没有成功。

有人可以帮我吗?


可能是初始化锯齿形数组的重复问题。 - nawfal
6个回答

11

官方教程。基本上,您首先创建“外部”数组,然后循环遍历它,并逐个创建每个“内部”数组。

两个示例:

int[][] a = new int[] { new int[]{ 1, 2 }, new int[]{ 3, 4, 5, 6, 7 }};

int[][] c = new int[100][];
for (int i = 0; i < c.length; i++) c[i] = new int[5];

如果你只需要一个二维矩形数组,可以使用int[,] b = new int[10, 7];


是的,我看过那个实现。但如果我使用它,并且我的内部数组有100个元素,我必须写下那句话100次。对吗? - user1439564
我刚刚看到这个:data[,] = new uint[0xFFFF, 20]; 看起来它能解决问题。 - user1439564
当然可以,这就是我在最后提到的矩形2D数组。 - Matzi

2
int[][] jaggedArray = new int[3][];
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 };

请参考不规则数组 (C# 编程指南)


1

我不知道在C语言中如何操作,但是在C#中你可以使用交错数组:

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

http://www.dotnetperls.com/jagged-array


你的语法完全错了。你只填充了第一个数组。 - Security Hound

0
创建和显示锯齿数组的元素
 int[][] a = new int[2][];//its mean num of row is 2 which fixed
        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());
            }
        }
        //loop for out put the values of jagged array
        for (int row = 0; row < a.Length; row++)
        {
            for (int col = 0; col < a[row].Length; col++)
                Console.Write(a[row][col]+"\t");
            Console.WriteLine("");
        }

在这里我使用了插入循环

a[row].length

使用

而不是

a.length,

原因是每一行的列数都不同,所以在第二个循环中,您必须指定每一行的长度。


@matzi,你的第一行代码有误,可以尝试使用以下代码。你写的是"new int[]",这是错误的。下面是正确的内部嵌套数组的写法: int[][] a = new int[][] { new int[]{ 1, 2 }, new int[]{ 3, 4, 5, 6, 7 }}; - Adiii

0
namespace jaggedarray
{
    class Program
    {
        static void Main(string[] args)
        {

            int[][] jagged = new int[5][];
            jagged[0] = new int[4] { 22, 33,44,55 };
            jagged[1] = new int[1] { 2 };
            jagged[2] = new int[4] { 1, 2, 3, 4 };
            jagged[3] = new int[2] { 12,13};
            jagged[4] = new int[1] {2};
            for (int i = 0; i < jagged.Length; i++ ) {
                for (int j = 0; j < jagged[i].Length; j++)
                {
                    Console.Write("\t{0}",jagged[i][j]);
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            // input jagged array

            int [][] arrays=new int[5][];
            Console.WriteLine("Please Enter the ValueType to make a jagged array:");
            arrays[0] = new int[1];
            arrays[1] = new int[2];
            arrays[2]=new int [3];
            arrays[3]=new int [4];
            arrays[4] = new int[5];
            for (int i = 0; i < arrays.Length;i++ ) {

                for (int j = 0; j < arrays[i].Length; j++)
                {
                    arrays[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.WriteLine("This is jagged array::");
            for (int i = 0; i < arrays.Length;i++ ) {

                for (int j = 0; j < arrays[i].Length;j++ ) {
                    Console.Write("\t{0}", arrays[i][j]);

                }
                Console.WriteLine();
            }
        }

    }
}

3
你好,欢迎来到Stack Overflow。仅有代码的回答通常不太有用,除非它们解释了其作用。特别是对于长段的代码来说更是如此。请考虑添加一些说明,解释你所改变或编写的内容。 - JasonMArcher

0

我认为这可能会对你有所帮助

       static void Main(string[] args)
    {
        Console.WriteLine("enter the size of rows");

        int n = Convert.ToInt32(Console.ReadLine());

        int[][] a = new int[n][];
        for (int i = 0; i < a.Length; i++)
        {
            Console.WriteLine("enter the number of elements for row {0}", i + 1);
            int x = Convert.ToInt32(Console.ReadLine());
            a[i]=new int[x];
            for (int j = 0; j < a[i].Length; j++)
            {
                a[i][j] = Convert.ToInt32(Console.ReadLine());
            }
        }
        for (int i = 0; i < a.Length; i++)
        {
            for (int j = 0; j < a[i].Length; j++)
            {
                Console.Write(a[i][j]+"\t"); 
            }
            Console.WriteLine();
        }

            Console.ReadKey();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接