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<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 中的一样。后者是【类型指定】的。

 

转载于:https://www.cnblogs.com/sweetXiaoma/p/8559486.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值