C#基本文法複習-使用數組和集合

來源:互聯網
上載者:User

標籤:

什麼是數組?

  數組是一個無序的元素序列,數組中的所有元素都具有相同的類型。

聲明陣列變數:

  int[] a;數組的大小不是聲明的一部分;

建立數組的執行個體:

  a=new int[3];

初始化陣列變數:

  int[] a=new int[2]{1,2};

  int[] a={1,2};

  Time[] schedule={new Time(1,2),new Time(5,30)};

建立隱式類型的數組:

  var name=new[]{new {Name="John",Age=44},new {Name="Diana",Age=45}};

遍曆數組:

  可以使用for語句,或者是foreach語句,在不知道數組的類型時可以將類型定義為var;

複製數組:

  int pins={9,3,7,2};

  int[] copy=new int[pins.Length];

  pins.copyto(copy,0);

  使用system.array的copy(源,拷貝到的數組,數組的長度);

使用多維陣列:

  int[,] items=new int[4,6];

什麼事集合類:

  集合類中的元素是object類型的,在儲存數值時會對數值直接進行裝箱;

arraylist集合類:

  有add(),remove()insert()方法;

queue集合類:

  有enqueque與dequeue兩種方法;

stack集合類:

  有push與pop兩種類型;

hashtable集合類:

  hashtable a =new hashtable();

  a["z"]=23;

  a["w"]=24;

  froeach(DictionaryEntry element in ages)

  {

    string name=(string)element.key;

    int age=(int)element.value;//拆箱進行強制類型轉化

  }

sortedList與hashtable使用方法相同;

C#基本文法複習-使用數組和集合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.