C#数组

50 阅读1分钟

一维数组 : 只有一行,列可以多行

1.定义方法

            int[] arr1 = { 1, 2, 3 };
            int[] arr2 = new int[3];
            int[] arr3 = new int[] { 1, 2, 3 };

2.得到数组的长度

            arr1.Length

二维数组 : 行可以多行,列可以多列,每行的列必须一样

1.定义方法

            int[,] arr1 = { { 1, 2 },
                            { 3, 4 },
                            { 5, 6 } };

            int[,] arr2 = new int[3 , 2];

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

2.得到数组的行数

            arr1.GetLength(0)

3.得到数组的列数

            arr1.GetLength(1)

4.给数组赋值

            arr1[1, 1] = 99;

交错数组 : 行可以多行,行是里面的一维数组,列可以多列,列是一维数组里面的元素,每行不需要一样

1.定义方法

            int[][] arr1 = { new int[] { 1, 2, 3 },
                             new int[] { 4, 5},
                             new int[] { 1 } 
                            };

            int[][] arr2 = new int[3][];

            int[][] arr3 = new int[][] { new int[] { 1, 2, 3 },
                                         new int[] { 4, 5},
                                         new int[] { 1 }
                                        };

2.得到数组的行数

            arr1.GetLength(0)

3.得到数组的列数

            arr1[0].Length

4.给数组赋值

            arr1[1][1] = 99;