c# int[]、Array、ArrayList

            //public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
            //public class ArrayList : IList, ICollection, IEnumerable, ICloneable

            //1、Array只能存储同类型对象,但object[]例外
            //2、ArrayList可以存储不同类型的数据,其内部使用object[] _items实现了装箱
            //3、Array始终是连续存放的,但ArrayList不一定
            //4、Array初始化必须指定大小,且创建后的数组大小是固定的,但ArrayList的大小可动态指定

            //创建Array(数组)
            var arry = Array.CreateInstance(typeof(int), 3);
            arry.SetValue(1, 0);
            arry.SetValue(2, 1);
            arry.SetValue(3, 2);

            //简写
            int[] arry = { 1, 2, 3 };
            int[] arry = new int[] { 1, 2, 3 };
            int[] arry = new int[3] { 1, 2, 3 };

            //二维数组
            int[,] arry = new int[3, 2] {
                { 1, 2 },
                { 1, 2 },
                { 1, 2 }
            };

            //三维数组
            int[,,] arry = new int[3, 1, 4] {
                { { 1, 2, 3, 4 } },
                { { 1, 2, 3, 4 } },
                { { 1, 2, 3, 4 } }
            };

            int[,,] arry = new int[3, 3, 1] {
                { { 1 }, { 2 }, { 3 } },
                { { 1 }, { 2 }, { 3 } },
                { { 1 }, { 2 }, { 3 } }
            };

            int[,,] arry = new int[1, 3, 3] {
                {
                    { 1, 2, 3 },
                    { 1, 2, 3 },
                    { 1, 2, 3 }
                }
            };

            //创建ArrayList
            var arrayList = new ArrayList();
            arrayList.Add("");

参考文献:

Array 类 | Microsoft Docs

ArrayList 类 | Microsoft Docs

posted @ 2021-05-20 10:32  狼王爷  阅读(494)  评论(0编辑  收藏  举报