8. Use the generic tarray to find the specified element from the dynamic array

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.