zoukankan      html  css  js  c++  java
  • Delphi 2009 泛型容器单元(Generics.Collections)[4]: TDictionary<T>

    TDictionary 泛型字典,类似哈希表.

    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
      System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
      Vcl.StdCtrls;
    
    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses
      System.Generics.Collections;
    
        {定义一个泛型TDictionary类,指定由Cardinal,string构成}
    var
      Dictionary: TDictionary<Cardinal, string>;
    
    {建立}
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Key: Cardinal;
      Value: string;
      str: string;
      k, v: Boolean;
    begin
      Key := StrToIntDef(Edit1.Text, 0);
      Value := Edit2.Text;
      if Value = '' then
        Value := 'Null';
      k := Dictionary.ContainsKey(Key);     {Key 是否存在}
      v := Dictionary.ContainsValue(Value); {Value是否存在}
      if not k then
      begin
        Dictionary.Add(Key, Value);
        Memo1.Lines.Add(Format('%d=%s', [Key, Value])); {同步显示}
      end;
    
      if k and not v then
      begin
        str := Format('key 已存在: %d=%s; 是否修改其值?', [Key, Dictionary[Key]]);
        if MessageBox(0, PChar(str), '提示', MB_OKCANCEL or MB_ICONQUESTION) = mrOk then
        begin
          Dictionary.AddOrSetValue(Key, Value);       {也可使用上一句}
          Memo1.Lines.Values[IntToStr(Key)] := Value; {同步显示}
        end;
      end;
    
      if k and v then
      begin
        str := Format('%d=%s 已存在, 不能重复添加', [Key, Value]);
        MessageBox(0, PChar(str), '错误', MB_OK + MB_ICONHAND);
      end;
      Text := IntToStr(Dictionary.Count);
    
    end;
    
    {删除: Remove}
    procedure TForm1.Button2Click(Sender: TObject);
    var
      key: Integer;
      i: Integer;
    begin
      key := StrToIntDef(Edit1.Text, 0);
      if not Dictionary.ContainsKey(key) then
      begin
        ShowMessageFmt('key: %d 不存在', [key]);
        Exit;
      end;
    
      Dictionary.Remove(key);
      Text := IntToStr(Dictionary.Count);
    
       {同步显示}
      i := Memo1.Lines.IndexOfName(IntToStr(key));
      if i > -1 then
        Memo1.Lines.Delete(i);
    
    end;
    
    {尝试取值: TryGetValue}
    procedure TForm1.Button3Click(Sender: TObject);
    var
      Key: Integer;
      Value: string;
    begin
      Key := StrToIntDef(Edit1.Text, 0);
      if Dictionary.TryGetValue(Key, Value) then
        ShowMessageFmt('key: %d 已存在, 其值是: %s', [Key, Value])
      else
        ShowMessageFmt('key: %d 不存在', [Key]);
    end;
    {清空: Clear}
    
    procedure TForm1.Button4Click(Sender: TObject);
    begin
      Dictionary.Clear;
      Text := IntToStr(Dictionary.Count);
      Memo1.Clear; {同步显示}
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Dictionary := TDictionary<Cardinal, string>.Create;
      Memo1.Clear;
      Button1.Caption := Button1.Caption + ' 添加';
      Button2.Caption := Button2.Caption + ' 删除';
      Button3.Caption := Button3.Caption + ' 尝试取值';
      Button4.Caption := Button4.Caption + ' 清空';
    
      Edit1.Clear;
      Edit2.Clear;
      Edit1.NumbersOnly := True;
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      Dictionary.Free;
    end;
    
    end.
  • 相关阅读:
    storage存储对象和数组类型时候的问题
    关于vue-router路径配置的问题
    解决v-for产生的警告的办法
    网页调用打印机打印文件
    vue-router的link样式设置问题
    在vue项目当中使用sass
    使用正则获取地址栏参数的方法
    escape、encodeURI和encodeURIComponent的区别
    SQLServer安装错误之--->无法打开项 UNKNOWNComponentsDA42BC89BF25F5BD0AF18C3B9B1A1EE8c1c4f01781cc94c4c8fb1542c0981a2a
    软件收集网站!
  • 原文地址:https://www.cnblogs.com/redhat588/p/12765651.html
Copyright © 2011-2022 走看看