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的數組,元素數組的長度未知。string

不一樣的格式:io

int[] numbers = new int[5];class

int[] numbers2 = new []{100, 200, 300, 400, 500};List

int[] numbers3 = {100, 200, 300, 400, 500};二維數組

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

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

System.Collections.ArrayListnew

ArrayList al = new ArrayList();

al.Add(5);

al.Add("Hello Tom");System.Collections.Generic.List

List intList = new List();

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

Dictionary dic = new Dictionary();

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

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

哈 竟然有人留言了。

簡單說一下區別吧。

一、數組(Array)和 其他四個的區別是【類型固定】【長度固定】,其他四個長度都不固定。

二、ArrayList 和 List 的區別是 List 是【類型固定】的。

三、HashTable 和 Dictionary 的 區別和 2 種的同樣。是【類型固定】的。

(編輯:雷林鵬 來源:網絡)

相關文章
相關標籤/搜索