https://www.cnblogs.com/bumpkin/p/17261247.html
uses generics.collections;
var List: TList<Integer>; FoundIndex: Integer; begin { Create a new List. } List := TList<Integer>.Create; { Add a few values to the list. } List.AddRange([5, 1, 8, 2, 9, 14, 4, 5, 1]); showmessage('Index of first 1 is ' + IntToStr(List.IndexOf(1))); showmessage('Index of last 1 is ' + IntToStr(List.LastIndexOf(1))); showmessage('Does List contains element 100? ' + BoolToStr(List.Contains(100))); { Add another element to the list. } List.Add(100); showmessage('There are ' + IntToStr(List.Count) + ' elements in the list.'); { Remove the first occurrence of 1. } List.Remove(1); { Delete a few elements from position 0. } List.Delete(0); List.DeleteRange(0, 2); { Extract the remaining 1 from the list. } List.Extract(1); { Set the capacity to the actual length. } List.TrimExcess; showmessage('Capacity of the list is ' + IntToStr(List.Capacity)); { Clear the list. } List.Clear; { Insert some elements. } List.Insert(0, 2); List.Insert(1, 1); List.InsertRange(0, [6, 3, 8, 10, 11]); { Sort the list. } List.Sort; { Binary search for the required element. } if List.BinarySearch(6, FoundIndex) then //必须是Sorted 数列,并且是从小到大排列,序号从1开始 showmessage('Found element 6 at index ' + IntToStr(FoundIndex)); { Reverse the list. } List.Reverse; showmessage('The element on position 0 is ' + IntToStr(List.Items[0])); List.Free; readln; end.
Unit IntList; //用列表来装整数 Interface Uses classes; Type TIntegerList = Class(TList) Private Procedure SetInteger(Index: Integer; Value: LongInt); Function GetInteger(Index: Integer): LongInt; Public Property Items[Index: Integer]: LongInt Read GetInteger Write SetInteger; Default; Procedure Add(Value: LongInt); Function IndexOf(Value: LongInt): Integer; Procedure Sort; End; Function SortHelper(Item1, Item2: Pointer): Integer; Implementation Function SortHelper(Item1, Item2: Pointer): Integer; Begin If (LongInt(Item1) < LongInt(Item2)) Then result := -1 Else begin If (LongInt(Item1) > LongInt(Item2)) Then result := 1 Else result := 0; end; End; Procedure TIntegerList.Sort; Begin Inherited Sort(SortHelper); End; Function TIntegerList.IndexOf(Value: LongInt): Integer; Begin result := Inherited IndexOf(Pointer(Value)); End; Procedure TIntegerList.SetInteger(Index: Integer; Value: LongInt); Begin Inherited Items[Index] := Pointer(Value); End; Function TIntegerList.GetInteger(Index: Integer): LongInt; Begin result := LongInt(Inherited Items[Index]); End; Procedure TIntegerList.Add(Value: LongInt); Begin Inherited Add(Pointer(Value)); End; End.