如何更改泛型类型值?

Kee*_*per 0 delphi generics delphi-2010

在我的应用程序中,我创建了TList类型列表,用于存储整数或双精度:

TKList<T> = class
  private
    FItems: TList<T>;
    function GetItem(Index: Integer): T;
    procedure SetItem(Index: Integer; const Value: T);
    function GetMaxValue(): T;
    function GetMinValue(): T;
  public
    constructor Create; overload;
    constructor Create(const AKList: TKList<T>); overload;
    destructor Destroy; override;
    procedure Assign(const AKList: TKList<T>);
    function Add(const AValue: T): Integer;
    procedure Clear;
    function Count: Integer;
    procedure Invert;
    function ToString: string; override;
    function Info: string;
    property Values[Index: Integer]: T read GetItem write SetItem; default;
  end;
Run Code Online (Sandbox Code Playgroud)

如何实现Invert()过程来反转泛型List中的值?

提前致谢.

zz1*_*433 5

假设你想要1, 3, 5在调用你希望拥有的函数之后反转数组5, 3, 1

然后,您可以实现这样的过程.

procedure TKList<T>.Invert;
var
  I: Integer;
begin
  for I := 0 to (Count - 1) div 2 do
    FItems.Exchange(I, Count - I - 1);
end;
Run Code Online (Sandbox Code Playgroud)

我会建议Reverse它的名字,因为Invert有点令人困惑.