C#List<T> 常用实例方法2

一、容量与内存相关

1. TrimExcess()

作用:释放多余容量,让 Capacity 等于 Count,减少内存占用。
适用:集合不再新增元素时使用。

2. EnsureCapacity(int capacity)

作用:预先扩容,指定最小容量,避免频繁扩容带来性能损耗。

示例

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> list = new List<int>();
        Console.WriteLine($"初始 Capacity:{list.Capacity}");

        list.EnsureCapacity(20); // 预先设置容量为20
        Console.WriteLine($"EnsureCapacity 后 Capacity:{list.Capacity}");

        for (int i = 1; i <= 10; i++)
            list.Add(i);

        Console.WriteLine($"元素数量 Count:{list.Count}");
        list.TrimExcess(); // 裁剪多余空间
        Console.WriteLine($"TrimExcess 后 Capacity:{list.Capacity}");
    }
}

输出:

初始 Capacity:0
EnsureCapacity 后 Capacity:20
元素数量 Count:10
TrimExcess 后 Capacity:10

二、区间查找 / 条件索引

1. FindIndex(int startIndex, Predicate<T> match)

从指定下标开始,查找第一个符合条件元素的索引

2. FindIndex(int startIndex, int count, Predicate<T> match)

限定起始位置 + 查找个数,匹配条件索引

3. FindLastIndex(Predicate<T> match)

查找最后一个符合条件元素的索引

4. FindLastIndex(int startIndex, Predicate<T> match)

从指定下标向前查找最后匹配索引

示例

List<int> nums = new List<int> { 2, 4, 6, 4, 8, 4 };

// 从下标2开始找第一个等于4的索引
int idx1 = nums.FindIndex(2, x => x == 4);
// 最后一个等于4的索引
int idx2 = nums.FindLastIndex(x => x == 4);

Console.WriteLine($"FindIndex:{idx1}");
Console.WriteLine($"FindLastIndex:{idx2}");

输出:

FindIndex:3
FindLastIndex:5

三、批量判断 & 整体校验

1. TrueForAll(Predicate<T> match)

所有元素都满足条件 返回 true,否则 false

示例

List<int> list = new List<int> { 2, 4, 6, 8 };
bool allEven = list.TrueForAll(x => x % 2 == 0);
Console.WriteLine($"是否全部偶数:{allEven}"); // True

四、范围删除之外:复制、填充

1. CopyTo(T[] array)

将 List 元素完整复制到一维数组

2. CopyTo(T[] array, int arrayIndex)

从目标数组指定下标开始复制

3. CopyTo(int listIndex, T[] array, int arrayIndex, int count)

精准范围复制:List 起始位置 → 数组起始位置 → 复制个数

示例

List<string> list = new List<string> { "A", "B", "C", "D" };
string[] arr = new string[5];

list.CopyTo(arr);
Console.WriteLine(string.Join(",", arr)); // A,B,C,D,

五、反向查找元素

FindLast(Predicate<T> match)

获取最后一个满足条件的元素,无匹配返回 default(T)

示例

List<int> nums = new List<int> { 1, 3, 5, 3, 7 };
int lastThree = nums.FindLast(x => x == 3);
Console.WriteLine($"最后一个3:{lastThree}"); // 3

六、索引相关补充

LastIndexOf(T item)

从后往前查找元素,返回最后一次出现的索引,无则 -1

List<int> nums = new List<int> { 1, 2, 2, 3 };
int lastIdx = nums.LastIndexOf(2);
Console.WriteLine(lastIdx); // 2

七、方法总览(之前 + 本次补充 完整清单)

原有常用(上一轮)

  • 增删:Add / AddRange / Insert / InsertRange / Remove / RemoveAt / RemoveRange / RemoveAll / Clear
  • 查询:Contains / IndexOf / Exists / Find / FindAll
  • 排序转换:Sort / Reverse / GetRange / ToArray / ToList
  • 遍历:ForEach

本次新增补充(全为实例方法)

  1. 容量管理:EnsureCapacityTrimExcess
  2. 高级索引查找:FindIndex(多重载)、FindLastIndexLastIndexOf
  3. 反向元素查找:FindLast
  4. 整体判断:TrueForAll
  5. 数组复制:CopyTo(多重载)

完整整合演示(所有补充方法放一起运行)

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        #region 容量操作
        List<int> list = new List<int>();
        Console.WriteLine($"初始容量:{list.Capacity}");
        list.EnsureCapacity(15);
        Console.WriteLine($"预扩容后:{list.Capacity}");

        for (int i = 1; i <= 8; i++)
            list.Add(i);
        list.TrimExcess();
        Console.WriteLine($"裁剪后容量:{list.Capacity}\n");
        #endregion

        #region FindIndex / FindLastIndex / LastIndexOf
        List<int> nums = new List<int> { 2, 4, 6, 4, 8, 4 };
        int fIndex = nums.FindIndex(2, x => x == 4);
        int flIndex = nums.FindLastIndex(x => x == 4);
        int lIndex = nums.LastIndexOf(4);
        Console.WriteLine($"FindIndex:{fIndex}");
        Console.WriteLine($"FindLastIndex:{flIndex}");
        Console.WriteLine($"LastIndexOf:{lIndex}\n");
        #endregion

        #region FindLast
        int lastItem = nums.FindLast(x => x == 4);
        Console.WriteLine($"FindLast 结果:{lastItem}\n");
        #endregion

        #region TrueForAll
        bool allEven = nums.TrueForAll(x => x % 2 == 0);
        Console.WriteLine($"全部偶数?{allEven}\n");
        #endregion

        #region CopyTo
        string[] strArr = new string[6];
        List<string> strList = new List<string> { "一", "二", "三" };
        strList.CopyTo(strArr);
        Console.WriteLine("CopyTo 结果:" + string.Join(" ", strArr));
        #endregion

        Console.ReadLine();
    }
}
posted @ 2026-05-26 14:56  人生就是修炼  阅读(9)  评论(0)    收藏  举报