Just as Object Pascal has a library of standard procedures and functions, it also has a library of standard classes. Many of these standard classes are contained in the unit Classes. We will not discuss them all here, but we will slowly introduce them over time throughout this book. These standard classes will prove incredibly useful for interacting with the file system and operating system and later for creating graphical user interfaces (GUIs). For now, we introduce the TList class. TList is very similar to a dynamic array except that it is more coder-friendly. In other words, the methods it introduces will greatly reduce code we would have to write to achieve the same functionality from dynamic arrays. The following code is an example of using a TList object to store references to other objects such as TRectangle or TCircle (which we have defined in a previous section):
var
  List : TList;
begin
  List := TList.Create;
  List.Add(TRectangle.Create);
  List.Add(TCircle.Create);
  List.Add(TRectangle.Create);
  Writeln(List.Count);
end.
In this example, we add three shape objects to the list. The Count field returns the number of items in the array. The code above is equivalent to creating a dynamic array of length 3 and assigning the objects to this array:
var
  List : array of TObject;
begin
  SetLength(List,3);
  List[0] := TRectangle.Create;
  List[1] := TCircle.Create;
  List[2] := TRectangle.Create;
  Writeln(High(List)+1);
end.
The first main difference, however, is that we do not need to know how many items we are adding to TList when we create it. In a dynamic array, we either have to know how many items we're adding to it when we create it or we have to manually resize it. TList handles resizing internally. In other words, it has encapsulated resizing of its array so the coder does not have to deal with it. TList has a number of other useful functions listed in the table below (there are more, but this is sufficient for now). You can refer back to this table in future sections:
Method Definition
Description
Add(Item)
Adds something to the end of the list. Item must be a memory reference.
Clear
Clears the list.
Delete(Index : Integer)
Removes the item at position Index.
Exchange(Index1,Index2 : Integer)
Swaps two items in the list.
IndexOf(Item) : Integer
Returns the index of the item if it is in the list. Otherwise it returns -1.
Insert(Index : Integer; Item) : Integer
Inserts an item into the list at the position specified by Index.
Pack
Removes all nil references from the list.