(2)ArrayList HashTable List Dictionary
ArratLis集合对象
需引入命名空间System.Collections
初始化
ArrayList Objs = new ArrayList(n);//n为可选,可指定长度
增加元素
objArrayList.Add(Obj实例名);
访问
(类型) Objs[index] //按指定索引(下标)取得对象
删除
ArrayList.Remove(Obj实例名) //删除指定对象名的对象
ArrayList.RemoveAt(index) //删除指定索引的对象
ArrayList.Clear() //清除集合内的所有元素
遍历读取时需要强制转换
Hashtable集合对象
初始化
Hashtable _hashtable = new Hashtable();
增加元素
students.Add(Key , Value);
访问
(类型)_hashtable[Key];
删除
_hashtable.Remove(Key);
遍历读取时需要强制转换
List<T>
Exp:
List<Student> students = new List<Student>();
增加时强类型检查 增加元素方法与ListArray相同 通过索引删除
Dictionary<K,V>
Exp:
Dictionary<string,Student> students = new Dictionary<string,Student>();
1 public class Student
2 {
3 public Student(String name)
4 :this(name, 10){ }
5
6 public Student(String name, int age)
7 {
8 _name = name;
9 _age = age;
10 }
11 //学生姓名
12 private String _name;
13 public String Name
14 {
15 get { return _name;}
16 set { _name = value;}
17 }
18 //学生年纪
19 private int _age;
20 public int Age
21 {
22 get { return _age; }
23 set { _age = value; }
24 }
25 }
26
27 public class Teacher
28 {
29 public Teacher(String name)
30 :this(name, 10){ }
31
32 public Teacher(String name, int age)
33 {
34 _name = name;
35 _age = age;
36 }
37 //学生姓名
38 private String _name;
39 public String Name
40 {
41 get { return _name;}
42 set { _name = value;}
43 }
44 //学生年纪
45 private int _age;
46 public int Age
47 {
48 get { return _age; }
49 set { _age = value; }
50 }
51 }
52
53 class Program
54 {
55 static void Main(string[] args)
56 {
57 Student stu1 = new Student("zhang", 18);
58 Student stu2 = new Student("Li", 19);
59 // Teacher tea1 = new Teacher("KK");
60
61 //ArrayList
62 ArrayList students = new ArrayList();
63 students.Add(stu1);
64 students.Add(stu2);
65 // students.Add(tea1);
66 Console.WriteLine("有学生人数: {0}",students.Count);
67 foreach (Object s in students)
68 {
69 Student val = (Student)s;
70 Console.WriteLine(val.Name);
71 }
72
73 //List<T>
74 List<Student> students2 = new List<Student>();
75 // students2.Add(tea1);
76 students2.Add(stu1);
77 students2.Add(stu2);
78 Console.WriteLine("有学生人数: {0}", students2.Count);
79 foreach (Student s in students2)
80 {
81 Console.WriteLine(s.Name);
82 }
83
84 //Hashtable
85 Hashtable hashStudent = new Hashtable();
86 hashStudent.Add(1, stu1);
87 hashStudent.Add(2, stu2);
88 Console.WriteLine("有学生人数: {0}", hashStudent.Count);
89 foreach (Object s in hashStudent.Values)
90 {
91 Student val2 = (Student)s;
92 Console.WriteLine(val2.Name);
93 }
94 Student objStu = (Student)hashStudent[1];
95 Console.WriteLine(objStu.Name);
96
97 //Dictionary<K, V>
98 Dictionary<int, Student> dicStudent = new Dictionary<int, Student>();
99 dicStudent.Add(1, stu1);
100 dicStudent.Add(2, stu2);
101 Console.WriteLine("有学生人数: {0}", hashStudent.Count);
102 foreach (Student s in dicStudent.Values)
103 {
104 Console.WriteLine(s.Name);
105 }
106 Console.WriteLine(dicStudent[2].Name);
107 }
2 {
3 public Student(String name)
4 :this(name, 10){ }
5
6 public Student(String name, int age)
7 {
8 _name = name;
9 _age = age;
10 }
11 //学生姓名
12 private String _name;
13 public String Name
14 {
15 get { return _name;}
16 set { _name = value;}
17 }
18 //学生年纪
19 private int _age;
20 public int Age
21 {
22 get { return _age; }
23 set { _age = value; }
24 }
25 }
26
27 public class Teacher
28 {
29 public Teacher(String name)
30 :this(name, 10){ }
31
32 public Teacher(String name, int age)
33 {
34 _name = name;
35 _age = age;
36 }
37 //学生姓名
38 private String _name;
39 public String Name
40 {
41 get { return _name;}
42 set { _name = value;}
43 }
44 //学生年纪
45 private int _age;
46 public int Age
47 {
48 get { return _age; }
49 set { _age = value; }
50 }
51 }
52
53 class Program
54 {
55 static void Main(string[] args)
56 {
57 Student stu1 = new Student("zhang", 18);
58 Student stu2 = new Student("Li", 19);
59 // Teacher tea1 = new Teacher("KK");
60
61 //ArrayList
62 ArrayList students = new ArrayList();
63 students.Add(stu1);
64 students.Add(stu2);
65 // students.Add(tea1);
66 Console.WriteLine("有学生人数: {0}",students.Count);
67 foreach (Object s in students)
68 {
69 Student val = (Student)s;
70 Console.WriteLine(val.Name);
71 }
72
73 //List<T>
74 List<Student> students2 = new List<Student>();
75 // students2.Add(tea1);
76 students2.Add(stu1);
77 students2.Add(stu2);
78 Console.WriteLine("有学生人数: {0}", students2.Count);
79 foreach (Student s in students2)
80 {
81 Console.WriteLine(s.Name);
82 }
83
84 //Hashtable
85 Hashtable hashStudent = new Hashtable();
86 hashStudent.Add(1, stu1);
87 hashStudent.Add(2, stu2);
88 Console.WriteLine("有学生人数: {0}", hashStudent.Count);
89 foreach (Object s in hashStudent.Values)
90 {
91 Student val2 = (Student)s;
92 Console.WriteLine(val2.Name);
93 }
94 Student objStu = (Student)hashStudent[1];
95 Console.WriteLine(objStu.Name);
96
97 //Dictionary<K, V>
98 Dictionary<int, Student> dicStudent = new Dictionary<int, Student>();
99 dicStudent.Add(1, stu1);
100 dicStudent.Add(2, stu2);
101 Console.WriteLine("有学生人数: {0}", hashStudent.Count);
102 foreach (Student s in dicStudent.Values)
103 {
104 Console.WriteLine(s.Name);
105 }
106 Console.WriteLine(dicStudent[2].Name);
107 }
108 }