List<T> 泛型集合

    //
    // 摘要:
    //     Represents a strongly typed list of objects that can be accessed by index. Provides
    //     methods to search, sort, and manipulate lists.
    //
    // 类型参数:
    //   T:
    //     The type of elements in the list.
    public class List<[NullableAttribute(2)] T> : ICollection<T>, IEnumerable<T>, IEnumerable, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection, IList

 

public class Program
    {
        
        private static void Main()
        {
            Employee employee1 = new Employee("1", "三", "张", 25);
            Employee employee2 = new Employee("2", "四", "张", 29);
            Employee employee3 = new Employee("3", "五", "张", 31);
            Employee employee4 = new Employee("4", "二", "张", 36);
            var array = new Employee[2] { employee2, employee3 };

            //List容量默认初始值是4个元素,然后容量扩展为8、16、32,每次扩展都是原来得2倍。
            //而且列表容量改变时,整个集合就要重新分配一个新得内存块中,
            //所以尽可能在初始化时确定List的容量。
            var employees = new List<Employee>(4);
            Console.WriteLine($"List 的容量 {employees.Capacity}");
            Console.WriteLine($"List 当前存在的元素个数 {employees.Count}");
            
            //向List集合中添加元素
            employees.Add(employee1);
            //去除集合中多余的容量
            employees.TrimExcess();
            Console.WriteLine($"List 的容量 {employees.Capacity}");
            Console.WriteLine($"List 当前存在的元素个数 {employees.Count}");
           
            //向List集合中添加多个元素
            employees.AddRange(array);
            Console.WriteLine($"List 的容量 {employees.Capacity}");
            Console.WriteLine($"List 当前存在的元素个数 {employees.Count}");
           
            //向List集合中的指定位置插入元素
            employees.Insert(0, employee4);
            Console.WriteLine($"List 的容量 {employees.Capacity}");
            Console.WriteLine($"List 当前存在的元素个数 {employees.Count}");

            //通过List集合的索引值访问指定的元素
            Console.WriteLine(employees[2]);

            //通过索引删除指定位置1的元素。
            //RemoveAt要比Remove速度快,因为Remove方法需要使用IndexOf方法获取索引值然后再删除
            employees.RemoveAt(0);
            //Remove先匹配元素,然后才进行删除,删除失败会返回False
            employees.Remove(employee4);
            //RemoveRange会在指定位置删除掉指定元素个数
            employees.RemoveRange(0, 1);

            Console.WriteLine($"List 的容量 {employees.Capacity}");
            Console.WriteLine($"List 当前存在的元素个数 {employees.Count}");

            //在List集合中查找元素的索引值
            int index= employees.IndexOf(employee3);
            Console.WriteLine($"employee3 的索引值 {index}");
            //在List集合中用Lambda表达式查找元素的索引值
            index =employees.FindIndex(em => em.Id == "2");
            Console.WriteLine($"Id = 1 的元素索引值 {index}");

            foreach (var employee in employees)
            {
                Console.WriteLine(employee.ToString("All", null));
            }

            //List集合创建只读集合,不能对该集合进行写操作
            var employeesReadOnly = employees.AsReadOnly();

            foreach (var employee in employeesReadOnly)
            {
                Console.WriteLine(employee.ToString("All", null));
            }

        }

    }

    public class Employee : IComparable<Employee>, IFormattable
    {
        public string Id { get; }

        public string FirstName { set; get; }

        public string Lastname { get; set; }

        public int WorkingSeniority { get; set; }

        public Employee(string id, string firstName, string lastName, int workingSeniority)
        {
            Id = id;
            FirstName = firstName;
            Lastname = lastName;
            WorkingSeniority = workingSeniority;
        }

        public int CompareTo([AllowNull] Employee other)
        {
            return WorkingSeniority.CompareTo(other?.WorkingSeniority);
        }

        public override string ToString()
        {
            return $"{Lastname}{FirstName}";
        }
        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (format is null)
                format = "N";

            return format switch
            {
                "N" => ToString(),
                "F" => FirstName,
                "L" => Lastname,
                "W" => WorkingSeniority.ToString(),
                "All" => $"ID: {Id} {ToString()} WorkingSeniority:{WorkingSeniority}",
                _ => ToString(),
            };
        }

    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值