美文网首页恰同学少年
C#常用集合类型

C#常用集合类型

作者: LH_晴 | 来源:发表于2017-06-03 15:37 被阅读30次

https://msdn.microsoft.com/zh-cn/library/system.collections(v=vs.110).aspx

https://msdn.microsoft.com/zh-cn/library/system.collections.generic(v=vs.110).aspx

1.ArrayList类

ArrayList类主要用于对一个数组中的元素进行各种处理。在ArrayList中主要使用Add、Remove、RemoveAt、Insert四个方法对栈进行操作。

Add方法用于将对象添加到 ArrayList 的结尾处;

Remove方法用于从 ArrayList 中移除特定对象的第一个匹配项;

RemoveAt方法用于移除 ArrayList 的指定索引处的元素;

Insert方法用于将元素插入 ArrayList 的指定索引处。

2.Stack类

Stack(堆栈)类主要实现了一个LIFO(Last In First Out,后进先出)的机制。元素从堆栈的顶部插入(入栈操作),也从堆栈的顶部移除(出栈操作)。在Stack中主要使用Push,Pop,Peek三个方法对栈进行操作。

Push方法用于将对象插入Stack 的顶部;

Pop方法用于移除并返回位于 Stack 顶部的对象;

Peek方法用于返回位于 Stack顶部的对象但不将其移除。

3.Queue类

Queue(队列)类主要实现了一个FIFO(First In First Out,先进先出)的机制。元素在队列的尾部插入(入队操作),并从队列的头部移出(出队操作)。在Queue中主要使用Enqueue、Dequeue、Peek三个方法对队进行操作。

Enqueue方法用于将对象添加到 Queue 的结尾处;

Dequeue方法移除并返回位于 Queue 开始处的对象;

Peek方法用于返回位于 Queue 开始处的对象但不将其移除。

4.Hashtable类

Hashtable(哈希表)是一种键/值对集合,这些键/值对根据键的哈希代码进行组织。在一个Hashtable中插入一对Key/Value时,它自动将Key值映射到Value,并允许获取与一个指定的Key相关联的Value。在Hashtable中主要使用Add、Remove两个方法对哈希表进行操作。

Add方法用于将带有指定键和值的元素添加到 Hashtable 中;

Remove方法用于从 Hashtable 中移除带有指定键的元素。

//实例化Hashtable类的对象

Hashtable student = new Hashtable ();

//向Hashtable中添加元素

student.Add("S1001", "Tom");

student.Add("S1002", "Jim");

student.Add("S1003", "Lily");

student.Add("S1004", "Lucy");

//遍历Hashtable

foreach (DictionaryEntry item in student)

{

string id = item .Key.ToString ();

string name = item .Value.ToString ();

Console.WriteLine("学生的ID:{0}   学生姓名:{1}",id,name);

}

//移除Hashtable中的元素

student.Remove("S1002");

说明:Hashtable不能包含重复的key。如果调用Add方法来添加一个keys数组中已有的key,就会抛出异常。为了避免这种情况,可以使用ContainsKey       方法来测试哈希表中是否包含一个特定的Key。

5.SortedList类

SortedList类也是键/值对的集合,但与哈希表不同的是这些键/值对是按键排序,并可以按照键和索引访问。在SortedList中主要使用Add、Remove、RemoveAt三个方法对SortedList进行操作。

Add方法用于将带有指定键和值的元素添加到 SortedList 中;

Remove方法用于从 SortedList 中移除带有指定键的元素;

RemoveAt方法用于移除 SortedList 的指定索引处的元素。

//实例化SortedListTest类的对象

SortedList student = new SortedList();

//向SortedList中添加元素

student.Add("S1001", "Tom");

student.Add("S1003", "Jim");

student.Add("S1002", "Lily");

student.Add("S1004", "Lucy");

//遍历SortedList

foreach (DictionaryEntry element in student)

{

string id = element.Key.ToString();

string name = element.Value.ToString();

Console.WriteLine("学生的ID:{0}   学生姓名:{1}", id, name);

}

//移除SortedList中key为“S1003”的元素

student.Remove("S1003");

//移除SortedList中索引为“0”的元素,即第一个元素

student.RemoveAt(0);

说明:同样SortedList也不能包含重复的key。而且使用foreach语句遍历SortedList对象时,会返回DictionaryEntry对象。该对象将根据Key属性,以排序后的顺序返回。

6.Dictionary类

varlist =newDictionary()

{

{"1",1},

{"2",2},

{"3",3}

};

list.Add("a", 1);

list.Add("b", 2);

list.Add("c", 3);

foreach(variteminlist)

{

Console.WriteLine(item.Key + item.Value);

}

//KeyValuePair

foreach(KeyValuePair kvinlist)

{

Console.WriteLine(kv.Key + kv.Value);

}

//通过键

foreach(stringkeyinlist.Keys)

{

Console.WriteLine(key + list[key]);

}

//直接取值

foreach(intvalinlist.Values)

{

Console.WriteLine(val);

}

//移除元素

foreach(stringkeyinnewList(list.Keys))

{

if(key =="a")

list.Remove(key);

if(key =="b")

list.Remove(key);

}

stringstr =String.Join(",",list.Values.Select(i => i.ToString()).ToArray());

相关文章

网友评论

    本文标题:C#常用集合类型

    本文链接:https://www.haomeiwen.com/subject/piqmfxtx.html