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的数组,元素数组的长度未知。

不同的格式:

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

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

哈 居然有人留言了。

简单说一下区别吧。

1、数组(Array)和 其余四个的区别是【类型固定】【长度固定】,其余四个长度都不固定。

2、ArrayList 和 List 的区别是 List 是【类型固定】的。

3、HashTable 和 Dictionary 的 区别和 2 种的一样。是【类型固定】的。

(编辑:雷林鹏 来源:网络)

猜你喜欢

转载自www.cnblogs.com/pengpeng1208/p/9267117.html