【C#视频】之 ArrayList 、HashTable 、List<T> 、Dictionary

1、using System.Collections; ——导入 ArrayList 类

        static void Main(string[] args)
        {
            int[] nums = new int[] { 1, 2, 3, 4, 5 };
            ArrayList arrayList = new ArrayList();
            arrayList.Add(10);
            arrayList.Add(29);
            arrayList.Add("芬芬");
            arrayList.Add('女');
            arrayList.Add(true);
            arrayList.Add(3.15m);
            arrayList.Add(45.4);
            arrayList.Add(nums);
            //arrayList[0] = 100;//重新赋值
            arrayList.Add("哈哈,休息");
            //给集合添加数据,用Add()方法,内容不限定

            for (int i = 0; i < arrayList .Count ; i++)
            {
                if (arrayList[i] is int[])
                {
                    int[] numbers = (int[])arrayList[i];
                    for (int j = 0; j < numbers.Length; j++)
                    {
                        Console.WriteLine(numbers[j]);
                    }
                }
                //if部分是将nums数组中的数据显示出来
                else
                {
                    Console.WriteLine(arrayList [i]);
                }
            }
            Console.ReadKey();
        }

2、集合的常用方法:

            arrayList.Clear();//清空了
            arrayList.Contains(1);//判断这个集合中是否包含这个元素
            int num = arrayList.Count;//集合中元素的个数
            int number = arrayList.IndexOf(2);//找某一个元素对应的索引

            int[]nums = { 1, 2, 3 };
            arrayList.InsertRange(4,nums );//往某一个索引位置插入一个数组

            arrayList.Remove(1);//移除某一元素
            arrayList.RemoveAt(1);//移除索引对应的值
            arrayList.RemoveRange(3, 100);//从指定索引处开始移除,移除多少个,如果超出索引报异常

            arrayList.Reverse();//反转
            arrayList.Sort();//排序

3、ArrayList 方法

添加数据:Add()、AddRange()……添加数组用add

使用Add可以添加数据,并且与数组一样可以使用下标(索引)访问数据

使用下标访问的数据是object类型的,必要时需要进行转换,必须满足里氏转换原则

将add参数设为object,目的——为了通用

AddRange将数组或集合当中的数据批量的一个一个的加进来

数组或集合中有多少个数据,新集合中就添加了多少个数据,类型等一 一对应

但Add同样可以将数组或集合作为数据加入,但此时加入将数组和集合作为一个项添加过去

只有找到该项,才能通过下标访问到其中的数据

4、HashTable 方法

HashTable的增、删、查、判存

添加数据,都是键值对的形式

键值对均是object类型

键值对中的就是为了找数据用的,必须提供,不允许重复

HashTable 使用键作为寻找的方式,是一种无序的结构

使用<hashtable 实例名>【键】,将返回object类型的,由键对应的数据

转换,使用里氏转换原则

5、using System.Collections; ——导入 HashTable 类

        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            //哈希表  以键值对的形式存值  key-------键    value------值
            //无序的

            ht.Add("老苏",1001);
            ht.Add(1002,"老牛");
            ht.Add("小赵","小路");

            foreach (object  str in ht.Keys )
            {
                Console.WriteLine("key{0},------------value{1}",str,ht[str]);
            }

            Console.WriteLine("添加成功");

            Console.ReadKey();
        }

6、foreach

语法:

foreach(集合中单个的类型,局部变量名 in 集合对象)

{

循环体

循环体当中“局部变量”表示集合中遍历的数据

}

循环的过程(重点)

总是从开始,一直循环到结束,中间不会停下来,除了break

临时变量,可以为其赋值吗?数组或集合中的数据会受到影响吗?

常规下,数组或集合此时是只读的

临时变量的类型

设断点查看

使用var判断

 

简繁体转换

class Program
{
    简体字
    
    繁体字

 static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            //遍历所有的简体字
            //把简体字作为key存到哈希表中,把繁体字作为value存到哈希表中
            for (int i = 0; i < Jian .Length ; i++)
            {
                ht.Add(Jian[i], HXW[i]);
            }
            Console.WriteLine();
    
    文章

for (int i = 0; i < text.Length ; i++)
            {
                //判断这个哈希表中是否有这个简体字
                if (ht.ContainsKey (text[i])) //该处写 ContainsKey 或 Contain 都可以
                {
                    //如 ht[小]====曉;text[i]===小
                    Console.Write(ht[text[i]]);
                }
                else
                {
                    Console.Write(text [i]);
                }
            }
            Console.ReadKey();
        }

}

7、泛型集合——List<T>       

1)为了专门处理某种类型       ArrayList 对应的是List<类型名>

2)在尖括号中写什么类型,这个集合就变成了什么类型的集合

如:List<int> list = new List<int>(); 

 list.Add(2);     int类型

 

            List<string> listStr = new List<string>();

            listStr.Add("哈哈");

String 类型

3)添加数据、插入数据、索引访问数据都是这个类型的,不用考虑索引的转化问题

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

            Random r = new Random();
            int num = 0;
            while (list .Count !=10)
            {
                num = r.Next(1, 100);
                if (!list .Contains (num ))
                {
                    list.Add(num);
                }
            }
         
            Console.WriteLine("最大值:{0}",list .Max ());
            Console.WriteLine("最小值:{0}", list.Min ());
            Console.WriteLine("和为:{0}", list.Sum ());
            Console.ReadKey();

8、Dictionary

Dictionary<Tkey , TValue>

1)默认提供命名控件,提倡使用

2)Hashtable 对应的是   Dictionary<键的类型,值的类型>

在尖括号中填入键的类型与值的类型,那么这个集合就变成了一个指定的键值对模型

其使用方式与Hashtable 一样

            Dictionary<string, string> dic = new Dictionary<string, string>();

            dic.Add("老苏","凤姐");
            dic.Add("老牛","芙蓉姐姐");
            dic.Add("老马","春哥");
            dic.Add("老虎","增哥");
            dic.Add("老蒋","小月月");

            foreach (string  item in dic .Keys )
            {
                Console.WriteLine("key---:{0},valut---:{1}",item ,dic [item ]);
            }
            Console.ReadKey();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值