c#的集合类型

1.C#的数组
2.C#的ArrayList和List
3.C#的Hashtable和Dictionary

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

namespace lesson7
{
    class Program
    {
        static void Main(string[] args)
        {//集合型的数据类型 并且实现了IEnumerable 适用于读取每次数据
            List<int> listInt = new List<int>() { 1, 2, 3 };
            foreach(var intInList in listInt)
            {//循环中每次都会提取赋予变量
                Console.WriteLine(intInList);
            }

            //数组是一个引用类型 System.Array
            int[] number = new int[5];
            string[,] name = new string[5, 4];
            //这里还有一个数组的数组 就是每一行的数字不固定
            byte[][] scores = new byte[5][];
            for(int i=0;i<scores.Length;i++)
            {
                scores[i] = new byte[i + 3];
            }
            for(int i=0;i<scores.Length;i++)
            {//可以看到每一排都是不一样的
                Console.WriteLine("length of row {0} is {1}", i, scores[i].Length);
            }
            int[] number2=new int[5] {1,2,3,4,5};
            int[] number3 = new int[] { 1, 2, 3, 4, 5 };
            int[] number4 = { 1, 2, 3, 4, 5 };
            string[,] name2 = { { "g", "k" }, { "q", "r" } };
            //不能省掉 new int[] 要不默认就是多维数组
            int[][] numbers2 = { new int[] { 1, 2, 3 }, new int[] { 5, 6, 7, 8 } };

            foreach(var i in number2)
            {
                Console.WriteLine(i);
            }


            //System.Collection 进行添加和删除元素
            ArrayList al = new ArrayList();//数组列表可以存储各种类型
            al.Add(5);
            al.Add(100);
            al.Remove(5);
            al.Add("iloveunity");
            foreach(var i in al)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine(al[0]);//这种列表对代码的安全性提出了考验,
            //因为拿出来的不知道是什么元素

            //现在比较推荐的做法是List,提高代码的质量
            List<int> intList2 = new List<int>();
            intList2.Add(500);
            intList2.AddRange(new int[] { 501, 502 });//添加一串元素
            Console.WriteLine(intList2.Contains(200));
            Console.WriteLine(intList2.IndexOf(501));
            intList2.Remove(200);
            intList2.Insert(1, 1001);//插入元素
            foreach (var i in intList2)
            {
                Console.WriteLine(i);
            }

            Hashtable ht = new Hashtable();
            ht.Add("first", "i");
            ht.Add("second", "love");
            ht.Add(100, 1000);
            Console.WriteLine(ht["second"]);
            Console.WriteLine(ht[100]);
            //同样hash表也是类型不安全的 ,因此我们的解决办法是 强类型
            Dictionary<string, string> d = new Dictionary<string, string>();
            //强制规定是字符串是key value也是字符串 但它不是线程安全的
            d.Add("first", "i");
            //ConcurrentDictionary是安全的

            SortedList<int, int> sl = new SortedList<int, int>();
            sl.Add(5, 105);
            sl.Add(2, 102);
            sl.Add(10, 99);//根据key值来排序的
            foreach(var i in sl)
            {
                Console.WriteLine(i);
                Console.WriteLine(i.Value);
            }

            //stack ,queue这是两个特殊的队列
            Console.ReadLine();
        }
       
    }
}

猜你喜欢

转载自xuyi1994.iteye.com/blog/2230978