Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>

Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持.

Generics.Collections 包含了以下实用类:
TList<T>
TQueue<T>
TStack<T>
TDictionary<TKey,TValue>
TObjectList<T>
TObjectQueue<T>
TObjectStack<T>
TObjectDictionary<TKey,TValue>



有了以上泛型的容器, 恐怕 Classes.TList 和 Contnrs 单元下的 TObjectList 等系列容器也就只为兼容存在了.

Generics.Collections.TList<T> 既然是泛型的, 那肯定应该容得下字符串列表, 本例就依此测试吧.

如果你对泛型不了解, 应先看看: http://www.cnblogs.com/del/archive/2008/08/14/1268258.html



本例效果图:



代码文件:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Generics.Collections; {Delphi 2009 新增的泛型容器单元}

var
  List: TList<string>;  {定义一个泛型 TList 类, 这指定了要用于 string}
  str: string = ‘Test‘;

{建立}
procedure TForm1.FormCreate(Sender: TObject);
begin
  List := TList<string>.Create;

  Memo1.Clear;
  Edit1.Text := str;
  Button1.Caption := Button1.Caption + ‘ 显示‘;
  Button2.Caption := Button2.Caption + ‘ 添加‘;
  Button3.Caption := Button3.Caption + ‘ 插入‘;
  Button4.Caption := Button4.Caption + ‘ 删除1‘;
  Button5.Caption := Button5.Caption + ‘ 删除2‘;
  Button6.Caption := Button6.Caption + ‘ 查找‘;
  Button7.Caption := Button7.Caption + ‘ 排序‘;
  Button8.Caption := Button8.Caption + ‘ 翻转‘;
  Button9.Caption := Button9.Caption + ‘ 清空‘;
  Button10.Caption := Button10.Caption + ‘ 添加数组‘;
end;

{释放}
procedure TForm1.FormDestroy(Sender: TObject);
begin
  List.Free;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if Edit1.Text <> ‘‘ then str := Edit1.Text;
end;

{显示}
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  Memo1.Clear;
  for i := 0 to List.Count - 1 do
    Memo1.Lines.Add(List[i]);  {List[i] = List.Item[i]}
  Text := Format(‘当前总数: %d‘, [List.Count]);
end;

{添加}
procedure TForm1.Button2Click(Sender: TObject);
begin
  List.Add(str);
  Button1.Click; {刷新显示}
end;

{插入, 譬如插入在最前面}
procedure TForm1.Button3Click(Sender: TObject);
begin
  List.Insert(0, str);
  Button1.Click;
end;

{根据序号删除, 譬如删除第一个数据}
procedure TForm1.Button4Click(Sender: TObject);
begin
  List.RemoveAt(0);
  Button1.Click;
end;

{根据内容删除, 譬如删除第一个数据}
procedure TForm1.Button5Click(Sender: TObject);
var
  s: string;
begin
  s := List[0];
  List.Remove(s);
  Button1.Click;
end;

{查找}
procedure TForm1.Button6Click(Sender: TObject);
var
  n: Integer;
begin
  n := List.IndexOf(str); {LastIndexOf 是从后面找; 也可用 List.Contains(str) 判断是否包含 str}
  if n = -1 then
    ShowMessage(‘没找到‘)
  else
    ShowMessageFmt(‘%s 是第 %d 个‘, [str, n+1]);
end;

{排序}
procedure TForm1.Button7Click(Sender: TObject);
begin
  List.Sort;
  Button1.Click;
end;

{翻转}
procedure TForm1.Button8Click(Sender: TObject);
begin
  List.Reverse;
  Button1.Click;
end;

{清空}
procedure TForm1.Button9Click(Sender: TObject);
begin
  List.Clear;
  Button1.Click;
end;

{添加数组}
procedure TForm1.Button10Click(Sender: TObject);
const
  arr: array[0..2] of string = (‘CodeGear‘, ‘Delphi‘, ‘2009‘);
begin
  List.Add(‘Embarcadero‘);
  List.AddRange(arr);
  Button1.Click;
end;

end.


窗体文件:


object Form1: TForm1
  Left = 0
  Top = 0
  Caption = ‘Form1‘
  ClientHeight = 201
  ClientWidth = 349
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = ‘Tahoma‘
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 121
    Height = 201
    Align = alLeft
    Lines.Strings = (
      ‘Memo1‘)
    ScrollBars = ssBoth
    TabOrder = 0
    ExplicitHeight = 206
  end
  object Button1: TButton
    Left = 136
    Top = 13
    Width = 81
    Height = 25
    Caption = ‘Button1‘
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 127
    Top = 44
    Width = 100
    Height = 25
    Caption = ‘Button2‘
    TabOrder = 2
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 127
    Top = 75
    Width = 100
    Height = 25
    Caption = ‘Button3‘
    TabOrder = 3
    OnClick = Button3Click
  end
  object Button4: TButton
    Left = 127
    Top = 106
    Width = 100
    Height = 25
    Caption = ‘Button4‘
    TabOrder = 4
    OnClick = Button4Click
  end
  object Button5: TButton
    Left = 127
    Top = 137
    Width = 100
    Height = 25
    Caption = ‘Button5‘
    TabOrder = 5
    OnClick = Button5Click
  end
  object Button6: TButton
    Left = 240
    Top = 44
    Width = 100
    Height = 25
    Caption = ‘Button6‘
    TabOrder = 6
    OnClick = Button6Click
  end
  object Button7: TButton
    Left = 240
    Top = 75
    Width = 100
    Height = 25
    Caption = ‘Button7‘
    TabOrder = 7
    OnClick = Button7Click
  end
  object Button8: TButton
    Left = 240
    Top = 106
    Width = 100
    Height = 25
    Caption = ‘Button8‘
    TabOrder = 8
    OnClick = Button8Click
  end
  object Button9: TButton
    Left = 240
    Top = 137
    Width = 100
    Height = 25
    Caption = ‘Button9‘
    TabOrder = 9
    OnClick = Button9Click
  end
  object Edit1: TEdit
    Left = 240
    Top = 15
    Width = 100
    Height = 21
    TabOrder = 10
    Text = ‘Edit1‘
    OnChange = Edit1Change
  end
  object Button10: TButton
    Left = 127
    Top = 168
    Width = 214
    Height = 25
    Caption = ‘Button10‘
    TabOrder = 11
    OnClick = Button10Click
  end
end
时间: 2024-10-10 20:59:13

Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>的相关文章

泛型容器单元(Generics.Collections)[2]: TQueue&lt;T&gt; 队列列表

TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TQueue 主要有三个方法.一个属性:Enqueue(入列).Dequeue(出列).Peek(查看下一个要出列的元素);Count(元素总数). 本例效果图: 代码文件: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dia

泛型容器单元(Generics.Collections)[3]: TStack&lt;T&gt; 堆栈列表

TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TStack 主要有三个方法.一个属性:Push(压栈).Pop(出栈).Peek(查看下一个要出栈的元素);Count(元素总数). 本例效果图: 代码文件: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, S

从头认识java-13.11 对比数组与泛型容器,观察类型擦除给泛型容器带来什么问题?

这一章节我们继续类型擦除的话题,我们将通过对比数组与泛型容器,观察类型擦除给泛型容器带来什么问题? 1.数组 package com.ray.ch13; public class Test { public static void main(String[] args) { Fruit[] fruits = new Apple[5]; fruits[0] = new Apple(); fruits[1] = new Fuji(); fruits[2] = new Fruit(); } } cla

C++制作一个泛型容器(可以盛放各种类型的对象)

如果你想要一个可以盛放各种类型的对象,那么基本上可以说在C++里没有,或者你可以用vector<boost::any>或者其他的什么来模拟,我说那都不怎么好.问题就在于我的类型会在运行时动态的增加,你不可能知道我会增加什么类型,我的头文件也不会给你. 现在是不是觉得C++的泛型用不上了,是的,C++的泛型本质上是对相似代码的复用,做的事情都是同一件事情,但仅仅是处理类型的差别.这种情况用的还是比较少的,比如vector,queue,map等这些容器是用泛型的最好的地方了.但你想过没有,这些类型

从头认识java-13.11 对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题?

这一章节我们继续类型擦除的话题,我们将通过对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题? 1.数组 package com.ray.ch13; public class Test { public static void main(String[] args) { Fruit[] fruits = new Apple[5]; fruits[0] = new Apple(); fruits[1] = new Fuji(); fruits[2] = new Fruit(); } } cla

Delphi 对泛型TList的的改进(TSimpleList)

TSimpleList类应用源码 uSimpleList.pas 源码 unit uSimpleList; interface uses Generics.Collections; type TSimpleList<T> = class(TList<T>) private FCurIndexPos: integer; function DoPopByIndex(Index: integer): T; procedure FreeAllItems; procedure SetCurI

JAVA泛型容器的类型检查

泛型容器是通过指定容器包含对象的类型,由编译器保证对象类型的正确性,在编译阶段就能检查出类型错误.如下列将List<Long>对象longList赋予一个List<GenericTest>对象gtList,会报编译错误. public class GenericTest { public static List<Long> longList = Arrays.asList (1L ,2L); public static void main(String args[]){

数组与泛型容器区别

一.基本 class Shape{ void draw(){ System.out.println(this+".draw()"); } } class Circle extends Shape{ @Override public String toString() { return "Circle"; } } class Rect extends Shape{ @Override public String toString() { return "Re

Delphi项目构成之单元文件PAS

单元文件是Pascal源文件,扩展名为.pas. 有三种类型的单元文件: 窗体/数据模块和框架的单元文件(form/data module and frame units),一般由Delphi自动生成. 组件的单元文件(component units),由您或者Delphi生成. 通用的单元文件(general-purpose units),有您创建. 下面让我们看看基本的Pascal单元文件结构是如何的? Step1.在主菜单上选择[File | New | Unit],Delphi将创建一个