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

delphi – 在创建示例代码期间填充的TDictionary

发布时间:2020-12-15 09:05:07 所属栏目:大数据 来源:网络整理
导读:有没有人有TDictionary TKey,TValue的示例代码?在构造函数中填充? 解决方法 您需要调用接收类型为TEnumerable TPair TKey,TValue类型的Collection参数的 dictionary constructor overload. 例如,假设我们有TDictionary string,Integer.然后我们可以向构造
有没有人有TDictionary< TKey,TValue>的示例代码?在构造函数中填充?

解决方法

您需要调用接收类型为TEnumerable< TPair< TKey,TValue>>类型的Collection参数的 dictionary constructor overload.

例如,假设我们有TDictionary< string,Integer>.然后我们可以向构造函数传递一个TEnumerable< TPair< string,Integer>>的实例.这种事的一个例子是TList<TPair<string,Integer>>.

List := TList<TPair<string,Integer>>.Create;
List.Add(TPair<string,Integer>.Create('Foo',42));
List.Add(TPair<string,Integer>.Create('Bar',666));
Dictionary := TDictionary<string,Integer>.Create(List);

这是非常笨拙的,你永远不会喜欢这个选项而不是简单的Create,然后是一系列对Add的调用.如果你碰巧手边有一个现成的集合,你只能使用传入现有集合的选项.

从TEnumerable< T>派生的类的另一个例子.是TDictionary本身:

type
  TDictionary<TKey,TValue> = class(TEnumerable<TPair<TKey,TValue>>)

因此,如果您已经有一个字典实例,则可以创建另一个实例并使用第一个实例的内容对其进行初始化:

Dict2 := TDictionary<string,Integer>.Create(Dict1);

(编辑:李大同)

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

    推荐文章
      热点阅读