.Net Framework 4/C# 集合和索引器

发布于:2025-06-05 ⋅ 阅读:(21) ⋅ 点赞:(0)

一、ArrayList 类(集合)

        ArrayList 类位于 System.Collections 命名空间下,它可以动态地添加和删除元素。

        ArrayList 提供了3个构造器,通过这3个构造器可以有3种声明方式:

  • 默认构造器,将会以默认(16)的大小初始化内部的数组,构造器格式如下:
public ArrayList();

ArrayList List = new ArrayList();

用 Add 方法添加元素:
List.Add(10);
  • 用一个 ICollection 对象来构造,并将该集合的元素添加到 ArrayList 中,构造器格式如下:

public ArrayList(ICollection);

ArrayList List = new ArrayList(arrayName);

int[] arr = new int[]{1,2,3,4,5};
ArrayList List = new ArrayList(arr);
  • 用指定的大小初始化内部的数组,构造器格式如下:

public ArrayList(int);

ArrayList List = new ArrayList(n);

ArrayList List = new ArrayList(10);

(一)ArrayList 元素的添加

        向 ArrayList 集合中添加元素时,可以使用 ArrayList 类提供的 Add 方法和 Insert 方法。

  1. Add:将对象添加到 ArrayList 集合的结尾处,例如:List.Add(7);
  2. InsertRange:在指定的位置插入一个集合,例如:List.InsertRange(0,new int[]{1,2});
  3. Insert:将元素插入到 ArrayList 集合的指定索引处,例如:List.Insert(3,7);

        需要注意的是,如果将一个对象(类对象、数组、LIst 本身)用 Add 方法添加到 ArrayList 集合中,输出的是这个对象所在的类的命名空间。所以,ArrayList 为了解决这个问题,提供了一个 AddRange 方法,例如:List.AddRange(new int[]{1, 2, 3, 4, 5, 6, 7, 8});

(二)ArrayList 元素的删除

        在 ArrayList 集合中删除元素时,可以使用 ArrayList 类提供的 Clear 方法、Remove 方法、RemoveAt 方法和 RemoveRange 方法。

  1. Clear 方法:从 ArrayList 中移除所有的元素,例如有:List.Clear();
  2. RemoveRange 方法:从 ArrayList 中移除一定范围的元素,例如有:List.RemoveRange(1,3);
  3. RemoveAt 方法:从 ArrayList 中移除指定索引处的元素,例如有:List.RemoveAt(3);
  4. Remove 方法:从 ArrayList 中移除特定对象的第一个匹配项,例如有:List.Remove(3);

(三) ArrayList 元素的反转

        在 ArrayList 反转元素时,可以使用 ArrayList 类提供的 Reverse 方法。即将元素从后往前输出: List.Reverse();

(四)ArrayList 元素的查找

        查找 ArrayList 集合中的元素时,可以使用 ArrayList 类提供的 Contains 方法、IndexOf 方法和 LastIndexOf 方法。IndexOf 方法和 LastIndexOf 方法的用法与 string 字符串类的同名方法的用法基本相同。

        Contains 方法用来确定某元素是否在 ArrayList 集合中,其语法格式如下:

public virtual bool Contains(Objcect item)

        item:要在 ArrayList 中查找的 Object,该值可以为空引用;

        返回值:如果在 ArrayList 中找到 item,则为 true,否则为 false。

        例如有:

int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new ArrayList(arr);
List.Contains(2);

(五)ArrayList 的遍历—— foreach 语句

        foreach 语句用于枚举一个集合的元素,并对该集合中的每个元素执行一次嵌入语句,但是,foreach 语句不应用于更改集合内容,以避免产生不可预知的错误。foreach 语句的基本格式如下:

foreach([类型] [迭代变量名] in [集合类型表达式])
{
    语句块
}

        例如有:

ArrayList alt = new ArrayList();
alt.Add("1");
alt.Add("2");
alt.Add("3");
alt.Add("4");
alt.Add("5");
foreach(string alt2 in alt)
{
    Console.WriteLine(alt2);
}

        在 foreach 的表达式中,类型可以用一个 var 来表示,它可以根据值来推断出值的类型的关键字。

(六)ArrayList 类的长度

        ArrayList 集合有两个表示长度的属性,分别是 count 和 capcity,二者的区别就是 count 表示这个集合中实际包含的元素的个数,而 capcity 表示这个集合中可以包含的元素的个数。

二、Hashtable(哈希表/集合) 

        Hashtable 哈希表它表示键/值对的集合,这些键/值对根据键的哈希代码进行组织,它的每一个元素都是一个存储在 DictionaryEntry 对象中的键/值对。键不能为空引用,但值可以。

        Hashtable 的构造函数有很多种,这里介绍常用的两种: 

  • 使用默认的初始容量、加载因子、哈希代码提供程序和比较器来初始化 Hashtable 类的新的空实例,语法如下:
public Hashtable()
  • 使用指定的初始容量、默认加载因子、默认哈希代码提供程序和默认比较器来初始化 Hashtable 类的新的空实例,语法如下:
public Hashtable(int capacity)

         capacity:Hahtable 对象最初可包含的元素的近似数目

(一)Hashtable 元素的添加

        向 Hashtable 中添加元素时,可以使用 Hahtable 类提供的 Add 方法,Add 方法用来将带有指定键和值的元素添加到 Hashtable 中,其语法格式如下:

public virtual void Add(Object key, Object value)

key:要添加的元素的键
value:要添加的元素的值,该值可以为空引用

        如果指定了 Hashtable 的初始容量,则不用限定向 Hashtable 对象中添加的因子个数,容量会根据加载的因子自动增加。

        需要注意的是,用 Add 方法添加元素时,键是唯一的,不能重复出现,而值可以。同时,可以采用下标的形式添加元素,此时,若哈希表集合中已存在对应下标的键时,新的键对应的值会覆盖旧的值,若不存在对应下标的键时,才会在集合中添加新的元素。例如有:

Hashtable ht = new Hashtable();
ht.Add(1, "张三");
ht.Add(2, "李四");

ht.Add(1, "小美");			//添加失败,键 1 已存在

采用下标的形式添加时:
ht[3] = "王五";				//会添加新的元素
ht[1] = "小帅";				//会覆盖原先键 1 的值,也就是覆盖掉“张三”

(二)Hashtable 元素的删除

        在 Hashtable 中删除元素时,可以使用 Hashtable 类提供的 Clear 方法和 Remove 方法。

        1、Clear 方法

        该方法用来从 Hashtable 中移除所有元素,其语法格式:

public virtual void Clear()

        2、Remove 方法

        该方法用来从 Hashtable 中移除带有指定键的元素,其格式如下:

public virtual void Remove(Object key)

        key:要移除的元素的键

        例如有:

Hashtable hashtable = new Hashtable();

hashtable.Add("sex","男");
hashtable.Remove("sex");

(三)Hashtable 的遍历

        Hashtable 的遍历与数组类似,都可以使用 foreach 语句,但由于 Hashtable 中的元素是一个键/值对,因此需要使用 DictionaryEntry 类型来进行遍历,DictionaryEntry 类型表示一个键/值对的集合。例如有:

Hashtable hashtable = new Hashtable();
hashtable.Add("name","张三");
hashtable.Add("sex","男");
foreach(DictionaryEntry dicEntry in hashtable)
{
    Console.WriteLine(dicEntry.key + dicEntry.Value);
}

(四)Hashtable 元素的查找

        在 Hashtable 中查找元素时,可以使用 Hashtable 类提供的 Contains 方法、ContainsKey 方法和 ContainsValue 方法。

        1、Contains 方法

        该方法用来确定 Hashtable 中是否包含特定键,其语法格式如下:

public virtual bool Contains(Object key)

key:要在 Hashtable 中定位的键
返回值:如果包含则为 true,否则为 false

        2、ContainsKey 方法

        ContainsKey 方法与 Contains 方法实现的功能和语法都相同。

        3、ContainsValue 方法

        该方法用来确定 Hashtable 中是否包含特定值,其语法格式如下:

public virtual bool ContainsValue(Object value)

value:要在 Hashtable 中定位的值,该值可以为空引用
返回值:如果包含则为 true,否则为 false

三、List 泛型集合

        List 泛型集合与 ArrayList 集合类似,其基本语法如下:

List<int> ls = new List<int>();

        List 元素的添加、删除、查找和遍历等,都与 ArrayList 集合类似,可参考 ArrayList 集合的内容。但与 ArrayList 不同的是,所添加的元素必须与声明的类型一致,也就是说上述代码声明了一个存储 int 类型的数据的 List 集合,就不能用 Add 方法添加非整型的数据。

四、Dirtionary 泛型集合(键值对集合)

        Dirtionary 泛型集合与 Hashtable 哈希表集合类似,也是采用键值对的方式存储数据,其基本语法如下:

Dirtionary<int, string> dic = new Dirtionary<int, string>();

        Dirtionary 元素的添加、删除、查找和遍历等,都与 Hashtable 集合类似,可参考 Hashtable 哈希表集合的内容。但与 Hashtable 不同的是,所添加的元素必须与声明的类型一致。

        在用 foreach 遍历时,可以用 KeyValuePair(键值对)进行遍历,例如对上述的集合进行遍历:

foreach(KeyValuePair<int string> in dic)
{
    ....
}

五、索引器

        索引器一种常见的场景就是声明一个数组并初始化,通过下标可以访问数组中的元素,此时,这个中括号就是一个索引器,还有一种用法就是在类中声明一个私有的数组以及该数组的属性,在类的外部通过这个类的对象来为属性赋值时,可以通过索引器,让对象以索引的方式操作数组,例如有:

class Person
{
    private int[] numbers = new int[5];

    public int[] Numbers
    {
        get{return numbers;}
        set{numbers = value;}
    }

    //索引器,让对象以索引的方式操作数组
    //第一个 int 是数组的类型,表示访问拿到的值是 int 类型
    //第二个 int 是数组内的元素的类型,int 表示访问数组的整数类型
    public int this[int index]
    {
        get{return numbers[index];}
        set{numbers[index] = value;}
    }

    //此时第二个 string 表示键值是 string 类型的,反推上述的 int 类型数组
    //Dictionary<string, string> dic = new Dictionary<string, string>()
    //public string this[string index]
    // {
    //     get{return dic[index];}
    //     set{dic[index] = value;}
    // }
}

使用方法:
static void Main(string[] args)
{
    Person p = new Person();
    p[0] = 1;
    p[1] = 2;
    p[2] = 3;
    p[3] = 4;
    p[4] = 5;
}


网站公告

今日签到

点亮在社区的每一天
去签到