Generics Collections TArray (Delphi)

From RAD Studio XE3 Code Examples
Jump to: navigation, search

Language:

Description

This example demonstrates the usage of the TArray static functions. The example requires a memo, a button, and an edit box.

Code

procedure TForm3.Button1Click(Sender: TObject);
var
  I, FoundIndex: Integer;
  Arr: array of String;
begin
  { Populate an array with the items in the memo. }
  SetLength(Arr, InMemo.Lines.Count);
 
  for I := 0 to InMemo.Lines.Count - 1 do
    Arr[I] := InMemo.Lines[I];
 
  { Sort the array. }
  TArray.Sort<String>(Arr, TStringComparer.Ordinal);
 
  { Binary search the required value. }
  if TArray.BinarySearch<String>(Arr, Edit1.Text, FoundIndex, TStringComparer.Ordinal) then
  begin
    { Element was found. Show a message. }
    MessageDlg('Element "' + Edit1.Text + '" was found in the list!', mtInformation, [mbOK], 0);
  end else
  begin
    { Element was not found. Show a message. }
    MessageDlg('Element "' + Edit1.Text + '" was not found in the list!', mtError, [mbOK], 0);
  end;
end;

Uses

Personal tools