C# 數組 【溫故而知新】

來源:互聯網
上載者:User

標籤:c#   數組   c#數組   

簡單的數組知識代碼及注釋講解
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 數組{    class Program    {        static void Main(string[] args)        {            //數組的聲明            int[] iArr;            string[] sArr;            //數組的初始化            iArr = new int[4];            sArr = new string[2];            //同時聲明和初始化數組            string[] str = new string[2];            //數組初始化器            int[] ii = new int[7] { 3, 4, 5, 6, 7, 8, 9 };//數組初始化器只能在聲明陣列變數時使用,不能在聲明數組之後使用            int[] iii = new int[] { 3, 4, 5, 6, 7, 8, 9 };//用花括弧初始化數組大小,可以不指定數組的大小,編譯器會自動統計元素的個數            int[] iiii = { 1, 2, 3, 4, 5, 6, 7 };//更簡化的方式            //訪問數組            //通過索引訪問            int[] myArr = new int[] { 4, 7, 11, 2 };            int v1 = myArr[0];            int v2 = myArr[1];            myArr[3] = 44;//更改數組中的值            //通過迴圈索引訪問            for (int i = 0; i < myArr.Length; i++)            {                Console.WriteLine(myArr[i]);            }            //通過迭代器訪問            foreach (var val in myArr)            {                Console.WriteLine(val);            }            //使用參考型別數組            //自訂類型的數組            Person[] myPerson = new Person[2];            //為數組的每個元素分配記憶體            myPerson[0] = new Person { FirstName = "aa", LastName = "bb" };            myPerson[1] = new Person { FirstName = "cc", LastName = "dd" };            //自訂類型數組使用初始化器            Person[] myPerson2 ={new Person{FirstName = "aa", LastName = "bb"  },                                 new Person {FirstName = "cc", LastName = "dd"}};            //多維陣列            //數組在初始化時候指定每一維的大小(也稱為階),聲明數組之後就不能修改其階數了            int[,] twoDim = new int[3, 3];            twoDim[0, 0] = 1;            twoDim[0, 1] = 1;            twoDim[0, 2] = 1;            twoDim[1, 0] = 1;            twoDim[1, 1] = 1;            twoDim[1, 2] = 1;            twoDim[2, 0] = 1;            twoDim[2, 1] = 1;            twoDim[2, 2] = 1;            //使用數組初始化器初始二維數組            int[,] twoArr ={                                {1,2,3},                                {4,5,6},                                {7,8,9}                            };            //使用數組初始化器初始三維數組            int[, ,] threeArr ={                                     {{1,2,3},{4,5,6}},                                     {{7,8,9},{10,11,12}},                                     {{13,14,15},{16,17,18}}                               };            Console.WriteLine(threeArr[1, 1, 2]);            //鋸齒數組:鋸齒數組的大小設定比較靈活,每一行都可以有不同的大小            //初始化鋸齒數組            int[][] jagged = new int[3][];            jagged[0] = new int[2] { 1, 2 };            jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };            jagged[2] = new int[3] { 9, 10, 11 };            //迭代鋸齒數組中的元素            for (int row = 0; row < jagged.Length; row++)            {                for (int element = 0; element < jagged[row].Length; element++)                {                    Console.WriteLine("row:{0},element:{1},value:{2}", row, element, jagged[row][element]);                }            }            Console.ReadKey();        }    }    public class Person    {        public string FirstName { get; set; }        public string LastName { get; set; }        public override string ToString()        {            return string.Format("{0},{1}", FirstName, LastName);        }    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.