找回密码
 立即注册
首页 业界区 安全 Delphi 动态数组利器:TList 与 TList<T> 完全指南 ...

Delphi 动态数组利器:TList 与 TList<T> 完全指南

决任愧 2025-5-31 23:03:42
Delphi 动态数组利器:TList 与 TList 完全指南

1. 前言

在 Delphi 开发中,动态数组是高频使用的数据结构。传统 TList 和现代泛型 TList 各有优势,但如何选择?本文将从基本用法性能对比实际示例,带你全面掌握这两种列表的用法,并给出最佳实践建议。
2. TList:经典的指针动态数组

TList 是 Delphi 早期的动态数组实现,存储 Pointer 类型,适合需要直接操作内存的场景。
2.1 基本用法
  1. var
  2.   MyList: TList;
  3.   P: Pointer;
  4. begin
  5.   MyList := TList.Create;
  6.   try
  7.     P := Pointer(42); // 存储整数(需类型转换)
  8.     MyList.Add(P);    // 添加元素
  9.     // 访问元素(需强制转换)
  10.     IntegerValue := Integer(MyList[0]);
  11.   finally
  12.     MyList.Free; // 需手动释放
  13.   end;
  14. end;
复制代码
2.2 优缺点


  • 优点

    • 轻量级,无泛型开销。
    • 兼容旧版 Delphi。

  • 缺点

    • 类型不安全,需手动转换。
    • 不自动管理对象,易内存泄漏。

3. TList:类型安全的现代泛型列表

从 Delphi 2009 开始引入的 TList(位于 System.Generics.Collections),提供编译时类型检查,更安全易用。
3.1 基本用法
  1. var
  2.   IntList: TList<Integer>;
  3. begin
  4.   IntList := TList<Integer>.Create;
  5.   try
  6.     IntList.Add(100);       // 直接添加整数
  7.     FirstItem := IntList[0]; // 直接访问(无需转换)
  8.   finally
  9.     IntList.Free; // 自动管理内存(若 T 是托管类型)
  10.   end;
  11. end;
复制代码
3.2 核心功能

操作代码示例说明添加元素IntList.Add(42)末尾添加删除元素IntList.Delete(0)按索引删除查找元素IntList.IndexOf(100)返回索引(-1 表示不存在)排序IntList.Sort默认升序遍历for Item in IntList do ...支持 for-in 循环3.3 高级技巧

(1) 自定义排序
  1. IntList.Sort(
  2.   TComparer<Integer>.Construct(
  3.     function(const A, B: Integer): Integer
  4.     begin
  5.       Result := B - A; // 降序排序
  6.     end
  7.   )
  8. );
复制代码
(2) 二分查找(需先排序)
  1. IntList.Sort;
  2. if IntList.BinarySearch(100, FoundIndex) then
  3.   WriteLn('Found at index: ', FoundIndex);
复制代码
4. 性能对比与选型建议

4.1 性能对比

场景TListTList类型安全性❌ 需手动转换✅ 编译时检查内存管理需手动释放自动管理(部分类型)执行速度⚡ 更快⚡ 稍慢(泛型开销)代码可读性低高4.2 选型建议


  • 用 TList 如果

    • 需要类型安全(如存储字符串、接口、记录等)。
    • 开发新项目,或使用 Delphi 2009+。

  • 用 TList 如果

    • 维护旧代码,或需要极致性能(如高频操作裸指针)。

5. 实战示例:对象列表管理

假设需要管理 TStudent 对象列表:
  1. type
  2.   TStudent = class
  3.   public
  4.     Name: string;
  5.     Age: Integer;
  6.   end;
  7. var
  8.   Students: TList<TStudent>;
  9.   Student: TStudent;
  10. begin
  11.   Students := TList<TStudent>.Create;
  12.   try
  13.     // 添加学生
  14.     Student := TStudent.Create;
  15.     Student.Name := 'Alice';
  16.     Student.Age := 20;
  17.     Students.Add(Student);
  18.     // 遍历输出
  19.     for Student in Students do
  20.       WriteLn(Student.Name, ' (', Student.Age, ')');
  21.     // 释放对象(需手动)
  22.     for Student in Students do
  23.       Student.Free;
  24.   finally
  25.     Students.Free;
  26.   end;
  27. end;
复制代码
注意:TList 不会自动释放对象,需手动遍历 Free!
6. 总结


  • TList:适合底层指针操作,但需谨慎管理内存。
  • TList:推荐大多数场景,类型安全、易维护。
  • 最佳实践

    • 新项目优先用 TList。
    • 涉及对象时,记得手动释放内存。
    • 高频操作考虑预分配 Capacity 提升性能。

进一步学习

  • 官方文档:Generics.Collections
  • 推荐书籍:《Delphi High Performance》

希望这篇指南能帮助你高效使用 Delphi 的动态数组!如果有问题,欢迎留言讨论。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册