天天看點

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

static void Test01()
        {
            ArrayList list = new ArrayList();
            //Add方法的參數類型是object類型 在傳遞參數的時候整型會轉化成object類型  這樣屬于裝箱操作
            list.Add();
            //如果實作  list[] +   得到正确的結果  要将list[]轉化成int類型  也就是說要進行拆箱操作  
            Console.WriteLine((int)list[] + );
            //泛型集合在聲明的時候已經确定了裡面的元素類型
            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#從入門到精通", );
            Console.WriteLine(index);

            List<int> numList = new List<int>();
            numList.Add();
            numList.Add();
            numList.Add();
            numList.Add();
            numList.Add();
            numList.Add();
            numList.Add();

            List<int> newList = numList.GetRange(, numList.Count);
            index = -;
            //記錄查找元素的數量
            int count = ;
            while (newList.IndexOf() != -)
            {
                index = newList.IndexOf();
                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#程式設計之道",,"王垚"));
            books.Add("0000001", new Book("C#從入門到精通", , "YY"));
            books.Add("0000002", new Book("C#從入門到放棄", , "亞東"));
            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);
            }
        }