集合、哈希表、泛型集合、字典

集合(ArrayList)

   用的时候导入命名空间:using System.Collections;

   加入数据时,不用考虑数据类型

   拿出数据时多数情况需要里氏转换

方法

用ArrayList的方法添加内容,定义一个person类有一个打招呼方法,如果需要用到person类的内容,里氏转换并调用打招呼方法,否则,输出添加的内容

代码:

            ArrayList arrayList = new ArrayList();//类的初始化
            arrayList.Add("小蚊子");//往arrayList中添加
            arrayList.Add(12);
            arrayList.Add(new person("小郭", 20, ''));
            for (int i = 0; i < arrayList.Count; i++)//遍历 arrayList的数组,arrayList.Count为数组的长度
            {
                if (arrayList[i] is person)//用is判断arrayList[i] 是否能转化为 person类
                {
                    ((person)arrayList[i]).SayHi();//如果能转换为person,强转,调用person中SayHi方法
                }
                else
                {
                    Console.WriteLine(arrayList[i]);//输出arrayList[i]
                }

            }
            Console.ReadKey();
        }
    }
    public class person
    {

        public person(string name, int age, char sex)
        {
            this.Name = name;
            this.Age = age;
            this.Sex = sex;

        }
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
        private char _sex;
        public char Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }
        public void SayHi()
        {
            Console.WriteLine("{0}岁的{1}在软件学院是最纯洁的{2}学生", Age, Name, Sex);
        }
    }
}

ArrayList其它常用方法:

           ArrayList arrayList = new ArrayList();
            int[] nums = new int[] { 3, 4, 5, 6, 7, 9, 8, 8 };
            arrayList.InsertRange(0, nums);//在指定索引位置插入一个数组
            int num = arrayList.LastIndexOf(9);//查找出元素最后一次出现的位置
            arrayList.Remove(9);//移除集合中某个元素
            arrayList.RemoveAt(2);//给个索引,移除这个索引对应的元素
            arrayList.RemoveRange(4, 2);//从指定索引位置开始移除,移除几个元素
            arrayList.Reverse();//集合反转
            arrayList.Sort();//排序

哈希表(Hashtable)
   用的时候导入命名空间:using System.Collections;

   加入数据时,不用考虑数据类型,以键值对的方式存储数据,键值不能相同

   哈希表无序

常用方法:

           Hashtable hashTable = new Hashtable();//实例化化 hashTable
            hashTable.Add(1, "小杨");//在 hashTable中插入键值为1,值为小杨数据
            hashTable.Add(2, "老苏");
           hashTable.Clear();//清空 hashTable中的值
           bool result = hashTable.Contains(1);//判断 hashTable中是否包含键值为1的数据
           hashTable.Remove("小杨");//移除小杨
           // hashTable[key];//这个key对应的值
           int num = hashTable.Count;//哈希表的长度

泛型集合(List<T>)
  不需要导入命名空间

  存放指定类型的数据

  如果为int类型,可以直接进行求和,最大值,最小值,平均值

方法:求一个int类型的泛型集合长度为100的最大值,最小值,平均值,和

            List<int> num = new List<int>();//实例化num
            int max, min, sum;
            for (int i = 0; i < 100; i++)//在num中添加字段
            {
                num.Add(i);
            }
            sum = num.Sum();//计算num的和
            max = num.Max();//最大值
            min = num.Min();//最小值
            double avg = num.Average();//求平均值

字典:Dictionary<TKey,TValue>

  不需要导入命名空间,需要实例化

  键值和值都是指定的类型

  Key:键值类型,一个哈希表中键值不能相同

  TValue:值类型

例子:简繁转换

           Dictionary<char, char> dic = new Dictionary<char, char>();
            string newStr = "";
            for (int i = 0; i < Jian.Length; i++)
            {
                dic.Add(Jian[i], HXW[i]);
            }
            for (int i = 0; i < str.Length; i++)
            {

                if (dic.ContainsKey(str[i]))
                {
                    newStr += dic[str[i]];
                }
                else
                {
                    newStr += str[i];
                }
            }
            Console.WriteLine(newStr);
            Console.ReadKey();


总结:

    HashTable,ArrayList,可以存放任意类型的数据,HashTable、Dictionary都是以键值对的方式存放数据,List和Dtctionary插入数据时不用刻意的考虑数据类型。这四个都可以对其数据进行增,删,查,改

原文地址:https://www.cnblogs.com/guohuiru/p/2776671.html