C#中4个常用的集合

1.ArrayList

         ArrayList类似于数组,有人也称它为数组列表。ArrayList可以动态维护,而数组的容量是固定的。

它的索引会根据程序的扩展而重新进行分配和调整。和数组类似,它所存储的数据称为元素,它所保存的元素数就是它的容量。默认初始容量为0,在使用它时,需引入命名空间System.Connections;以下代码可以定义一个ArrayList:

using System.Collections;

//创建容量为0的ArrayList对象

ArrayList myList = new ArrayList();

//创建容量为5的ArrayList对象

ArrayList myList = new ArrayList(5);

//获取对象中实际包含的元素数

int num = myList.Count();

ArrayList通过Add()方法添加元素,其方法返回一个Int类型的值,这个值代表所添加的元素在集合中的索引。

参数:如果向ArrayList中添加的元素是值类型,那么这些元素就会自动装箱处理转换为Object引用类型,然后保存,所以ArrayList中的所有元素都是对象的引用。

删除ArrayList中的元素有三种方法,分别为:

对象名.RomoveAt(int index);

对象名.Romove(Object value);

对象名.Clear();(这种方法会将集合中的所有元素删除,俗称"清空"~~~)

2.HashTable

C# /提供了一种称为HashTable的数据结构,通常称为哈希表,有的人称它为"字典".HashTable的数据是通过键(Key)和值(Value)来组织的,同ArrayList一样,它也属于System.Collections命名空间中,它所存放的每个元素都是键/值对.以下为HashTable的常用方法和属性:

属性名称:Count

属性名称:Keys

属性名称:Values  说明: 获取包含在HashTable中值的集合

方法名称:Add(Object key,Object Value)

方法名称:Remove(Object Key)

方法名称:Clear()

和ArrayList不同,访问HashTable元素时可以直接通过键名来获取具体值,同样,由于值类型是Object.所以当得到一个值时也需要通过类型转换得到指定类型的对象.

3. 泛型集合: List<T>

 

泛型是C#2.0中的一个新特性。泛型引入了一个新概念:类型参数。通过使用类型参数(T),减少了运行时强制转换成装箱操作的风险。通过泛型集合可以最大限度的重用代码、保护类型的安全及提高性能。

定义一个 List<T>泛型集合的方法如下:

List<T> 对象名 = new List<T>();

List<T>添加元素、获取元素、删除元素以及遍历和ArrayList用法都是类似的,但 List<T>保障了类型的安全性。在获取元素时无需进行类型转换.下面我们把List<T>和ArrayList作以比较

不用点:List<T>对所保存元素做类型约束,而ArrayList可以增加任意类型。添加、读取值类型元素 List<T>无需拆箱装箱,而ArrayList需要做拆箱、装箱处理。

相同点:通过索引访问集合中的元素,添加、删除元素方法相同

4.泛型集合Dictionary<K,V>

它具有泛型的全部特性,编译时检查类型约束,获取元素时无需类型转换,并且它存储数据的方式和HashTable类似。也是通过Key/Value对元素保存的。定义语法为:

Dictionary<K,V>对象名 = new Dictionary<K,V>

<K,V>中的K表示集合中Key的类型,V表示Value的类型,它的含义和List<T>是相同的.例如:

Dictionary<string,SE> engineers = new Dictionary<string,SE>();

在这个集合中,Key类型是string类型,Value是SE类型。 下面我们把 Dictionary<K,V> 和HashTable作以比较:

不同点: Dictionary<K,V>对所保存的元素做类型约束,而HashTable可以增加任何类型。 Dictionary<K,V>添加、读取值类型元素无需拆箱、装箱,而HashTable需要做拆箱、装箱处理

相同点:通过Key获取Value, 添加、删除、遍历元素方法相同

C#中四种集合的运用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;

namespace C_中四种集合的运用
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList array = new ArrayList();//实例化一个ArrayList集合对象
            Hashtable hashtable = new Hashtable();//实例化一个HashTable集合对象
            List<Student> list = new List<Student>();//实例化一个List<>集合对象
            Dictionary<string, Student> dictionary = new Dictionary<string, Student>();//实例化一个Directory<>集合对象
            Student Zhang = new Student("张三", '', 22);
            Student Li = new Student("李四", '', 20);          //实例化3个Student对象
            Student Sun = new Student("小五", '', 21);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Console.WriteLine("                  使用ArrayList 集合         ");
            array.Add(Zhang);
            array.Add(Li);       //用ArrayList的对象名添加Student对象
            array.Add(Sun);
            Console.WriteLine("**********使用For循环遍历**********");
            for (int i = 0; i < array.Count; i++)
            {
                Student stu = (Student)array[i];
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex);
            }
            Console.WriteLine();
            Console.WriteLine("删除Student张三的信息");
            array.Remove(Zhang);//用对象名删除元素
            Console.WriteLine("\n***********使用Foreach循环遍历一***********");

            foreach (Student item in array)
            {
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", item.Name, item.Age, item.Sex);

            }


            Console.WriteLine("\n***********使用Foreach循环遍历二***********");
            foreach (Student item in array)
            {
                Student stu = (Student)item;
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex);
            }
            Console.WriteLine("\n***********使用Foreach循环遍历三***********");
            foreach (var item in array)
            {
                Student stu = (Student)item;
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex);
            }
            Console.WriteLine();
            Console.WriteLine("                 使用HashTable集合");
            hashtable.Add("张三", Zhang);
            hashtable.Add("李四", Li);//
            hashtable.Add("小五", Sun);
            Console.WriteLine("删除Student李四的信息");
            hashtable.Remove("李四");//用键(key)移除元素
            Console.WriteLine("\n***********使用Foreach循环遍历集合的所有值一***********");
            foreach (Student item in hashtable.Values)//循环遍历集合的值
            {
                
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", item.Name, item.Age, item.Sex);
            }
            Console.WriteLine("\n***********使用Foreach循环遍历集合的所有值二***********");
            foreach (Student item in hashtable.Values)//循环遍历集合的值
            {
                Student stu = (Student)item;
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex);
            }
            Console.WriteLine("\n***********使用Foreach循环遍历集合的所有值三***********");
            foreach (var item in hashtable.Values)//循环遍历集合的值
            {
                Student stu = (Student)item;
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex);
            }
            Console.WriteLine();
            Console.WriteLine("***********使用Foreach循环遍历Name值******");
            foreach (string Name in hashtable.Keys)//循环遍历集合的键(key)
            {
                Console.WriteLine("我的姓名是{0}", Name);
            }
            Console.WriteLine();
            Console.WriteLine("                  使用List<>集合");
            list.Add(Zhang);
            list.Add(Li);           //为List<>集合添加元素
            list.Add(Sun);
            Console.WriteLine("***********使用Foreach循环遍历***********");
            list.Remove(Sun);//移除集合元素Sun的信息
            foreach (var item in list)
            {
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", item.Name, item.Age, item.Sex);
            }
            Console.WriteLine();
            list.RemoveAt(0);//移除集合中索引为0的元素
            Console.WriteLine("***********使用For循环遍历*********");
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", list[i].Name, list[i].Age, list[i].Sex);
            }
            Console.WriteLine();
            Console.WriteLine("                  使用Directory<>集合");
            dictionary.Add("张三", Zhang);
            dictionary.Add("李四", Li);      //为Directory<>集合添加元素
            dictionary.Add("小五", Sun);
            Console.WriteLine("*************使用Foreach循环遍历***********");
            foreach (var item in dictionary.Values)
            {
                Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", item.Name, item.Age, item.Sex);
            }
            Console.WriteLine();
            dictionary.Remove("李四");//移除集合中李四的信息
            Console.WriteLine("************使用Foreach循环遍历Name值******");
            foreach (string Name in dictionary.Keys)
            {
                Console.WriteLine("我的姓名是{0}", Name);
            }
            sw.Stop();
            Console.WriteLine("时间:{0}", sw.ElapsedMilliseconds);
            Console.ReadLine();
        }

    }
}
//--------------------------Student-----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace C_中四种集合的运用
{
    public class Student
    {
        public Student() { }
        public Student(string name, char sex, int age)
        {
            this.Name = name;
            this.Sex = sex;
            this.Age = age;
        }
        public string Name { get; set; }
        public char Sex { get; set; }
        public int Age { get; set; }
    }

}
C#中四种集合的运用

相关文章:

  • 2021-05-04
  • 2021-09-09
  • 2021-07-23
  • 2022-12-23
  • 2021-06-05
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-09
  • 2021-09-21
  • 2021-06-28
  • 2021-11-29
  • 2021-06-26
  • 2021-09-07
相关资源
相似解决方案