1. 程式人生 > >黑馬程式設計師_List泛型集合如Dictionary鍵值對集合的一些用法

黑馬程式設計師_List泛型集合如Dictionary鍵值對集合的一些用法

List泛型集合:List<T>
泛型集合
就是為了專門處理某種型別
 ArrayList對應的是 List<型別名>
在尖括號中寫什麼型別,這個集合就變成了什麼型別的集合

語法:List<資料型別>  集合名稱=new List<資料型別>();

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06List泛型集合
{
    class Program
    {
        static void Main(string[] args)
        {
          
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.AddRange(new int[] { 1, 2, 3, 4, 5 });
            list.AddRange(list);
            //將集合轉換成陣列
            //List<string> listTwo = new List<string>();
            //listTwo.ToArray()

            //List<byte> list1 = new List<byte>();
            //list1.ToArray()


            //for (int i = 0; i < list.Count; i++)
            //{
            //    Console.WriteLine(list[i]);
            //}

            foreach (int item in list)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
         //   ArrayList listArr = new ArrayList();
        }
    }
}

2,鍵值對集合:Dictionary <TKey,TValue>
預設提供名稱空間,提倡使用
Hashtable對應的是 Dictionary<鍵的型別, 值的型別>
在尖括號中填入鍵的型別與值的型別,那麼這個集合就變成了一個指定的鍵值對模型

 static void Main(string[] args)
        {
            #region 鍵值對的應用
            Console.WriteLine("請輸入一個字串");
            string str = Console.ReadLine();
            Dictionary<char, int> dic = new Dictionary<char, int>();

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == ' ')
                {
                    continue;
                }
                if (!dic.ContainsKey(str[i]))
                {
                    dic.Add(str[i], 1);
                }
                else
                {
                    dic[str[i]]++;
                }
            }
            //dic = dic.OrderBy(r=>r.Value).ToDictionary(r=>r.Key,r=>r.Value);//升序排列
            dic = dic.OrderByDescending(r => r.Value).ToDictionary(r => r.Key, r => r.Value);//降序
            foreach (KeyValuePair<char, int> item in dic)
            {
                Console.WriteLine("{0}--{1}", item.Key, item.Value);
            }
            Console.ReadKey();

}

3,泛型集合與鍵值對集合的結合

 #region 泛型集合與鍵值對集合的結合
            //List<int> list = new List<int>();
            //List<int> listTwo = new List<int>();
            //for (int i = 1; i <= 100; i++)
            //{
            //    if (i % 2 == 0)
            //    {
            //        list.Add(i);
            //    }
            //    else
            //    {
            //        listTwo.Add(i);
            //    }
            //}
            //Dictionary<int, int> dic = new Dictionary<int, int>();
            //for (int i = 0; i < 50; i++)
            //{
            //    dic.Add(listTwo[i], list[i]);
            //}
            //foreach (KeyValuePair<int, int> item in dic)
            //{
            //    Console.Write("{0} {1}\t", item.Key, item.Value);
            //}
            //Console.ReadKey();
            //foreach (var item in list)
            //{
            //    Console.Write("{0}\t", item);
            //}
            //Console.WriteLine();
            //foreach (var item in listTwo)
            //{
            //    Console.Write("{0}\t", item);
            //}
            //Console.WriteLine(list.Count);
            //Console.ReadKey();
            #endregion

 //int[] number={2,4,6,8,12};
            //int[] number1 = { 3, 7, 9, 11 };
            //List<int> list = new List<int>(9);
            //for (int i = 0; i < number.Length; i++)
            //{
            //    list.Add(number[i]);
            //}
            //int count = 1;
            //for (int i = 0; i < number1.Length; i++)
            //{
            //    list.Insert(count,number1[i]);
            //    count += 2;

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