C# 數(shù)組

        static void Main(string[] args)
        {
            //數(shù)組用元素類型后加方括號的方式表示
            char[] a = new char[3]; //數(shù)組繼承自System.Array,一旦數(shù)組創(chuàng)建完畢,它的長度將不能更改
            a[0] = 'a';
            a[1] = 'b';
            a[2] = 'c';
            for (int i = 0; i < a.Length; i++)
                Console.WriteLine(a[i]);


            //數(shù)組初始化
            char[] b1 = new char[] { 'a', 'b', 'c' };
            char[] b2 = { 'a', 'b', 'c' };  //簡寫,省略new


            //創(chuàng)建數(shù)組時其元素總會用默認值初始化
            //值類型
            PStruct[] c = new PStruct[10];
            Console.WriteLine(c[3].x);  //0
            //引用類型
            PClass[] d = new PClass[10];
            for (int i = 0; i < d.Length; i++)  //必須實例化這10個PClass實例,否則會提示Runtime error,NullReferenceException
                d[i] = new PClass();
            Console.WriteLine(d[3].x);


            //多維數(shù)組:矩形數(shù)組和鋸齒形數(shù)組
            //(1)矩形數(shù)組:代表n維的內(nèi)存塊。使用逗號分離每個維度。
            int[,] e = new int[3, 3];
            for (int i = 0; i < e.GetLength(0); i++)    //GetLength返回給定維度的長度
                for (int j = 0; j < e.GetLength(1); j++)
                    e[i, j] = i * 3 + j;
            //也可按照如下方式初始化
            int[,] f1 = new int[,]
            {
                {0,1,2 },
                {3,4,5 },
                {6,7,8 }
            };
            //或者這樣初始化,省略new
            int[,] f2 =
            {
                {0,1,2 },
                {3,4,5 },
                {6,7,8 }
            };
            //或使用var
            var f3 = new int[,]
            {
                {0,1,2 },
                {3,4,5 },
                {6,7,8 }
            };

            //(2)鋸齒形數(shù)組:數(shù)組的數(shù)組。使用一對方括號對表示每個維度。
            int[][] g = new int[3][];
            for(int i = 0; i < g.Length; i++)   //每一個內(nèi)層數(shù)組都隱式初始化為null而不是一個空數(shù)組,因此都需要手動創(chuàng)建
            {
                g[i] = new int[3];
                for (int j = 0; j < g[i].Length; j++)
                    g[i][j] = i * 3 + j;
            }
            //也可按照以下方式初始化
            int[][] h1 = new int[][]
            {
                new int[]{0,1,2},
                new int[]{3,4,5},
                new int[]{6,7,8}
            };
            //或者這樣初始化,省略new
            int[][] h2 =
            {
                new int[]{0,1,2},
                new int[]{3,4,5},
                new int[]{6,7,8}
            };
            //或使用var
            var h3 = new int[][]
            {
                new int[]{0,1,2},
                new int[]{3,4,5},
                new int[]{6,7,8}
            };


            //邊界檢查
            int[] k = new int[5];
            Console.WriteLine(k[3]);    //若使用k[6]則會引發(fā)異常:IndexOutOfRangeException

        }
        //值類型
        public struct PStruct { public int x, y; }
        //引用類型
        public class PClass { public int x, y; }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容