zoukankan      html  css  js  c++  java
  • 使用泛型的 TArray 从动态数组中查找指定元素

    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; {引用泛型单元}
    
    {从字符串数组中查找}
    procedure TForm1.Button1Click(Sender: TObject);
    var
      arr: array of string;
      num: Integer;
    begin
      {构建动态数组}
      SetLength(arr, 5);
      arr[0] := 'aaa';
      arr[1] := 'bbb';
      arr[2] := 'ccc';
      arr[3] := 'ddd';
      arr[4] := 'eee';
    
      {查找}
      TArray.BinarySearch<string>(arr, 'ddd', num);
    
      {显示查找结果}
      if num < Length(arr) then {如果 num = Length(arr) 就是没找到}
        ShowMessage(IntToStr(num)); {3}
    end;
    
    {从整数数组中查找}
    procedure TForm1.Button2Click(Sender: TObject);
    var
      arr: array of Integer;
      num: Integer;
    begin
      {构建动态数组}
      SetLength(arr, 5);
      arr[0] := 11;
      arr[1] := 22;
      arr[2] := 33;
      arr[3] := 44;
      arr[4] := 55;
    
      {查找}
      TArray.BinarySearch<Integer>(arr, 44, num);
    
      {显示查找结果}
      if num < Length(arr) then {如果 num = Length(arr) 就是没找到}
        ShowMessage(IntToStr(num)); {3}
    end;
    
    end.
    
  • 相关阅读:
    maxProfit2
    maxProfit
    getRow
    generate
    hasPathSum
    minDepth
    isBalanced
    sortedArrayToBST
    不得不学的 TCP三次握手和四次挥手
    java生态框架、中间件该有的都有了
  • 原文地址:https://www.cnblogs.com/del/p/1579913.html
Copyright © 2011-2022 走看看