第6章 集合、索引器与泛型

第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 接口中的索引器

  • 接口中索引器不使用修饰符
  • 接口中索引器只包含访问器getset,没有实现语句

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,VT等字母表示。

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
{}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值