C# 中的集合(Array/ArrayList/List/HashTable/Dictionary)

小馬同窗的編程日記。

C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)

int [] numbers = new int[5]; // 長度爲5,元素類型爲 int。
string[,] names = new string[5,4]; // 5*4 的二維數組
byte[][] scores = new byte[5][]; // 長度爲 5 的數組,元素爲 byte的數組,元素數組的長度未知。html

不一樣的格式:
int[] numbers = new int[5];
int[] numbers2 = new []{100, 200, 300, 400, 500};
int[] numbers3 = {100, 200, 300, 400, 500};編程

 

names.GetLength(0); // 得到二維數組的橫向長度數組

names.GetLength(1); // 得到二維數組的縱向長度。post

 

System.Collections.ArrayListspa

ArrayList al = new ArrayList();htm

al.Add(5);blog

al.Add("Hello Tom");get

System.Collections.Generic.List<T>string

List<int> intList = new List<int>();io

intList.Add(500);

intList.AddRange(new int[]{1,100};

intList.Insert(1, 1000);

cw(intList.Contains(100));

cw(intList.indexOf(10));

System.Collections.HashTable

HashTable ht = new HashTable();

ht.Add("name", "Tom");

ht.Add("age", 18);

System.Collections.Generic.Dictionary<TKey, TValue>

Dictionary<string, string> dic = new Dictionary<string, string>();

dic.Add("name", "Tom");

dic.Add("age", "eighteen");

 

哈  竟然有人留言了。

簡單說一下區別吧。

一、數組(Array)和 其他四個的區別是【類型指定】【長度固定】,其他四個長度均可以不固定(也能夠指定長度)。

二、ArrayList 和 List<T> 的區別是 List<T> 是【類型指定】的。

三、HashTable 和 Dictionary<Tkey, Tvalue> 的 區別和 2 中的同樣。後者是【類型指定】的。

 

 
分類:  C#
相關文章
相關標籤/搜索