C#基础-059 泛型集合List<>与字典Dictionary<,>

 static void Test01()
        {
            ArrayList list = new ArrayList();
            //Add方法的参数类型是object类型 在传递参数的时候整型1会转化成object类型  这样属于装箱操作
            list.Add(1);
            //如果实现  list[0] + 2  得到正确的结果  要将list[0]转化成int类型  也就是说要进行拆箱操作  
            Console.WriteLine((int)list[0] + 2);
            //泛型集合在声明的时候已经确定了里面的元素类型
            List<string> list0 = new List<string>();
            //里面的传入的数据类型只能和声明时定义的类型保持一致
            //泛型能在编译时,提供强大的类型检查,减少数据类型之间的显示转化、装箱操作和运行时的类型检查。
            list0.Add("C#编程之道");
            list0.Add("C#从入门到精通");
            list0.AddRange(list0);
            Console.WriteLine(list0.Count);

            bool ret = list0.Contains("C#编程之道");
            Console.WriteLine(ret);

            int index = list0.IndexOf("C#从入门到精通", 2);
            Console.WriteLine(index);

            List<int> numList = new List<int>();
            numList.Add(1);
            numList.Add(2);
            numList.Add(5);
            numList.Add(1);
            numList.Add(2);
            numList.Add(2);
            numList.Add(4);

            List<int> newList = numList.GetRange(0, numList.Count);
            index = -1;
            //记录查找元素的数量
            int count = 0;
            while (newList.IndexOf(2) != -1)
            {
                index = newList.IndexOf(2);
                count++;
                newList.RemoveAt(index);
            }
            Console.WriteLine(count);
            Console.WriteLine("-------------------------");
            foreach (var item in numList)
            {
                Console.WriteLine(item);
            }
        }     

Dictionary:

 class Book
    {
        private string name;
        private int price;
        private string author;

        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public int Price
        {
            get
            {
                return price;
            }

            set
            {
                price = value;
            }
        }

        public string Author
        {
            get
            {
                return author;
            }

            set
            {
                author = value;
            }
        }

        public Book(string name, int price, string author)
        {
            this.Name = name;
            this.Price = price;
            this.Author = author;
        }

        public override string ToString()
        {
            return string.Format("书名:{0} 价格:{1} 作者:{2}", Name, Price, Author);
        }
    }
  static void Test01()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("张杰", "高飞");
            dic.Add("刘欢", "好汉歌");
            //在一个字典中 键是唯一的
            //在一个字典中不同的键可以对应相同的值
            dic.Add("周杰伦", "青花瓷");
            dic.Add("刘英", "青花瓷");
            bool key = dic.ContainsKey("周杰伦");
            Console.WriteLine(key);
            bool value = dic.ContainsValue("好汉歌");
            Console.WriteLine(value);
            KeyCollection keys = dic.Keys;
            //dic.Remove("刘英");
            foreach (var item in keys)
            {
                Console.WriteLine(item);
            }
        }
        static void Test02()
        {
            Dictionary<string, Book> books = new Dictionary<string, Book>();
            books.Add("0000000",new Book("C#编程之道",56,"王垚"));
            books.Add("0000001", new Book("C#从入门到精通", 98, "YY"));
            books.Add("0000002", new Book("C#从入门到放弃", 2, "亚东"));
            foreach (var item in books)
            {
                Console.WriteLine(item.Key +" " + item.Value.Price);
            }
        }

        static void Test03()
        {
            SortedDictionary<string, string> dic = new SortedDictionary<string, string>();
            dic.Add("张杰", "高飞");
            dic.Add("刘欢", "好汉歌");
            //在一个字典中 键是唯一的
            //在一个字典中不同的键可以对应相同的值
            dic.Add("周杰伦", "青花瓷");
            dic.Add("刘英", "青花瓷");
            dic.Add("asdef", "青花瓷");
            dic.Add("asdeh", "青花瓷");
            bool key = dic.ContainsKey("周杰伦");
            Console.WriteLine(key);
            bool value = dic.ContainsValue("好汉歌");
            Console.WriteLine(value);
            foreach (KeyValuePair<string,string> item in dic)
            {
                Console.WriteLine(item.Key);
            }
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值