c#集合

153 阅读4分钟

集合

本文章主要介绍两种集合:(1)ArrayList;(2)Hashtable;

一、ArrayList

ArrayList类似于数组,其大小可根据需要动态改变,也可称之为动态数组。

有如下学生类:

class Student
{
    public Student(string code,string name,string address)
    {
        this.Code = code;
        this.Name = name;
        this.Address = address;
    }
    public string Code { get; set; } //学号
    public string Name { get; set; } //姓名
    public string Address { get; set; }  //宿舍地址
}

在main方法中实例化了6个学生对象:

Student s1 = new Student("001","郑楷", "802");
Student s2 = new Student("002","周金辉", "803");
Student s3 = new Student("003","钱章", "804");
Student s4 = new Student("004","吕源勋", "805");
Student s5 = new Student("005","刘权", "806");
Student s6 = new Student("006","徐帅", "807");

集合的定义:

ArrayList list = new ArrayList();

集合中添加元素:

list.Add(s1);
list.Add(s2);
list.Add(s3);
list.Add(s4);
list.Add(s5);
list.Add(s6);

获取集合元素个数:

Console.Write(list.Count);

循环遍历集合中的元素:

foreach (Student item in list)
{
	Console.Write(item.Name + "  ");
}

for (int i = 0; i < list.Count; i++)
{
    Student stu = (Student)list[i];
    Console.Write(stu.Name + " ");
}

集合中删除元素:

list.Remove(s5);  //根据对象删除
list.Remove(4);   //根据下标索引删除

集合的插入:

Student s7 = new Student("007","张劲飞", "801");
list.Insert(2, s7);  //向索引编号为2的元素前面插入

集合元素的清除:

list.Clear();

集合的更多属性和方法,可查看如下表格:

下表列出了 ArrayList 类的一些常用的 属性

属性描述
Capacity获取或设置 ArrayList 可以包含的元素个数。
Count获取 ArrayList 中实际包含的元素个数。
IsFixedSize获取一个值,表示 ArrayList 是否具有固定大小。
IsReadOnly获取一个值,表示 ArrayList 是否只读。
IsSynchronized获取一个值,表示访问 ArrayList 是否同步(线程安全)。
Item[Int32]获取或设置指定索引处的元素。
SyncRoot获取一个对象用于同步访问 ArrayList。

下表列出了 ArrayList 类的一些常用的 方法

序号方法名 & 描述
1public virtual int Add( object value ); 在 ArrayList 的末尾添加一个对象。
2public virtual void AddRange( ICollection c ); 在 ArrayList 的末尾添加 ICollection 的元素。
3public virtual void Clear(); 从 ArrayList 中移除所有的元素。
4public virtual bool Contains( object item ); 判断某个元素是否在 ArrayList 中。
5public virtual ArrayList GetRange( int index, int count ); 返回一个 ArrayList,表示源 ArrayList 中元素的子集。
6public virtual int IndexOf(object); 返回某个值在 ArrayList 中第一次出现的索引,索引从零开始。
7public virtual void Insert( int index, object value ); 在 ArrayList 的指定索引处,插入一个元素。
8public virtual void InsertRange( int index, ICollection c ); 在 ArrayList 的指定索引处,插入某个集合的元素。
9public virtual void Remove( object obj ); 从 ArrayList 中移除第一次出现的指定对象。
10public virtual void RemoveAt( int index ); 移除 ArrayList 的指定索引处的元素。
11public virtual void RemoveRange( int index, int count ); 从 ArrayList 中移除某个范围的元素。
12public virtual void Reverse(); 逆转 ArrayList 中元素的顺序。
13public virtual void SetRange( int index, ICollection c ); 复制某个集合的元素到 ArrayList 中某个范围的元素上。
14public virtual void Sort(); 对 ArrayList 中的元素进行排序。
15public virtual void TrimToSize(); 设置容量为 ArrayList 中元素的实际个数。

二、Hashtable

用于处理key/value(键/值)对的集合容器。

key通常用于快速查找,value用于存储对应于key的值。

有如下学生类:

class Student
{
    public Student(string code,string name,string address)
    {
        this.Code = code;
        this.Name = name;
        this.Address = address;
    }
    public string Code { get; set; } //学号
    public string Name { get; set; } //姓名
    public string Address { get; set; }  //宿舍地址
}

在main方法中实例化了6个学生对象:

Student s1 = new Student("001","郑楷", "802");
Student s2 = new Student("002","周金辉", "803");
Student s3 = new Student("003","钱章", "804");
Student s4 = new Student("004","吕源勋", "805");
Student s5 = new Student("005","刘权", "806");
Student s6 = new Student("006","徐帅", "807");

集合的定义:

Hashtable list = new Hashtable();

集合中添加元素:

list.Add(s1.Code,s1);
list.Add(s2.Code, s2);
list.Add(s3.Code, s3);
list.Add(s4.Code, s4);
list.Add(s5.Code, s5);
list.Add(s6.Code, s6);

获取集合元素个数:

Console.Write(list.Count);

循环遍历集合中的元素:

//list.Values代表值集合,list.Keys代表键集合
foreach (Student item in list.Values)
{
	Console.Write(item.Name + "  ");
}

集合中删除元素:

list.Remove("005");   //根据键删除元素

集合元素的清除:

list.Clear();

集合的更多属性和方法,可查看如下表格:

下表列出了 Hashtable 类的一些常用的 属性

属性描述
Count获取 Hashtable 中包含的键值对个数。
IsFixedSize获取一个值,表示 Hashtable 是否具有固定大小。
IsReadOnly获取一个值,表示 Hashtable 是否只读。
Item获取或设置与指定的键相关的值。
Keys获取一个 ICollection,包含 Hashtable 中的键。
Values获取一个 ICollection,包含 Hashtable 中的值。

下表列出了 Hashtable 类的一些常用的 方法

序号方法名 & 描述
1public virtual void Add( object key, object value ); 向 Hashtable 添加一个带有指定的键和值的元素。
2public virtual void Clear(); 从 Hashtable 中移除所有的元素。
3public virtual bool ContainsKey( object key ); 判断 Hashtable 是否包含指定的键。
4public virtual bool ContainsValue( object value ); 判断 Hashtable 是否包含指定的值。
5public virtual void Remove( object key ); 从 Hashtable 中移除带有指定的键的元素。