Generics Collections TArray (Delphi)
Description
This example demonstrates the usage of the TArray static functions.
Code
var
I, FoundIndex: Integer;
Arr: array of String;
begin
{ Initialize the random number generator }
Randomize;
{ Set array length to 9 }
SetLength(Arr, 9);
{ Populate the string array with random numbers }
for I := 0 to 9 do
begin
Arr[I] := Format('%d', [Random(10)]);
end;
{ Sort the string array }
TArray.Sort<String>(Arr, TStringComparer.Ordinal);
I := Random(10);
{ Look for I in the Arr string array. }
if TArray.BinarySearch<String>(Arr, I.ToString, FoundIndex,
TStringComparer.Ordinal) then
begin
// Element I was found. Show a message.
writeln(Format('Element %d was found in the list at index %d!',
[I, FoundIndex]));
end
else
begin
// Element I was not found. Show a message.
writeln('Element was not found in the list!');
end;
readln;
end.