第6章 集合、索引器与泛型
2016-05-26 15:19
218 查看
第6章 集合、索引器与泛型
6.1 集合
6.1.1 集合概述
6.1.2 ArrayList
ArrayList
动态数组,不限制元素个数和数据类型
1.
ArrayList初始化
ArrayList a = new ArrayList();
2. 向
ArrayList中添加元素
ArrayList a = new ArrayList(); Student stu = new Student("令狐冲", 21); a.Add(stu);
3. 访问
ArrayList中的元素
访问
ArrayList中的元素时必须进行拆箱操作,即强制转换。
Student x = (Student)a[0]; x.ShowMsg();
4. 删除
ArrayList中的元素
void Remove(Object obj) void RemoveAt(int index) void Clear()
5. 向
ArrayList中插入元素
void Insert(int index, Object value)
6. 遍历
ArrayList中的元素
for(int i = 0; i < a.Count; i++) { Student x = (Student)a[i]; // }
foreach(object x in a) { Student s = (Student)x; // }
6.1.3 哈希表HashTable
HashTable 哈希表名 = new HashTable([哈希表长度], [增长因子]); // 增长因子表示每调整一次增加容量多少倍 HashTable a = new HashTable(); Student x = new Student("令狐冲", 1001); a.Add(1001, stu);
遍历哈希表时既可以遍历其键集,也可以遍历其值集。
foreach(object s_no in a.Keys) { int i = (int)s_no; Student x = (Student)a[i]; }
6.1.4 栈和队列
1. 栈
Stack
Stack s = new Stack(); s.Push("令狐冲"); s.Pop();
2. 队列
Queue
Queue q = new Queue(); q.Enqueue("令狐冲");
6.2 索引器
一个相册对象a[
Album的实例]包含多张照片[即
Photo类的实例],所有照片存放在一个
photos数组之中,此时访问第
i张照片的一般形式是
a.photos[i],但若能用一个索引
i直接访问相册[即
a[i]获得第
i张照片],那么程序看起来更为直观,更容易编写。
6.2.1 索引器的定义
[修饰符] 数据类型 this[索引类型 index] { get { } set {} }
动手做一下
class Photo
class Photo { string _title; public Photo(string title) { this._title = title; } public string Title { get{return _title;} } }
class Album
class Album { private Photo[] photos; public Album(int capacity) { photos = new Photo[capacity]; } // **重点** public Photo this[int index] { get { if(index < 0 || index > photos.Lenght) return null; return photos[index]; } set { if(index < 0 || index >= photos.Lenght) return; photos[index] = value; } } }
6.2.2 索引器的使用
a[0] = new Photo("张三丰的照片"); // 等价于 a.photos[0] = new Photo("张三丰的照片");
6.2.3 索引器的重载
public Photo this[string title] { get { foreach(Photo p in photos) { if(p.Title.IndexOf(title) != -1) return p; } return null; } }
6.2.4 接口中的索引器
接口中索引器不使用修饰符接口中索引器只包含访问器
get或
set,没有实现语句
6.2.5 索引器与属性的比较
属性 | 索引器 |
---|---|
允许调用方法,如同公共数据成员 | 允许调用对象上的方法,如同对象是一个数组 |
可以通过简单的名称进行访问 | 可通过索引器进行访问 |
可以为静态成员或实例成员 | 必须为实例成员 |
其get访问器没有参数 | 其get访问器具有与索引器相同的形参表 |
其set访问器包含隐式value参数 | 除了value参数外,其set访问器还具有与索引器相同的形参表 |
6.3 泛型
6.3.1 泛型概述
6.3.2 泛型集合
1. List
List <Student> list = new List <Student>();
2. Dictionary
Dictionary <int, Student> dic = new Dictionary<int, Student>();
6.3.3 自定义泛型
1. 泛型类
类型参数的名称必须遵循
C#的命名规则,常用
K,
V和
T等字母表示。
public class Person <T> { } public class Person<T1, T2, T3> { }
2. *泛型方法
// 声明 void Swap<T>(ref T x, ref T y) {} // 调用 void Swap<int>(ref a, ref b)
3. 泛型接口
interface IDate<T> {}
6.3.4 泛型的高级应用
1. 约束泛型类的类型参数
在定义泛型类是,有时需要指定只有某种类型的对象或从这个类型派生的对象可被用作类型参数。这时,可以使用
where关键字约束类型参数。
public class Animal {} public class Pet<T> where T: Animal {}
2. 泛型类的继承性
如果基泛型类在定义时指定了约束,则从它派生的类型也将受到约束。
public class MyPet<T>: Pet<T> where T: Dog {}
相关文章推荐
- android studio发布公共类库到服务器maven仓库
- ht 4000 ml速查标签
- iOS开发之ImageView复用实现图片无限轮播
- jsp中绝对路径显示图片
- 日期工具类
- Java 如何判断线程池所有任务是否执行完毕
- C++实践参考:数组类模板
- Js内存泄露问题总结
- php打印数组 --- 打印出漂亮格式的数组
- 中英文字符 对齐
- centos kvm
- Linux学习:netstat命令
- 关于Activity生命周期的一些笔记
- python数据分析
- Vim简明教程【CoolShell】
- ViewPager与RadioGroup制作导航栏
- android双缓冲应用
- 运维自动化之centos6.5 yum安装salt
- ROS的节点(NODE)
- Android之SurfaceView