2016-03-21集合泛型集合、队列和栈
来源:互联网 发布:缎面连衣裙淘宝 编辑:程序博客网 时间:2024/06/06 14:09
集合
ArrayList
ArrayList 是一个可以动态维护长度的集合
其存储结构可表示为:
索引(下标) 值
0 obj1
1 obj2
2 obj3
…… ……
使用
- 引入System.Collections命名空间
- 实例化一个对象
ArrayList 名称 = new ArrayList()
示例:
ArrayList students = new ArrayList(); students.Add(s1); students.Add(s2); students.Add(s3); students.Add(s4); Console.WriteLine("\n这些是使用集合ArrayList存储输出的"); foreach (Student s in students) { s.SayHi(); }//也可以放着循环中,通过students[i]来访问
Hashtable 哈希表
结构如下:
//Hashtable存储信息 Hashtable teachers = new Hashtable(); teachers.Add(t1.Name, t1); teachers.Add(t2.Name, t2); teachers.Add(t3.Name, t3); Console.WriteLine("\n这些是用哈希表存储输出的:"); foreach (string str in teachers.Keys) { Teacher s = (Teacher)teachers[str]; s.SayHi(); }
List
- 引入命名空间
System.Collections.Generic - 使用示例:
Console.WriteLine("\n这些是用泛型集合List输出的"); List<Student> listStudent = new List<Student>(); listStudent.Add(s1); listStudent.Add(s2); listStudent.Add(s3); listStudent.Add(s4); for (int i = 0; i < listStudent.Count; i++) { listStudent[i].SayHi(); }
- 说明
List<类> listStudent = new List<类名>() 之后就只能存储这个类实例化出的对象,不能存储其他类型。
Dictionary<>
Dictionary
Console.WriteLine("\n 这些是用Dictionary存储输出的:"); Dictionary<int, Teacher> dicTeacher = new Dictionary<int, Teacher>(); dicTeacher.Add(1, t1); dicTeacher.Add(2, t2); dicTeacher.Add(3, t3); foreach (Teacher t in dicTeacher.Values) { t.SayHi(); }
队列和栈
栈 Stack与Stack
1.入栈Push
2.出栈PopQueue与Queue
1.入队Enqueue
2.出队Dequeue
//栈 Stack与Stack<T> Console.WriteLine("\n\n这是存储到栈再输出的结果"); Stack<Student> s0 = new Stack<Student>(); for (int i = 0; i < 4; i++) { s0.Push(listStudent[i]);//进栈 } int scount = s0.Count; for (int i = 0; i < scount; i++) { s0.Pop().SayHi();//出栈 } //队列 Queue与Queue<T> Console.WriteLine(""); Queue<Teacher> queT = new Queue<Teacher>(); queT.Enqueue(t1); queT.Enqueue(t2); queT.Enqueue(t3);//进入队列 for (int i = 0; i < 3; i++) { queT.Dequeue().SayHi(); }
0 0
- 2016-03-21集合泛型集合、队列和栈
- 非泛型集合,泛型集合,栈,队列
- 有关集合队列和栈的转换
- Java笔记集合之栈和队列
- JACK_C#_集合之栈和队列
- 栈,队列 集合!
- 集合和泛型2-----集合1
- 泛型集合和非泛型集合
- 集合和泛型
- 集合和泛型
- 集合和泛型
- 集合和泛型
- 集合和泛型
- 集合和泛型
- 用集合做队列、栈
- java中LinkedList 集合类实现栈和队列
- java中LinkedList 集合类实现栈和队列
- java中LinkedList 集合类实现栈和队列
- Java的多线程机制系列:不得不提的volatile及指令重排序
- Red Hat 6.5 配置 CentOS 6.5 Yum 源
- 个人所得税计算
- 对三层架构的浅层理解
- 2016-03-21文件操作
- 2016-03-21集合泛型集合、队列和栈
- ntohs, ntohl, htons,htonl, inet_addr,inet_ntoa区别
- python操作sqlalchemy表间关系
- jquery.scrollLoading.js内容(异步加载图片)
- 2016-03-20枚举、结构、索引器值类型、引用类型
- Red Hat 6.5-32 配置使用CentOS 6.5-32 Yum 源
- 字符串处理函数
- 2016-03-19前三天的笔记
- 通过HashMap快速将叶子节点构建成树状结构