C# 列表List的各种使用

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

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Class1> a = new List<Class1>();//声明class1类型的列表

            var b = new List<int>(10);

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

            //增
            c.Add(8);//添加到列表最后一位
            c.Insert(2, 33);//在2号位插入13,2号位后整体后移

            //删
            c.Remove(33);//冲第一个开始遍历,删除第一个遇到的元素,剩余数据前移
            c.RemoveAt(0);//删除指定位置的元素
            c.RemoveRange(0, 3);//删除0号位在内开始的三个元素

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

            //获取
            int d = b.Capacity;//获取当前最大容量
            int e = b.Count;//获取当前已用容量
            int f = c.Capacity;//获取当前最大容量
            int g = c.Count;//获取当前已用容量

            //查
            int h = c.IndexOf(464);//从前往后遍历
            int i = c.LastIndexOf(464);//从后往前遍历

            //判断
            bool j = c.Contains(2);//从前往后数,有则为true,没有则为false

            //输出
            Console.WriteLine("列表B的元素");
            for (int m = 0; m < d; m++)
            {
                b.Add(m);//添加到列表最后一位
                Console.Write(b[m] + " ");
            }
            Console.WriteLine();

            Console.WriteLine("列表C的元素");
            for (int n = 0; n < g; n++)
            {
                Console.Write(c[n] + " ");
            }
            Console.WriteLine();

            Console.WriteLine("查找的元素");
            Console.WriteLine(h);
            Console.WriteLine(i);
            Console.WriteLine("判断的元素");
            Console.WriteLine(j);

            Console.ReadLine();
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值