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


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

不同的格式:
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); // 獲得二維數組的縱向長度。

 

System.Collections.ArrayList

ArrayList al = new ArrayList();

al.Add(5);

al.Add("Hello Tom");

System.Collections.Generic.List<T>

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

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");

 

哈  居然有人留言了。

簡單說一下區別吧。

1、數組(Array)和 其余四個的區別是【類型指定】【長度固定】,其余四個長度都可以不固定(也可以指定長度)。

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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM