c# ArryList 键值对

来源:互联网 发布:千牛 卖家工作台 mac 编辑:程序博客网 时间:2024/05/21 06:52

1 ArrayListList

1)新建ArrayList

 ArrayList list = new ArrayList();

2)添加单个元素
     list.Add(true);
     ist.Add(1);
     list.Add("张三");

3)添加集合元素
    list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });

4)       list.Clear();清空所有元素

            list.Remove(true);删除单个元素 写谁就删谁
            list.RemoveAt(0);根据下标去删除元素
            list.RemoveRange(0, 3);根据下标去移除一定范围的元素
             list.Sort();//升序排列
            list.Reverse();反转
            list.Insert(1, "插入的");在指定的位置插入一个元素
            list.InsertRange(0, new string[] { "张三", "李四" });在指定的位置插入一个集合
            bool b = list.Contains(1);判断是否包含某个指定的元素

举例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace _07ArrayList的各种方法{    class Program    {        static void Main(string[] args)        {            ArrayList list = new ArrayList();            //添加单个元素            list.Add(true);            list.Add(1);            list.Add("张三");            //添加集合元素            list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });            //list.AddRange(list);            //list.Clear();清空所有元素            //list.Remove(true);删除单个元素 写谁就删谁            //list.RemoveAt(0);根据下标去删除元素            //list.RemoveRange(0, 3);根据下标去移除一定范围的元素            // list.Sort();//升序排列            //list.Reverse();反转            //list.Insert(1, "插入的");在指定的位置插入一个元素            //list.InsertRange(0, new string[] { "张三", "李四" });在指定的位置插入一个集合            //bool b = list.Contains(1);判断是否包含某个指定的元素            list.Add("颜世伟");            if (!list.Contains("颜世伟"))            {                list.Add("颜世伟");            }            else            {                Console.WriteLine("已经有这个屌丝啦");            }            for (int i = 0; i < list.Count; i++)            {                Console.WriteLine(list[i]);            }            Console.ReadKey();        }    }}
输出结果:

已经有这个屌丝啦
True
1
张三
1
2
3
4
5
6
7
8
9
颜世伟

例 2

 static void Main(string[] args)        {           // 写一个长度为10的集合,要求在里面随机地存放10个数字(0-9),            //但是要求所有的数字不重复            ArrayList list = new ArrayList();            Random r = new Random();            for (int i = 0; i <10; i++)            {                int rNumber = r.Next(0, 10);                //集合中没有这个随机数                if (!list.Contains(rNumber))                {                    list.Add(rNumber);                }                else//集合中有这个随机数                {                    //一旦产生了重复的随机数 这次循环就不算数                    i--;                }            }            for (int i = 0; i < list.Count; i++)            {                Console.WriteLine(list[i]);            }            Console.ReadKey();        }
2 键值对集合:根据键去找值

        static void Main(string[] args)        {            //创建了一个键值对集合对象            Hashtable ht = new Hashtable();            ht.Add(1, "张三");            ht.Add(2, true);            ht.Add(3, '男');            ht.Add(false, "错误的");            ht.Add(5, "张三");            ht[6] = "新来的";//这也是一种添加数据的方式            ht[1] = "把张三干掉";//替换原来键1对应的值            //abc----cba            if (!ht.ContainsKey("abc"))            {                ht.Add("abc", "cba");               // ht["abc"] = "哈哈哈";            }            else            {                Console.WriteLine("已经包含abc这个键!!!");            }           // ht.Clear(); //移除集合中所有的元素           // ht.Remove(3);//移除集合中键为3的元素            foreach (var item in ht.Keys)            {                Console.WriteLine("键是-----{0}=====值是{1}", item, ht[item]);            }            Console.ReadKey();        }





原创粉丝点击