黑马程序员_ArrayList、Hashtable、List、Dictionary的区别与应用

集合

ArrayList 

相比数组而言,它可以存储任何数据类型,且增删改查比数组要便捷;但是存储时使用的是object类型,赋值时要将类型转为object类型存储,使用时要从object类型转换出来,所以效率一般。

注意:使用前需要添加引用。using System.Collections;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 
 7 namespace 演示_集合
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int[] nums = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6 };
14             ArrayList al = new ArrayList();
15             //将nums数组加入到al集合中。
16             al.AddRange(nums);
17 
18             //将10加入到al集合中。
19             al.Add(10);
20 
21             //删除第一个值为3的成员,如果找不到,不报错。
22             al.Remove(3);
23 
24             //删除序号为5的成员,如果找不到,报错。
25             al.RemoveAt(5);
26 
27             //升序排序,Reverse降序排序。
28             al.Sort();
29 
30             //判断是否包含值为1的元素
31             if (al.Contains(1))
32             {
33                 Console.WriteLine("包含1");
34             }
35             else
36             {
37                 Console.WriteLine("不包含1");
38             }
39 
40             //在指定位置3插入值6
41             al.Insert(3, 6);
42 
43             //将集合转化为数组int 为数组中的元素类型,int[]为返回后的数组类型。
44             int[] num2 = (int[])al.ToArray(typeof(int));
45 
46             //清空集合。
47             al.Clear();
48 
49             //集合也可以通过下标来访问,叫做索引。
50             
51             Console.ReadKey(true);
52         }
53     }
54 }
ArrayList演示
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 
 7 namespace 手写集合
 8 {
 9     class Program
10     {
11         class MyCollection
12         {
13             ArrayList al;
14             public MyCollection()
15             {
16                 al = new ArrayList();
17             }
18             public void Add(object o)
19             {
20                 al.Add(o);
21                 //return ;
22             }
23             public void AddRange(ICollection ic)
24             {
25                 al.AddRange(ic);
26             }
27             public void Remove(object o)
28             {
29                 al.Remove(o);
30             }
31             public void RemoveAt(int i)
32             {
33                 al.RemoveAt(i);
34             }
35             public void Clear()
36             {
37                 al.Clear();
38             }
39             public void Insert(int i, object o)
40             {
41                 al.Insert(i, o);
42             }
43 
44             public object this[int i]
45             {
46                 //public int age,这是以前定义属性的方法,在这里,age就相当于[int i]!!!!!
47                 //注意,这里的定义索引相当于一个用[]访问的特殊的属性
48                 //this在类的内部,表示当前实例。
49                 get { return al[i]; }
50                 //set { al[i] = value; }
51             }
52         }
53         static void Main(string[] args)
54         {
55             MyCollection mc = new MyCollection();
56             int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
57             mc.AddRange(nums);
58             mc.Add(100);
59             Console.WriteLine(mc[3]);
60             Console.ReadKey(true);
61         }
62     }
63 }
ArrayList手写
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 
 7 namespace 人类集合
 8 {
 9     class Person
10     {
11         private string name;
12         private char sex;
13         private int age;
14         public Person(string n, char c, int i)
15         {
16             name = n;
17             sex = c;
18             age = i;
19         }
20         public void SayHello()
21         {
22             Console.WriteLine("我是{0},{1}生,今年{2}岁。", name, sex, age);
23         }
24     }
25     class Program
26     {
27         static void Main(string[] args)
28         {
29             Persons ps = new Persons();
30             ps.Add(new Person("张三", '', 18));
31             ps.Add(new Person("李四", '', 20));
32             ps.Add(new Person("小兰", '', 19));
33             for (int i = 0; i < ps.Count; i++)
34             {
35                 ps[i].SayHello();
36             }
37             Console.ReadKey(true);
38         }
39     }
40     class Persons
41     {
42         ArrayList al;
43         public Persons()
44         {
45             al = new ArrayList();
46         }
47         public void Add(object o)              //视频上是这样定义的public int Add(object o),我觉得没有必要,add方法不需要返回类型。
48         {                                      //这里后台自动转成object类型,使用的时候再转换回来,会浪费一定时间,降低效率。
49             al.Add(o);
50         }
51         public int Count
52         {
53             get { return al.Count; }
54         }
55         public Person this[int i]
56         {
57             get { return (Person)al[i]; }
58             //set { al[i] = value; }
59         }
60     }
61 }
用ArrayList为类型添加索引 

Hashtable

和AllayList相比不同的是,存储一个键值对照表,一般用来根据键名获取值,但是要注意的是,它的键名不能重复!

注意:使用前需要添加引用。using System.Collections; 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 
 7 namespace Hashtable的使用
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Hashtable ht = new Hashtable();
14             ht.Add("张三", "123456");
15             ht.Add("李四", "010-12345678");
16             ht.Add("小兰", "13888888888");
17 
18             //判断指定键是否存在
19             if (ht.ContainsKey("张三1"))
20             {
21                 Console.WriteLine(ht["张三"]);
22             }
23             else
24             {
25                 Console.WriteLine("不存在");
26             }
27 
28             //遍历输出所有键值对
29             foreach (DictionaryEntry temp in ht)
30             {
31                 Console.WriteLine(temp.Key + "	" + temp.Value);
32             }
33 
34             //遍历输出所有键
35             foreach (string  temp in ht.Keys)
36             {
37                 Console.WriteLine(temp);
38             }
39 
40             //Hashtable的键不允许重复
41             //ht.Add("小兰", "13888888888");
42             Console.ReadKey(true);
43         }
44     }
45 }
Hashtable的使用 

泛型集合

List

泛型,就是类型不确定,但是,在声明之后,类型就确定下来了,所以效率要远高于ArrayList,因为它赋值与使用的时候不需要和object类型相互转换。 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 using System.Diagnostics;
 7 
 8 namespace 泛型集合List                      //《泛型》的概念就是集合类型不确定!
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             ArrayList al = new ArrayList();
15             //每次处理过程都需要强转
16 
17             List<int> lInt = new List<int>();
18             //内部实现,就是类型决定,不需要强转,所以效率高!
19 
20             Stopwatch sw1 = new Stopwatch();
21             Stopwatch sw2 = new Stopwatch();
22 
23             //将1000000数字加到集合中。
24             sw1.Start();
25             for (int i = 0; i < 1000000; i++)
26             {
27                 al.Add(i);
28             }
29             sw1.Stop();
30             sw2.Start();
31             for (int i = 0; i < 1000000; i++)
32             {
33                 lInt.Add(i);
34             }
35             sw2.Stop();
36             Console.WriteLine(sw1.Elapsed);
37             Console.WriteLine(sw2.Elapsed);
38             Console.ReadKey(true);
39         }
40     }
41 }
List效率测试 

Dictionary

表示键和值的集合。Dictionary与Hashtable功能一样,但它和List一样,也是声明之后类型就确定下来,所以Dictionary的性能优于Hashtable。 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 泛型集合Dictionary
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Dictionary<string, string> dir = new Dictionary<string, string>();
13             dir.Add("张三", "123456");
14             dir.Add("李四", "010-12345678");
15             dir.Add("小兰", "13888888888");
16             foreach (KeyValuePair<string, string> temp in dir)                  //注意这里的类型为KeyValuePair
17             {
18                 Console.WriteLine(temp.Key + "	" + temp.Value);
19             }
20             Console.ReadKey(true);
21         }
22     }
23 }
Dictionary 注意类型 
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 
 7 namespace 金山词霸
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {   //导入词库
13             string[] words = File.ReadAllLines(@"dic.txt", Encoding.Default);
14 
15             //声明集合
16             Dictionary<string, string> dic = new Dictionary<string, string>();
17 
18             //集合导入词库
19             for (int i = 0; i < words.Length; i++)
20             {
21                 string[] temp = words[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
22                 if (!dic.ContainsKey(temp[0]))
23                 {
24                     dic.Add(temp[0], temp[1]);
25                 }
26                 else
27                 {
28                     dic[temp[0]] += "
" + temp[1];
29                 }
30             }
31 
32             //输入输出,连输入qqq退出。
33             while (true)
34             {
35                 Console.WriteLine("请输入您要查询的单词:");
36                 string str = Console.ReadLine().ToLower();
37                 if (str == "qqq")
38                 {
39                     Console.WriteLine("查词结束,按任意键退出。");
40                     Console.ReadKey(true);
41                     break;
42                 }
43                 else if (!dic.ContainsKey(str))
44                 {
45                     Console.WriteLine("对不起,词库中没有您需要查询的单词。");
46                     Console.ReadKey(true);
47                     Console.Clear();
48                     continue;
49                 }
50                 else
51                 {
52                     Console.WriteLine(dic[str]);
53                     Console.ReadKey(true);
54                     Console.Clear();
55                 }
56             }
57         }
58     }
59 }
金山词霸的例子 
原文地址:https://www.cnblogs.com/dlwcg/p/3612746.html