PAS
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;
Type
Tform1 = Class (tform)
Button1: tbutton;
Button2: tbutton;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Uses generics. collections; {reference generic units}
{Search from String Array}
Procedure tform1.button1click (Sender: tobject );
VaR
Arr: array of string;
Num: integer;
Begin
{Construct a dynamic array}
Setlength (ARR, 5 );
Arr [0]: = 'aaa ';
Arr [1]: = 'bbb ';
Arr [2]: = 'ccc ';
Arr [3]: = 'ddd ';
Arr [4]: = 'Eee ';
{Search}
Tarray. binarysearch <string> (ARR, 'ddd ', num );
{Show search result}
If num <length (ARR) Then {If num = length (ARR) is not found}
Showmessage (inttostr (Num); {3}
End;
{Search from integer array}
Procedure tform1.button2click (Sender: tobject );
VaR
Arr: array of integer;
Num: integer;
Begin
{Construct a dynamic array}
Setlength (ARR, 5 );
Arr [0]: = 11;
Arr [1]: = 22;
Arr [2]: = 33;
Arr [3]: = 44;
Arr [4]: = 55;
{Search}
Tarray. binarysearch <integer> (ARR, 44, num );
{Show search result}
If num <length (ARR) Then {If num = length (ARR) is not found}
Showmessage (inttostr (Num); {3}
End;
End.