1. 程式人生 > >C#中的集合物件總結

C#中的集合物件總結

1、雜湊表:

           //雜湊表
            Hashtable student = new Hashtable();
            for (int i = 0; i < 10; i++) 
            {
                string key = "sk" + i.ToString();
                string value = "sv" + i.ToString();

                if (!student.ContainsKey(key)) 
                {
                    student.Add(key, value);
                }
            }

            //根據鍵移除元素
            student.Remove("sk3");

            foreach (DictionaryEntry element in student)
            {
                string id = element.Key.ToString();
                string name = element.Value.ToString();
                
                Debug.WriteLine("學生的ID:" + id + ",學生的姓名:" + name);
            }
           //學生的ID:sk9,學生的姓名:sv9
           //學生的ID:sk8,學生的姓名:sv8
           //學生的ID:sk5,學生的姓名:sv5
           //學生的ID:sk6,學生的姓名:sv6
           //學生的ID:sk7,學生的姓名:sv7
           //學生的ID:sk4,學生的姓名:sv4
           //學生的ID:sk1,學生的姓名:sv1
           //學生的ID:sk0,學生的姓名:sv0
           //學生的ID:sk2,學生的姓名:sv2


2、ArrayList:

            ArrayList arrlist = new ArrayList();
            arrlist.Add("item0");
            arrlist.Add("item1");
            arrlist.Add("item2");
            arrlist.Add("item3");

            foreach (int n in new int[3] { 0, 1, 2 })
            {
                arrlist.Add(n);
            }
            //arrlist.Remove(0);//移除值為0的第一個元素
            //arrlist.RemoveAt(3);//移除index為3的元素
            //arrlist.Insert(1, "apple");//在index為1的位置插入元素

            for (int i = 0; i < arrlist.Count; i++)
            {
                Debug.WriteLine(arrlist[i].ToString());
            }



3、HashSet:

            HashSet<string> hSet = new HashSet<string>();
            hSet.Add("item0");
            hSet.Add("item1");
            hSet.Add("item0");
            hSet.Add("item0");

            foreach (string item in hSet)
            {
                Debug.WriteLine("foreach=" + item);
            }

            //foreach=item0
            //foreach=item1



4、List:

            //列表
            //list.
            List<string> list = new List<string>();

            list.Add("item0");
            list.Add("item1");
            list.Add("item2");

            for (int i = 0; i < list.Count; i++)
            {
                Debug.Write("list[" + i + "]=" + list[i] + "\n");
            }

            list.RemoveAt(0);

            Debug.Write("刪除掉一個元素\n");

            for (int j = 0; j < list.Count; j++)
            {
                Debug.Write("list[" + j + "]=" + list[j] + "\n");
            }

            Debug.Write("或者用foreach來遍歷\n");

            foreach (string item in list)
            {
                Debug.Write("foreach元素=" + item + "\n");
            }

            Debug.Write("新增陣列\n");

            list.AddRange(new string[] { "新增元素1", "新增元素2", "新增元素3" });

            list.ForEach(ShowItem);

            private static void ShowItem(string item) 
            {
                Debug.Write("新增後遍歷的item=" + item + "\n");
            }

5、Queue:

            //佇列
            //先進先出
            Queue<string> queue = new Queue<string>();
            queue.Enqueue("item0");
            queue.Enqueue("item1");
            queue.Enqueue("item2");
            queue.Enqueue("item3");

            foreach (string item in queue)
            {
                Debug.Write("foreach queue=" + item + "\n");
            }

            Debug.Write("__________________用while方式_________________\n");

            while (queue.Count > 0)
            {
                Debug.Write("while=" + queue.Dequeue() + "\n");
            }



6、stack:

            //棧
            //先進後出
            Stack<string> stack = new Stack<string>();
            stack.Push("item0");
            stack.Push("item1");
            stack.Push("item2");
            stack.Push("item3");

            foreach (string item in stack)
            {
                Debug.Write("stack-foreach:" + item + "\n");
            }

            Debug.Write("__________________用while方式_________________\n");

            while (stack.Count > 0)
            {
                Debug.Write("stack-while:" + stack.Pop() + "\n");
            }


7、LinkedList:

            //連結串列
            LinkedList<string> lList = new LinkedList<string>();
            LinkedListNode<string> node = new LinkedListNode<string>("root");
            lList.AddFirst(node);

            node = lList.AddAfter(node, "item0");
            node = lList.AddAfter(node, "item1");
            node = lList.AddAfter(node, "item2");
            node = lList.AddAfter(node, "item3");

            foreach (string item in lList)
            {
                Debug.WriteLine("foreach-lList:" + item);
            }

            node = lList.First;
            Debug.WriteLine("第一個元素:" + node.Value);
            node = lList.Last;
            Debug.WriteLine("最後一個元素:" + node.Value);

            MyList<string> list = new MyList<string>();

            for (int i = 0; i < 10; i++)
            {
                list.AddHead("item" + i);
            }
            foreach (string item in list)
            {
                Debug.WriteLine(item);
            }

            //item9
            //item8
            //item7
            //item6
            //item5
            //item4
            //item3
            //item2
            //item1
            //item0

            MyList<int> list = new MyList<int>();
            for (int i = 0; i < 10; i++)
            {
                list.AddHead(i);
            }

            foreach (int item in list)
            {
                Debug.WriteLine(item);
            }

            //9
            //8
            //7
            //6
            //5
            //4
            //3
            //2
            //1
            //0



8、SortedList:

            SortedList<int, string> sList = new SortedList<int, string>();
            sList.Add(5, "item0");
            sList.Add(25, "item1");
            sList.Add(24, "item2");
            sList.Add(475, "item3");

            foreach (KeyValuePair<int, string> item in sList)
            {
                //根據key的順序來
                Debug.WriteLine("key=" + item.Key.ToString() + ",value=" + item.Value);
            }

9、Dictionary:

            Dictionary<int, string> dict = new Dictionary<int, string>();
            dict.Add(141, "item0");
            dict.Add(46, "item1");
            dict.Add(541, "item2");
            dict.Add(21, "item3");

            foreach (KeyValuePair<int, string> item in dict)
            {
                //按新增的順序來
                Debug.WriteLine("key=" + item.Key.ToString() + ",value=" + item.Value);
            }
            dict.containsKey("xxx");//判斷有沒有包含這個key
            dict.containsValue("xxx");

排序,從小到大:

Dictionary<int, string> dictSorted = dict.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);

                            foreach (KeyValuePair<int, string> kv in dictSorted) 
                            {
                                Debug.WriteLine("keyNew=" + kv.Key + " valNew=" + kv.Value);
                            }
從大到小:
Dictionary<int, string> dic1desc = test.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value); 

用for迴圈遍歷:

                            List<int> indexList = new List<int>(dictSorted.Keys);
                            // 按照新的dict來獲取值
                            for (int i = 0; i < dictSorted.Count; i++) 
                            {
                                int index = indexList[i];
                                string name = dictSorted[i];
                            }





10、SortedDictionary:

            SortedDictionary<int, string> dict = new SortedDictionary<int, string>();
            dict.Add(23, "item0");
            dict.Add(54, "item1");
            dict.Add(132, "item2");
            dict.Add(12, "item3");

            foreach (KeyValuePair<int, string> item in dict)
            {
                //按key的順序來
                Debug.WriteLine("key=" + item.Key.ToString() + ",value=" + item.Value);
            }

            //key=12,value=item3
            //key=23,value=item0
            //key=54,value=item1
            //key=132,value=item2

11、DataTable:

            DataTable dataTable = MySQLiteHelper.ExecuteDataTable(sqlText);
            for (int i = 0; i < dataTable.Rows.Count; i++) 
            {
                for (int j = 0; j < dataTable.Columns.Count; j++) 
                {
                    Debug.WriteLine(dataTable.Rows[i][dataTable.Columns[j].ColumnName]);
                }
            }