標籤:
什麼是數組?
數組是一個無序的元素序列,數組中的所有元素都具有相同的類型。
聲明陣列變數:
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#基本文法複習-使用數組和集合