二十七、详测 Generics Collections TDictionary(3): TPairEnumerator、TKeyEnumerator、TValueEnumerator、ExtractPair
2009-11-11 14:26
399 查看
//这组功能没有多少实用价值
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Generics.Collections;
procedure TForm1.Button1Click(Sender: TObject);
var
Dictionary: TDictionary<string,Integer>;
ds: TDictionary<string,Integer>.TPairEnumerator;
ks: TDictionary<string,Integer>.TKeyEnumerator;
vs: TDictionary<string,Integer>.TValueEnumerator;
begin
Dictionary := TDictionary<string,Integer>.Create();
Dictionary.Add('n1', 111);
Dictionary.Add('n2', 222);
Dictionary.Add('n3', 333);
ds := Dictionary.GetEnumerator;
while ds.MoveNext do ShowMessageFmt('%s:%d', [ds.Current.Key, ds.Current.Value]);
{n2:222 n3:333 n1:111}
ks := Dictionary.Keys.GetEnumerator;
while ks.MoveNext do ShowMessageFmt('%s', [ks.Current]);
{n2 n3 n1}
vs := Dictionary.Values.GetEnumerator;
while vs.MoveNext do ShowMessageFmt('%d', [vs.Current]);
{222 333 111}
{ ExtractPair 应是提取元素, 但它的返回值有些问题; 该函数源码有待修改 }
Dictionary.ExtractPair('n1');
ShowMessage(IntToStr(Dictionary.Count)); {2}
Dictionary.Free;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Generics.Collections;
procedure TForm1.Button1Click(Sender: TObject);
var
Dictionary: TDictionary<string,Integer>;
ds: TDictionary<string,Integer>.TPairEnumerator;
ks: TDictionary<string,Integer>.TKeyEnumerator;
vs: TDictionary<string,Integer>.TValueEnumerator;
begin
Dictionary := TDictionary<string,Integer>.Create();
Dictionary.Add('n1', 111);
Dictionary.Add('n2', 222);
Dictionary.Add('n3', 333);
ds := Dictionary.GetEnumerator;
while ds.MoveNext do ShowMessageFmt('%s:%d', [ds.Current.Key, ds.Current.Value]);
{n2:222 n3:333 n1:111}
ks := Dictionary.Keys.GetEnumerator;
while ks.MoveNext do ShowMessageFmt('%s', [ks.Current]);
{n2 n3 n1}
vs := Dictionary.Values.GetEnumerator;
while vs.MoveNext do ShowMessageFmt('%d', [vs.Current]);
{222 333 111}
{ ExtractPair 应是提取元素, 但它的返回值有些问题; 该函数源码有待修改 }
Dictionary.ExtractPair('n1');
ShowMessage(IntToStr(Dictionary.Count)); {2}
Dictionary.Free;
end;
end.
相关文章推荐
- 详测 Generics Collections TDictionary(3): TPairEnumerator、TKeyEnumerator、TValueEnumerator、ExtractPair
- 详测 Generics Collections TDictionary(2): ContainsKey、ContainsValue、AddOrSetValue、TryGetValue
- 二十六、详测 Generics Collections TDictionary(2): ContainsKey、ContainsValue、AddOrSetValue、TryGetValue
- 详测 Generics Collections TDictionary(4): OnKeyNotify、OnValueNotify
- 二十八、详测 Generics Collections TDictionary(4): OnKeyNotify、OnValueNotify
- 五、Delphi 2009 泛型容器单元(Generics.Collections): TDictionary<T>
- 二十九、详测 Generics Collections TDictionary(5): 多种 Create 手段
- Delphi 2009 泛型容器单元(Generics.Collections)[4]: TDictionary<T>
- 详测 Generics Collections TDictionary(5): 多种 Create 手段
- 详测 Generics Collections TDictionary(1): Add、Items、Keys、Values、Remove
- Delphi 2009 泛型容器单元(Generics.Collections)[4]: TDictionary<T>
- 二十五、详测 Generics Collections TDictionary(1): Add、Items、Keys、Values、Remove
- Dictionary <(Of <(TKey, TValue>)>) Class Example
- 学用 ASP.Net 之 System.Collections.Specialized.NameValueCollection 类
- Dictionary通过下标获取key和value
- How to sort the dictionary by the value field
- 使用 python3.6 调用 jieba 库出现的 ValueError: dictionary file dict.txt must be utf-8 错误
- 关于NSMutableDictionary setValue:value forKey:key 以及 setobject:value forkey:key 的理解
- Unity3d中Dictionary和KeyValuePair的使用
- KeyValuePair VS DictionaryEntry