加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi从TObjectDictionary中提取密钥

发布时间:2020-12-15 04:33:35 所属栏目:大数据 来源:网络整理
导读:在此问题上分享代码作为参考: Delphi TPair Exception 如何在不使用TPair的情况下从TObjectDictionary具体条目中检索键和值,并且不从列表中提取/删除/删除该对? {$APPTYPE CONSOLE}uses SysUtils,Generics.Defaults,Generics.Collections;type TProduct =
在此问题上分享代码作为参考: Delphi TPair Exception

如何在不使用TPair的情况下从TObjectDictionary具体条目中检索键和值,并且不从列表中提取/删除/删除该对?

{$APPTYPE CONSOLE}

uses
  SysUtils,Generics.Defaults,Generics.Collections;

type
  TProduct = class
  private
    FName: string;
    procedure SetName(const Value: string);
  published
  public
    property Name: string read FName write SetName;
  end;

type
  TListOfProducts = TObjectDictionary<TProduct,Integer>;

{ TProduct }

procedure TProduct.SetName(const Value: string);
begin
  FName := Value;
end;


var
  MyDict: TListOfProducts;
  MyProduct1: TProduct;
  MyProduct2: TProduct;
  MyProduct3: TProduct;
  APair: TPair<TProduct,Integer>;
  aKey: string;

begin
  try
    MyDict := TListOfProducts.Create([doOwnsKeys]);
    MyProduct1 := TProduct.Create;
    MyProduct1.Name := 'P1';
    MyProduct2 := TProduct.Create;
    MyProduct2.Name := 'P2';
    MyProduct3 := TProduct.Create;
    MyProduct3.Name := 'P3';

    MyDict.Add(MyProduct1,1);
    MyDict.Add(MyProduct2,2);
    MyDict.Add(MyProduct3,3);

    //the code to look for a **concrete product** (ie: MyProduct1) goes here..

    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName,': ',E.Message);
  end;
end.

谢谢.

=========================

=代码与答案=

{$APPTYPE CONSOLE}

uses
  SysUtils,Integer>;

{ TProduct }

procedure TProduct.SetName(const Value: string);
begin
  FName := Value;
end;


var
  MyDict: TListOfProducts;
  MyProduct1: TProduct;
  MyProduct2: TProduct;
  MyProduct3: TProduct;

  MySearchedProduct: TProduct;   // From Answer.

  APair: TPair<TProduct,Integer>;
  aProductName: string;

begin
  try
    MyDict := TListOfProducts.Create([doOwnsKeys]);
    MyProduct1 := TProduct.Create;
    MyProduct1.Name := 'P1';
    MyProduct2 := TProduct.Create;
    MyProduct2.Name := 'P2';
    MyProduct3 := TProduct.Create;
    MyProduct3.Name := 'P3';

    MyDict.Add(MyProduct1,3);

    Writeln('Enter the Product Name to search: ');

    //the code to look for a **concrete product** goes here..
    Readln(aProductName);
    for MySearchedProduct in Mydict.Keys do
      if (MySearchedProduct.Name = aProductName) then
        break;
    if MySearchedProduct.Name = aProductName then
      WriteLn('I have found the product: ' + MySearchedProduct.Name)
    else
      WriteLn('I have not found a product with that name.');

    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName,E.Message);
  end;
end.

解决方法

您可以使用MyDict的“键和值”属性.

在这样的循环中:

var
  MyProduct: TProduct;
  Value: Integer;
begin

  for Value in MyDict.Values do
    writeln(Value);

  for MyProduct in MyDict.Keys do
    writeln(MyProduct.Name);

或者使用ToArray的索引:

writeln(MyDict.Keys.ToArray[1].Name);
writeln(MyDict.Values.ToArray[1]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读