您的位置:首页 > 其它

第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
{}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: