一.哈希表:Hashtable表示键/值对的集合,这些键/值对根据键的哈希代码进行组织。Hashtable是两行无限列的表格,是两行键值对,数组就是特殊哈希表。创建哈希表需要导入using System.Collections命名空间。Hashtable实现了ICollection和IEnumerable接口,它是通过索引来获得值,通过Add(object,object)方法添加键和值。
View Code
1 usingSystem;2 usingSystem.Collections;3 public classTest4 {5 public static voidMain()6 {7 //创建哈希表
8 Hashtable table = newHashtable();9 //添加键值对
10 table.Add("one","宫崎骏");11 table.Add(2,"久石让");12 table.Add("三",DateTime.Now);13 //拿出"久石让"(取值)
14 object o =table[2];15 Console.WriteLine(o.ToString());16 object o1 =table["三"];17 Console.WriteLine(o1.ToString());18 }19 }
运行结果为:
上面例子运用了哈希表的Add方法。Add方法能将带有指定键和值的元素添加到Hashtable中,Add方法在c#中的定义是:
public void Add
{
object key;
object value;
}
这说明哈希表中键/值赋值为什么类型都可以,因为任何类型都可以隐式装箱为object类型。哈希表中Item属性:获取或设置与指定键相关联的值。在c#中的表达形式为:
public virtual object Item[object key]
{
get;set;
}
下面例子具体展示如何设置项内容(Item),帮助理解上边c#中Item的表达式……
View Code
1 usingSystem;2 usingSystem.Collections;3 public classTest4 {5 public static voidMain()6 {7 //创建哈希表
8 Hashtable table = newHashtable();9 //添加键值对
10 table.Add("one","宫崎骏");11 table.Add(2,"久石让");12 table.Add("三",DateTime.Now);13 //拿出"久石让"(取值) 系统会默认调用ToString()
14 object o =table[2];15 Console.WriteLine(o.ToString());16 object o1 =table["三"];17 Console.WriteLine(o1.ToString());18 //拿出"久石让"为"方文山"(设置值) ToString()
19 table[2] = "方文山";20 Console.WriteLine(table[2].ToString());21 }22 }
运行结果为:
输出哈希表中所有键/值:
取出哈希表的键时,无法单独取出某一个或数个键,必须通过foreach循环发出全部键(注意:不是所有数据都能通过foreach循环打印,必须实现IEnumerable接
口才能使用foreach循环)。Keys是获取包含Hashtable中的键的ICollection。而Values是获取包含Hashtable中的值的ICollection。其中经过了两个上转型操作
View Code
1 usingSystem;2 usingSystem.Collections;3 public classTest4 {5 public static voidMain()6 {7 //创建哈希表
8 Hashtable table = newHashtable();9 //添加键值对
10 table.Add("one","宫崎骏");11 table.Add(2,"久石让");12 table.Add("三",DateTime.Now);13 //一.用ICollection接口接收键集合14 //ICollection collection = table.Keys;15 //二.用IEnumerable接口接收键集合16 //IEnumerable collection = table.Keys;17 //三.用var关键字接收集合
18 var collection =table.Keys;19 //foreach输出键
20 foreach(object o incollection)21 {22 Console.WriteLine(o);23 }24 //输出哈希表中值的集合(整体替换,和上方法单独再定义变量接收集合一样)
25 foreach(object o1 intable.Values)26 {27 Console.WriteLine(o1);28 }29 }30 }
运行结果为:
此过程为阐述IEnumerable接口:(作为上面案例的补充和说明)
c#中对 ICollection和 IEnumerable的关系说明……
public virtual ICollection Keys
{
get;
}
public interface ICollection : IEnumerable
{
…………
}
ICollection : IEnumerable中两次上转型操作过程类似与下面例子:
View Code
1 usingSystem;2 usingSystem.Collections;3 public classTest4 {5 public static voidMain()6 {7 IWuDang w =getWuDang();8 w.Sing();9 //二次上转型
10 IShaoLin s =getWuDang();11 s.Print();12 }13 public staticIWuDang getWuDang()14 {15 //上转型
16 return newA();17 }18 }19 public interfaceIShaoLin20 {21 voidPrint();22 }23 public interfaceIWuDang : IShaoLin24 {25 voidSing();26 }27 public classA : IWuDang28 {29 //必须实现接口中的所有方法
30 public voidSing()31 {32 Console.WriteLine("Sing");33 }34 public voidPrint()35 {36 Console.WriteLine("Print");37 }38 }
执行结果为:
二.链表:
最常用的链表要数List链表了,它表示可通过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序、和操作的方法。使用时要导入命名空间:using System.Collections.Generic List在c#中定义是:
public class List:IList,ICollection,IEnumerable,IList,ICollection,IEnumerable
表示List实现的接口。List的Add方法是将对象添加到List的结尾处。AddRange方法是指定集合的元素添加到List的末尾。Clear方法是从List中移除所有元素。Contains是确定某元素是否在List中。Remove方法是从List中移除特定对象的第一个匹配项。Count属性是获取List中实际包含的元素数。Item属性是获取或设置指定索引处得元素。RemoveAt方法是移除指定索引处得元素而RemoveRange方法是从List中移除一定范围的元素。List常用属性和方法在c#中定义有:public T Item[int index]{get;set;};public void Insert(int index,T item)其中index表示从零开始的索引,应该在此位置插入item。public void AddRange(IEnumerable collection)其中IEnumerable表示可以枚举的接口。public void RemoveAt(int index);public void RemoveRange(int index,int count);public bool Remove(T item);public bool Contains(T item)下面举一些简单例子予以形象说明……
View Code
1 usingSystem;2 usingSystem.Collections.Generic;3 public classTest4 {5 public static voidMain()6 {7 //这个链表的元素是string
8 List list = new List();9 list.Add("这是第一个元素");10 Console.WriteLine(list.Count);11 Console.WriteLine(list[0]);12 //list实现了IEnumerable接口,所以可以用AddRange方法添加list链表13 //创建一个list1
14 List list1 = new List();15 list1.Add("2list-1");16 list1.Add("2list-2");17 list1.Add("2list-3");18 list1.Add("2list-4");19 //用AddRange方法向list中添加一个list1集合
20 list.AddRange(list1);21 //Contains方法,返回值为布尔类型
22 Console.WriteLine(list.Count);23 if(list.Contains("2list-3"))24 {25 Console.WriteLine("存在一个2list-3元素");26 }27 else
28 {29 Console.WriteLine("不存在此元素");30 }31 //Insert方法,注意区分insert和add方法
32 list.Insert(3,"我是第四个元素");33 Console.WriteLine(list.Count);34 //Remove方法
35 list.Remove("我是第四个元素");36 Console.WriteLine(list.Count);37 //RemoveRange方法
38 list.RemoveRange(2,2);39 Console.WriteLine(list.Count);40 //Clear方法
41 list.Clear();42 Console.WriteLine(list.Count);43 }44 }
运行结果为:
下面是一段相关链接,帮组理解本节知识点:(运行结果略)
View Code
1 usingSystem;2 usingSystem.Collections.Generic;3 public classTest4 {5 public static voidMain()6 {7 List list = new List();8 //此过程经过了五次上转型操作
9 list.Add(new People(){Name = "one"});10 list.Add(new People(){Name = "two"});11 list.Add(new People(){Name = "three"});12 list.Add(new People(){Name = "four"});13 list.Add(new People(){Name = "five"});14 foreach(IWuDang wu inlist)15 {16 wu.GongFu();17 }18 }19 }20 public interfaceIWuDang21 {22 voidGongFu();23 }24 public classPeople:IWuDang25 {26 public stringName27 {28 get;set;29 }30 public voidPrint()31 {32 Console.WriteLine(this.Name);33 }34 public voidGongFu()35 {36 Console.WriteLine("太极拳");37 }38 }
泛型即泛指的类型,其类型可以是任意的,但确定后必须一致使用此类型,不得更换。例如 List中T可以是任意类型(只能确定的一种)这样可以减少装箱和拆箱操作,提高程序执行效率。把值类型的数据赋值给引用类型,叫装箱操作。把已经装箱过的引用类型强制转换为值类型叫拆箱操作。泛型类的构造函数内不含<>.下面是泛型类的定义:
View Code
1 usingSystem;2 usingSystem.Collections.Generic;3 public classTest4 {5 public static voidMain()6 {7 People p = new People();8 }9 }10 public class People
11 {12 publicT Age13 {14 get;set;15 }16 publicQ Name17 {18 get;set;19 }20 //构造函数
21 publicPeople()22 {23 }24 publicPeople(Q q,T t)25 {26 this.Name =q;27 this.Age =t;28 }29 }
三.ArrayList 数组链表:
非泛型集合,有下标。用 ArrayList list = new ArrayList() 来创建链表,方法、属性和用法与其他集合相同,但是ArrayList中Add方法在c#中的定义为public virtual int Add(object value)
四.Dictionary 集合:
Dictionary Dictionary d = new Dictionary();参看Hashtable操作,用法一样。