Pages

Tuesday 11 March 2014

How to use Generic TList Class in Delphi?

How to use Generic TList Class in Delphi?

TList generic class is used to store a list of various data type variables in Delphi. Below is the simple program to illustrate the concept of TList generic class in Delphi.

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]);

  writeln('Index of first 1 is ' + IntToStr(List.IndexOf(1)));
  writeln('Index of last 1 is ' + IntToStr(List.LastIndexOf(1)));
  writeln('Does List contains element 100? ' + BoolToStr(List.Contains(100)));

  { Add another element to the list. }
  List.Add(100);

  writeln('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;
  writeln('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
    writeln('Found element 6 at index ' + IntToStr(FoundIndex));

  { Reverse the list. }
  List.Reverse;
  writeln('The element on position 0 is ' + IntToStr(List.Items[0]));
  List.Free;
  readln;

end.

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.