在C#中如何弄出一个动态二维数组?

2025-03-06 12:59:44
推荐回答(3个)
回答1:

我们一般说的动态,是指在运行过程中随时可以添加修改行列的,如果你已经确定知道要几行几列那就不是动态了,动态数组一般是用List实现的,比如

List> array = new List>();
List item = new List(new int[] { 3, 4, 5, 6 });
array.Add(item);
item = new List(new int[] { 30, 40, 50, 60 });
array.Add(item);
int m = array[1][2];//此时的m即为50

回答2:

public static void Main(string[] args)
        {
            int row = 2, column = 3;
            int[,] a = new int[row, column];
            Console.WriteLine(a.Length);
            row = 5; 
            column = 4;
            a = new int[row, column];
            Console.WriteLine(a.Length);
            
            Console.WriteLine("二维数组的使用:");
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    Console.Write(a[i, j]+ " ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }

回答3:

用泛型的list<>,挺好用的,习惯下比二维数组好用,支持查找、调用等等操作,方法比数组多。而且c#2008以上支持更好。