这可能是一个非常简单的问题,但我认为我是故意盲目的东西; 如何通过Delphi(10.1)中的Dictionary类中的索引获取密钥.我的意思是结构有一个名为Count的属性,所以它必须有一些数组或列表,为什么我不能通过索引得到键.
我也在Dictionary类中尝试过KeyCollection属性,但它也没有任何用处.我需要这样的东西:
key: string;
key := dicTest.GetKey(keyIndex);
Run Code Online (Sandbox Code Playgroud)
非常感谢.
Delphi RTL通用字典是无序的.由于无序,容器中的项目不具有有意义的索引.
可以使用Keys属性枚举键:
var
dict: TDictionary<string, Integer>;
key: string;
....
for key in dict.Keys do
Writeln(key);
Run Code Online (Sandbox Code Playgroud)
同样,可以使用Values属性枚举值.
var
dict: TDictionary<string, Integer>;
value: Integer;
....
for value in dict.Values do
Writeln(value);
Run Code Online (Sandbox Code Playgroud)
如果你想枚举键/值对,那么字典本身就为它提供了一个枚举器:
var
dict: TDictionary<string, Integer>;
item: TPair<string, Integer>;
....
for item in dict do
Writeln(item.Key, ', ', item.Value);
Run Code Online (Sandbox Code Playgroud)
请注意,对于这些枚举器中的每一个,都不保证项目的顺序.简单的行为,例如向字典中添加新项,可能会导致枚举项的顺序发生变化.
| 归档时间: |
|
| 查看次数: |
731 次 |
| 最近记录: |