c# Arraylist集合与hashtable(不常用了,用泛型)

一、arraylist,可变数组

arraylist可以放入任何类型,其中元素都是obj类型,addrange()方法可以把数组元素放入,而不是数组整体

count表示实际存入元素个数,capacity为可存储容量,容量自动扩充,当超过现有元素个数时,自动扩容一倍空间,1-2-4-8-16...

 

 

 练习:

1. 创建一个集合,放入一些数字,求最大最小和平均值

 static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });
            int max=0;
            int min=0;
            int averge=0;
            int sum=0;

            for (int i = 0; i < list.Count; i++)
            {
             sum +=(int)list[i]; //arraylist中所有元素都是obj,必须进行转换
                if ((int)list[i]>max)
                {
                    max = (int)list[i];
                }
        
            }
            averge = sum / list.Count;

            Console.WriteLine($"和为{sum},平均为{averge},最大为{max}");
            Console.ReadKey();
        }

  2. 创建一个10的集合,放入随机数字,要求不能重复

     static void Main(string[] args)
        {
            ArrayList list = new ArrayList(10);
            for (int i = 0; i < 10; i++) //不能用count,因为还没有元素初始化
            {
                Random rm = new Random();
               int rms= rm.Next(0, 10);
                
                if (!list.Contains(rms))
                {
                    list.Add(rms);
                }
                else 
                {//退回上一次循环,去重
                    i--;
                }
            }

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }            
            Console.ReadKey();
        }
    }

  二、hashtable 字典,键值对集合

  static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            //add 添加值
            ht.Add(1, "猫");
            ht.Add(2, "狗");
            ht.Add(3, "猪");

            //下标添加,有则修改,没有则添加
            ht[4] = "牛";
            ht[1] = "龙";

            //foreach取出,keys,values表示键集合、值集合。结果:3 2 1
            foreach (var item in ht.Values)
            {
                Console.WriteLine(item);
            }            
            Console.ReadKey();
            //判断是否已经存在键或者值
            //ht.ContainsKey() 
            //ht.ContainsValue()

            //移除
            ht.Remove(3);
            //清除
            ht.Clear();

        }

  三、hashtable练习

简繁体转换,用户输入一行简体,转化为繁体

        static void Main(string[] args)
        {
            const string jian = "爱碍肮办坝补摆板帮报罢贝备笔";
            const string fan = "愛礙骯辦壩補擺闆幫報罷貝備筆";

            Hashtable ht = new Hashtable();
            //通过for循环 匹配 键-值
            for (int i = 0; i < jian.Length; i++)
            {
                ht[jian[i]] = fan[i];
            }

            //string就是char数组,可以遍历下标操作
            string input = Console.ReadLine();
            for (int i = 0; i < input.Length; i++)
            {
                if (ht.ContainsKey(input[i]))
                {
                    Console.Write(ht[input[i]]);   
                }
                else
                {
                    Console.Write(input[i]);
                }
            }
            Console.ReadKey();
        }

  

posted @ 2021-03-09 21:05  遥月  阅读(394)  评论(0)    收藏  举报