Make full use of generic Arrays

Source: Internet
Author: User
It took a week to solve a problem. At the same time, the first time I really started to use generic arrays, it felt great.

If you use tarray <word> instead of array of word, you can also useAlgorithm(Point missing ).

Uses generics. collections, generics. defaults; {sort method for testing tarray} procedure tform1.button1click (Sender: tobject); var arr: tarray <string>; // same as array of string S: string; begin setlength (ARR, 5); arr [0]: = 'aaa'; arr [1]: = 'aaa'; arr [2]: = '000000'; arr [3]: = '000000'; arr [4]: = '000000'; tarray. sort <string> (ARR); memo1.clear; for S in arr do memo1.lines. add (s); // 111 222 333 AAA aaaend; {test binarysearch method of tarray} procedure tform1.button2click (Sender: tobject); var arr: tarray <integer>; // same as array of integer I, n: integer; begin setlength (ARR, 5); for I: = 0 to length (ARR)-1 do arr [I]: = INTEGER (sqr (I); memo1.clear; for I: = low (ARR) to high (ARR) Do memo1.lines. add (inttostr (ARR [I]); If tarray. binarysearch <integer> (ARR, 4, N) Then showmessage (inttostr (n); // 2, that is, the third if tarray. binarysearch <integer> (ARR, 5, n) Then showmessage (inttostr (n); // end cannot be determined based on N when it is not found; {custom sorter} procedure tform1.button3click (Sender: tobject); var arr: tarray <integer>; num: integer; begin setlength (ARR, 5); arr [0]: = 2; arr [1]: = 4; arr [2]: = 3; arr [3]: = 1; arr [4]: = 5; tarray. sort <integer> (ARR, tcomparer <integer>. construct (function (const A, B: integer): integer begin result: = B-A; End); memo1.clear; For num in arr do memo1.lines. add (inttostr (Num); // 5 4 3 2 1end;

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.