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)
        Memo1: TMemo;
        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;
      i: Integer;
    begin
      {设置动态数组大小}
      SetLength(arr, Memo1.Lines.Count);
    
      {复制 Memo1 中的文本到数组}
      for i := 0 to Memo1.Lines.Count - 1 do arr[i] := Memo1.Lines[i];
    
      {排序}
      TArray.Sort<string>(arr);
    
      {在 Memo1 中查看数组中的数据}
      Memo1.Clear;
      for i := 0 to Length(arr) - 1 do Memo1.Lines.Add(arr[i]);
    end;
    
    {给整数数组排序}
    procedure TForm1.Button2Click(Sender: TObject);
    const
      num = 10;
    var
      arr: array of Integer;
      i: Integer;
    begin
      SetLength(arr, num);
    
      Randomize;
      Memo1.Clear;
    
      {把 10 个随机数放入数组, 并显示在 Memo1 中}
      for i := 0 to num - 1 do
      begin
        arr[i] := Random(100);
        Memo1.Lines.Add(IntToStr(arr[i]));
      end;
    
      {排序}
      TArray.Sort<Integer>(arr);
    
      {等 1 秒中后查看排序结果}
      Sleep(1000);
      Memo1.Clear;
      for i := 0 to num - 1 do Memo1.Lines.Add(IntToStr(arr[i]));
    end;
    
    end.
    
  • 相关阅读:
    从无到有制作Deb包的一个实例
    Redis内存存储结构分析
    岳麓实践论
    金砖四国(巴西、俄罗斯、印度和中国)
    用LLVM开发新语言
    QQ云输入法
    http://baike.baidu.com/view/1926473.htm
    21世纪商业评论
    update ubuntu to 11.10
    gnu make 中文手册教程pdf
  • 原文地址:https://www.cnblogs.com/del/p/1579897.html
Copyright © 2011-2022 走看看