C# List general interpretation

Used vs 2019

List<int> a = new List<int>();//声明了一个int类型的列表

            var b = new List<int>();

            var c = new List<int>(10) {
    
     1, 3, 4, 12, 33, 5, 76, 8, 76};//10代表列表的容量,大括号里是初始值

            c.Add(13);//添加一个数字到列表里

            Console.WriteLine(c[3]);

            int d = c.Capacity;//获取当前列表的最大容量

            int e = c.Count;//获取当前列表的长度

            Console.WriteLine(d + "     " + e);

            c.Insert(2, 13);//在2号位插入13,2号位以后的元素整体后移1位

            c.Remove(1);//从第一个元素开始从前向后遍历,删除第一个遇到的元素,剩余数据向前移动1位

            c.RemoveAt(0);//删除指定索引的元素

            c.RemoveRange(0, 3);//从某个索引开始,删除包裹该索引在内的3个元素

            int f =  c.IndexOf(76); //从后往前遍历 输出第一个遇到的与括号内元素相同的元素在列表中的索引 返回给f
            int g = c.LastIndexOf(76);//从后往前遍历 输出第一个遇到的与括号内元素相同的元素在列表中的索引 返回给g

            Console.WriteLine(f + "  " + g);

            c.Sort();//从小到大排序

            bool h = c.Contains(3);//确定3是否存在于列表

            Console.WriteLine(h);

            foreach (var item in c)
            {
    
    
                Console.Write(item + "  ");
            }

            Console.ReadKey();

Running results
Insert picture description here
If you can't understand it, you can go to the rookie tutorial to have a look!

Guess you like

Origin blog.csdn.net/m0_47605113/article/details/114916400