关于List<T>的属性与方法

本文探讨了List<T>的数据结构及其在编程中的使用,包括其属性和方法。内容源自刘老师的课程PPT,主要关注C#中的实现。

List<T>

转载刘老师课程PPT

List<int> intList = new List<int> { 100, 200, 300, 400, 500 };


var seg = intList.GetRange(1, 3);
for (int i = 0; i < seg.Count; i++)
{
    seg[i]++;
}

List<int>.Enumerator e = intList.GetEnumerator();

Console.WriteLine(e.Current);

while (e.MoveNext())
{
    Console.WriteLine(e.Current);
}
Console.WriteLine("------------------------------------");

foreach (var item in intList)
{
    Console.WriteLine(item);
}

Console.WriteLine("------------------------------------");
int sum = 0;

intList.ForEach(a => sum += a);
//intList.ForEach(action);

Console.WriteLine(sum);

Console.WriteLine("------------------------------------");
Console.WriteLine(String.Join(",", intList));
Console.WriteLine(String.Join(",", seg)); 

using System.Text.Json;

namespace ListSearch
{
    public class Book
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public double Price { get; set; }

        public override string ToString()
        {
            return JsonSerializer.Serialize(this);
        }

        public override bool Equals(object? obj)
        {
            if (obj == null) return false;

            Book other = obj as Book;

            if (other == null)
            {
                return false;
            }
            else if (other.Id == this.Id && other.Name == this.Name && other.Price == this.Price)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
using ListSearch;

List<double> list1 = new List<double> { 100.0, 200.0, 300.0, 400.0, 100.0, 200.0, 300.0, 400.0 };
Book book1 = new Book { Id = 1, Name = "Book-1", Price = 10 }, book6 = book1;
Book book2 = new Book { Id = 2, Name = "Book-2", Price = 20 };
Book book3 = new Book { Id = 3, Name = "Book-3", Price = 30 };
Book book4 = new Book { Id = 4, Name = "Book-4", Price = 40 };
Book book5 = new Book { Id = 1, Name = "Book-1", Price = 10 };

List<Book> list2 = new List<Book> { book1, book2, book3, book4 };

//bool res = list2.Contains(book5);

//bool res = list2.Exists(e => e.Price <= 30);

//bool res = list2.TrueForAll(e => e.Price <= 40);
//bool res = list1.TrueForAll(e => e % 100 == 0);

//Console.WriteLine(res);

//int idx = list1.IndexOf(300.0);
//int idx = list2.IndexOf(book5);

//int idx = list1.IndexOf(300, 2);
//Console.WriteLine(idx);

/*
int i = -1;
while (true)
{
    i = list1.IndexOf(300, i + 1);

    if (i == -1)
    {
        break;
    }
    Console.WriteLine(i);
}
*/

//int idx = list1.IndexOf(300, 3, 3);
int idx = list1.LastIndexOf(300);
Console.WriteLine(idx);

​​​​​​​

 public class Book:IComparable<Book>

public class Book:IComparable<Book>
{
    ..........
        public int CompareTo(Book? other)
        {
            if (other == null) return 1;

            return (this.Id - other.Id);

        }
    ..........
}
List<double> list1 = new List<double> { 80.0, 70.0, 60.0, 50.0 ,40.0, 30.0, 20.0, 10.0 };

Book book1 = new Book { Id = 1, Name = "Book-1", Price = 10 };
Book book2 = new Book { Id = 2, Name = "Book-2", Price = 20 };
Book book3 = new Book { Id = 3, Name = "Book-3", Price = 30 };
Book book4 = new Book { Id = 4, Name = "Book-4", Price = 40 };
Book book5 = new Book { Id = 1, Name = "Book-1", Price = 10 };

List<Book> list2 = new List<Book> { book1, book2, book3, book4 };

//var idx = list1.Find(e => e % 3 == 0);
//var idx = list2.Find(e => e.Price % 3 == 0);

//var idx = list2.FindLast(e => e.Price % 4 == 0);
//var idx = list1.FindLast(e => e % 3 == 0);
//Console.WriteLine(idx);

//var list = list1.FindAll(e => e % 3 == 0);
//var list = list2.FindAll(book => book.Price % 4 == 0);

//Console.WriteLine(String.Join(",", list));

//var idx = list1.FindIndex(e => e % 3 == 0);
//var idx = list1.FindIndex(1, e => e % 3 == 0);

//var idx = list2.FindIndex(2, 2, e => e.Price % 4 == 0);
//var idx = list2.FindLastIndex(2, 2, e => e.Price % 4 == 0);
//Console.WriteLine(idx);

//Console.WriteLine(String.Join(",", list1));
//list1.Sort();
//Console.WriteLine(String.Join(",", list1));

//var res = list1.BinarySearch(30);
//Console.WriteLine(res);
//Console.WriteLine(list1[res]);

Console.WriteLine(String.Join(",", list2));
list2.Sort();
Console.WriteLine(String.Join(",", list2));

var res = list2.BinarySearch(book5);
Console.WriteLine( res);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

issta

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值