RAD Studio VCL Reference
|
Ordered list.
TList<T> = class(TEnumerable<T>);
template <T> class TList : public TEnumerable<T>;
TList represents an ordered list, accessible by an index.
You can create a list with a specific collection of items and a comparison operator.
You can add, change, insert or remove an item from a list, or clear the entire list. You can add nil objects to the list.
You can sort, search and reverse a list.
Count contains the number of items in the queue. Capacity is the number of items the list can hold before being resized. You can also set and get values by indexing the Items array.
An OnNotify event tells you when the list has changed.
The class TObjectList inherits from TList and provides an automatic mechanism for freeing objects removed from lists.
C++ Examples:
/* Add the Delphi source file that appears on this Help page into a CPP Builder project that includes a CPP module containing the following code. An hpp file will be generated for the Delphi code when you build the project. Add the include line for that hpp file at the top of the CPP module. The FormCreates for both forms will execute and the following generics code will work. Remember to give the forms different names! */ void __fastcall TForm1::FormCreate(TObject *Sender) { // Prints type info for string type TGenericClass__1<System::UnicodeString> *GString = new TGenericClass__1<System::UnicodeString>(); GString->PrintTypeInfo(Memo1); // Prints type info for Byte type TGenericClass__1<Byte> *GByte = new TGenericClass__1<Byte>(); GByte->PrintTypeInfo(Memo1); // Prints type info for Double type TGenericClass__1<Double> *GDouble = new TGenericClass__1<Double>(); GDouble->PrintTypeInfo(Memo1); // Prints type info for "array of String" type // TGenericClass__1<array of string> *GStringArray = new TGenericClass__1<array of string>(); // GStringArray->PrintTypeInfo(Memo1); }
Delphi Examples:
{ This example demonstrates the usage of the generic TList class. } procedure TForm3.Button1Click(Sender: TObject); var List: TList<Integer>; FoundIndex: Integer; begin { Create a new List } List := TList<Integer>.Create(); { Register a notification call-back } List.OnNotify := ListChanged; { Add a few values to the list } List.AddRange([5, 1, 8, 2, 9, 14, 4, 5, 1]); MessageDlg('Index of first 1 is ' + IntToStr(List.IndexOf(1)), mtInformation, [mbOK], 0); MessageDlg('Index of last 1 is ' + IntToStr(List.LastIndexOf(1)), mtInformation, [mbOK], 0); MessageDlg('List contains elemnt 100? ' + BoolToStr(List.Contains(100)), mtInformation, [mbOK], 0); { Add another element to the list } List.Add(100); MessageDlg('There are ' + IntToStr(List.Count) + ' elements in the list.', mtInformation, [mbOK], 0); { Remove the first occurence of 1 } List.Remove(1); { Delete a few elements form position 0 } List.Delete(0); List.DeleteRange(0, 2); { Extract the remeining 1 from the list } List.Extract(1); { Set the capacity to the actual length } List.TrimExcess(); MessageDlg('There capacity of the list is ' + IntToStr(List.Capacity), mtInformation, [mbOK], 0); { 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 MessageDlg('Found element 6 at index ' + IntToStr(FoundIndex), mtInformation, [mbOK], 0); { Reverse the list } List.Reverse(); MessageDlg('The element on position 0 is ' + IntToStr(List.Items[0]), mtInformation, [mbOK], 0); end; procedure TForm3.ListChanged(Sender: TObject; const Item: Integer; Action: TCollectionNotification); begin { This method is called by the List everytime a change occurs } if Action = cnAdded then MessageDlg('Element added: ' + IntToStr(Item), mtInformation, [mbOK], 0) else if Action = cnRemoved then MessageDlg('Element removed: ' + IntToStr(Item), mtInformation, [mbOK], 0) else if Action = cnExtracted then MessageDlg('Element extracted: ' + IntToStr(Item), mtInformation, [mbOK], 0) end;
{ This example demostrates the methods to retreive of Run-time Type Information (RTTI) information. This example will print useful information about any given type including it's name, size, kind and sub-kind. } type TForm2 = class(TForm) Memo1: TMemo; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; { Defining a generic type that contains a single class (static) method that will print information about the type it operates on. } TGenericClass<T> = class(TList<T>) // TList is a class in Generics.Collections public procedure PrintTypeInfo(memo: TMemo); end; TGenericString = class(TGenericClass<UnicodeString>) end; TGenericByte = class(TGenericClass<Byte>) end; TGenericDouble = class(TGenericClass<Double>) end; var Form2: TForm2; implementation {$R *.dfm} { Method defintition } procedure TGenericClass<T>.PrintTypeInfo(memo: TMemo); var Info : PTypeInfo; Data : PTypeData; KindName : String; SubName : String; begin { Get type info for the "yet unknown type" } Info := System.TypeInfo(T); { There is no RTTI attached for some types like Records for example. } if Info <> nil then begin memo.Lines.Add('Type name: ' + Info^.Name); { Find out the name of an enum item from it's ordinal value } KindName := TypInfo.GetEnumName(System.TypeInfo(TTypeKind), Ord(Info^.Kind)); memo.Lines.Add('Type kind: ' + KindName); Data := GetTypeData(Info); if Info^.Kind = tkInteger then begin { In case of integer let's see the actual sub-type name } SubName := TypInfo.GetEnumName(System.TypeInfo(TOrdType), Ord(Data^.OrdType)); memo.Lines.Add('Integer kind: ' + SubName); end; if Info^.Kind = tkFloat then begin { In case of float let's see the actual sub-type name } SubName := TypInfo.GetEnumName(System.TypeInfo(TFloatType), Ord(Data^.FloatType)); memo.Lines.Add('Float kind: ' + SubName); end; if Info^.Kind = tkDynArray then begin { Let's check out the element size } memo.Lines.Add('Array element type name: ' + Data^.elType^^.Name); memo.Lines.Add('Array element type size: ' + IntToStr(Data^.elSize)); end; end; memo.Lines.Add('Size of type: ' + IntToStr(SizeOf(T))); end; procedure TForm2.FormCreate(Sender: TObject); var GenericString : TGenericClass<UnicodeString>; GenericByte : TGenericClass<Byte>; GenericDouble : TGenericClass<Double>; // GenericStringArray : TGenericClass<array of string>; begin { Prints type info for string type} GenericString := TGenericClass<UnicodeString>.Create; GenericString.PrintTypeInfo(Memo1); { Prints type info for Byte type} GenericByte := TGenericClass<Byte>.Create; GenericByte.PrintTypeInfo(Memo1); { Prints type info for Double type} GenericDouble := TGenericClass<Double>.Create; GenericDouble.PrintTypeInfo(Memo1); end; { Expected results are: Type name: string Type kind: tkUString Size of type: 4 Type name: Byte Type kind: tkInteger Integer kind: otUByte Size of type: 1 Type name: Double Type kind: tkFloat Float kind: ftDouble Size of type: 8 Type name: .1 Type kind: tkDynArray Array element type name: string Array element type size: 4 Size of type: 4 }
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|