Reprinted from: http://blog.csdn.net/iseekcode/article/details/4922001
A new CONTNRS unit has been added to the VCL from Delphi5, with 8 new classes defined in the cell, all of which are standard-based tlist classes.
TList
The Tlist class is actually a container class that can store pointers, provides a series of methods and properties to add, delete, rearrange, locate, access, and sort the containers in the container, which is an array-based mechanism for implementing the container, comparing vectors in C + + and ArrayList in Java.
Tlist is often used to hold a list of objects, based on the array implementation mechanism, which makes it very fast to access objects in the container with subscript, but as the object of the container increases, the insertion and deletion of objects decreases linearly, so it is not suitable for applications that frequently add and delete objects.
The following is a description of the properties and methods of the Tlist class:
- Count:integer; Property. Returns the number of items in the list.
- Items[index:integer]: Pointer; The default property. The items in the list are accessed directly from the index with a base of 0.
- ADD (item:pointer): Integer; Function. Used to add pointers to the list.
- Clear; Process. Empties the items in the list.
- Delete (Index:integer); Process. Delete the item in the list that corresponds to the index.
- IndexOf (item:pointer): Integer; Function. Returns the index of the pointer in the list.
- Remove (item:pointer): integer; Function. Removes a pointer from the list.
- Capacity:integer. Property. Can be used to get or set the number of pointers the list can hold.
- Extract (item:pointer): Pointer; Function. Extract is similar to remove, which removes the pointer from the list, but returns the deleted pointer.
- Exchange (Index1, Index2:integer); Process. Two pointers in the interchange list.
- First:pointer; Function. Returns the first pointer in a linked list.
- Last:pointer; Function. Returns the last pointer in the linked list.
- Move (Curindex, Newindex:integer); Process. Moves the pointer from the current position to the new location.
- Pack; Process. Removes all nil pointers from the list.
- Sort (Compare:tlistsortcompare); Process. To sort items in a linked list, you can set the Compare parameter to a user-defined sort function.
TObjectList
The TObjectList class inherits directly from the Tlist class and can be used as a container for objects. The TObjectList class is defined as follows:
TObjectList = Class (TList) ... public constructor Create; overload; Constructor Create (Aownsobjects:boolean); overload; function Add (aobject:tobject): Integer; function Remove (aobject:tobject): Integer; function IndexOf (aobject:tobject): Integer; function findinstanceof (aclass:tclass; Aexact:boolean=true; astartat:integer=0): Integer; Procedure Insert (Index:integer; Aobject:tobject); Property Ownsobjects:boolean; Property Items[index:integer]: TObject; Defaultend;
Unlike the Tlist class, the Add, Remove, IndexOf, and insert methods of the TObjectList class all need to pass TObject objects as parameters, which makes tobjectlist more suitable for saving objects than tlist because of the strong type checking of the compile period.
Additionally, the TObjectList object has the Ownsobjects property. When set to True (the default value), the same as the Tlist class, the TObjectList object destroys any objects that are removed from the list.
With the TObjectList class, we no longer have to use loops to release objects. This avoids the memory leaks caused by forgetting to release the objects in the linked list when the list object is released.
It is also important to note that the Ownsobjects property does not affect the Extract method, TObjectList the Extract method behaves like tlist, and knowledge removes the object reference from the list without destroying the object.
The TObjectList object also provides a findinstanceof function that returns an index of an object of only the specified object type in the list. If the Aextract parameter is true, only an object instance of the specified object type is positioned, and if the Aextract object is False,aclass, the subclass instance is also determined. The Astartat parameter can be used to find more than one instance of the list, as long as each call to the Findinstanceof function, the starting index plus 1, you can navigate to the next object, the way findinstanceof returns-1. Here is the sample code:
var idx:integer;begin idx:=-1; Repeat idx:= object.findinstanceof (Tmyobject, True, idx+1); If Idx>=0 then ... Until (idx<0); end;
TComponentList
The Tcompontentlist class is also defined in the Contnrs unit, and the class is defined as follows
TComponentList = Class (TObjectList) ... public function Add (acomponent:tcomponent): Integer; function Remove (acomponent:tcomponent): Integer; function IndexOf (acomponent:tcomponent): Integer; Procedure Insert (Index:integer; acomponent:tcomponent); Property Items[index:integer]: tcomponent; Default;end;
Note that TComponentList is inherited from the TObjectList class, and its add, Remove, IndexOf, insert, and items method calls all use the Tcomponent type parameter instead of the TObject type. It is therefore suitable as a container for tcomponent objects.
The TComponentList class also has a special feature, that is, if a component in the list is freed, it is automatically removed from the TComponentList list. This is done by using the Tcomponent Freenotification method to notify the linked list when the component is destroyed, so that the linked list can remove the object reference from the linked list.
Tclasslist class
The Tclasslist class is also defined in the Contnrs unit, and the class is defined as follows
Tclasslist = Class (TList) protected function GetItems (index:integer): Tclass; Procedure Setitems (Index:integer; Aclass:tclass);p ublic function Add (aclass:tclass): Integer; function Remove (aclass:tclass): Integer; function IndexOf (aclass:tclass): Integer; Procedure Insert (Index:integer; aclass:tclass); Property Items[index:integer]: Tclass read GetItems write Setitems; Default;end;
Unlike the previous two classes, this class inherits from Tlist, except that the arguments for add, Remove, indexof, and items are changed from the pointer to the Tclass meta class type
Delphi Container Class---TList, TObjectList, tcomponentlist, tclasslist use