Delphi XE 泛型使用全集队 栈 字典 列表 对象列表.docx
《Delphi XE 泛型使用全集队 栈 字典 列表 对象列表.docx》由会员分享,可在线阅读,更多相关《Delphi XE 泛型使用全集队 栈 字典 列表 对象列表.docx(29页珍藏版)》请在冰豆网上搜索。
DelphiXE泛型使用全集队栈字典列表对象列表
详测GenericsCollectionsTQueue
usesGenerics.Collections;
procedureTForm1.Button1Click(Sender:
TObject);
var
Queue:
TQueue;
s,str:
string;
List:
TList;
begin
Queue:
=TQueue.Create();
{入列}
Queue.Enqueue('AAA');
Queue.Enqueue('BBB');
Queue.Enqueue('CCC');
Queue.Enqueue('DDD');
{查看}
str:
='';
forsinQueuedostr:
=str+s+'';
ShowMessage(str);{AAABBBCCCDDD}
{出列,并查看出列元素}
ShowMessage(Queue.Dequeue);{AAA}
str:
='';
forsinQueuedostr:
=str+s+'';
ShowMessage(str);{BBBCCCDDD}
{查看下一个将要出列的是...}
ShowMessage(Queue.Peek);{BBB}
Queue.Free;
//从TList建立TQueue
List:
=TList.Create();
List.AddRange(['AA','BB','CC']);
Queue:
=TQueue.Create(List);
str:
='';
forsinQueuedostr:
=str+s+'';
ShowMessage(str);{AABBCC}
ShowMessage(IntToStr(Queue.Count));{3}
Queue.Clear;
ShowMessage(IntToStr(Queue.Count));{0}
//Queue.TrimExcess;{TrimExcess方法在Queue好像没有存在的意义}
List.Free;
Queue.Free;
end;
详测GenericsCollectionsTQueue(3):
OnNotify、Extract
//usesGenerics.Collections;
{准备给TQueue.OnNotify调用的事件过程}
procedureTForm1.MyQueueNotify(Sender:
TObject;constItem:
Integer;
Action:
TCollectionNotification);
begin
caseActionof
cnAdded :
ShowMessageFmt('Add:
%d',[Item]);
cnRemoved :
ShowMessageFmt('Remove:
%d',[Item]);
cnExtracted:
ShowMessageFmt('Extract:
%d',[Item]);
end;
end;
procedureTForm1.Button1Click(Sender:
TObject);
var
Queue:
TQueue;
begin
Queue:
=TQueue.Create();
Queue.OnNotify:
=MyQueueNotify;
Queue.Enqueue(11);{Add:
11}
Queue.Enqueue(22);{Add:
22}
Queue.Enqueue(33);{Add:
33}
Queue.Dequeue; {Remove:
11}
//Extract和Dequeue功能一致,区别只是在驱动OnNotify事件时传递的参数有区别,没多大意义
Queue.Extract; {Extract:
22}
//Queue.OnNotify:
=nil;
Queue.Free; {Remove:
33}
end;
详测GenericsCollectionsTStack
(1):
Push、Pop、Peek-其他功能同TQueue
usesGenerics.Collections;
procedureTForm1.Button1Click(Sender:
TObject);
var
Stack:
TStack;
s,str:
string;
begin
Stack:
=TStack.Create();
{压栈}
Stack.Push('AAA');
Stack.Push('BBB');
Stack.Push('CCC');
str:
='';
forsinStackdostr:
=str+s+'';
ShowMessage(str);{AAABBBCCC}
{出栈:
后进的先出}
Stack.Pop;
str:
='';
forsinStackdostr:
=str+s+'';
ShowMessage(str);{AAABBB}
{下一个将要出栈的...}
ShowMessage(Stack.Peek);{BBB}
Stack.Free;
end;
详测GenericsCollectionsTDictionary
(1):
usesGenerics.Collections;
procedureTForm1.Button1Click(Sender:
TObject);
var
Dictionary:
TDictionary;
K:
string;
V:
Integer;
str:
string;
b:
Boolean;
T:
Integer;
ds:
TDictionary.TPairEnumerator;
ks:
TDictionary.TKeyEnumerator;
vs:
TDictionary.TValueEnumerator;
begin
Dictionary:
=TDictionary.Create();
{添加}
Dictionary.Add('n1',111);
Dictionary.Add('n2',222);
Dictionary.Add('n3',333);
{访问}
ShowMessage(IntToStr(Dictionary['n2'])); {222}
ShowMessage(IntToStr(Dictionary.Items['n2']));{222}
{遍历Keys}
str:
='';
forKinDictionary.Keysdostr:
=str+K+'';
ShowMessage(str);{n2n3n1}//顺序乱了?
{遍历Values}
str:
='';
forVinDictionary.Valuesdostr:
=str+IntToStr(V)+'';
ShowMessage(str);{222333111}
{通过Keys遍历Values}
str:
='';
forKinDictionary.Keysdostr:
=str+IntToStr(Dictionary[K])+'';
ShowMessage(str);{222333111}
{删除}
Dictionary.Remove('n1');
str:
='';
forKinDictionary.Keysdostr:
=str+IntToStr(Dictionary[K])+'';
ShowMessage(str);{222333}
{取数量、清空}
ShowMessage(IntToStr(Dictionary.Count));{2}
Dictionary.Clear;
ShowMessage(IntToStr(Dictionary.Count));{0}
{判断指定的Key是否存在}
b:
=Dictionary.ContainsKey('n1');
ShowMessage(BoolToStr(b,True)); {True}
b:
=Dictionary.ContainsKey('n4');
ShowMessage(BoolToStr(b,True)); {False}
{判断指定的Value是否存在}
b:
=Dictionary.ContainsValue(111);
ShowMessage(BoolToStr(b,True)); {True}
b:
=Dictionary.ContainsValue(999);
ShowMessage(BoolToStr(b,True)); {False}
{使用AddOrSetValue时,如果Key存在则替换值;此时如果用Add将发生异常}
Dictionary.AddOrSetValue('n1',123);
ShowMessage(IntToStr(Dictionary['n1']));{123}
{使用AddOrSetValue时,如果Key不存在则同Add}
Dictionary.AddOrSetValue('n4',444);
ShowMessage(IntToStr(Dictionary['n4']));{444}
{尝试取值}
ifDictionary.TryGetValue('n2',T)then
ShowMessage(IntToStr(T));{222}
ds:
=Dictionary.GetEnumerator;
whileds.MoveNextdoShowMessageFmt('%s:
%d',[ds.Current.Key,ds.Current.Value]);
{n2:
222 n3:
333 n1:
111}
ks:
=Dictionary.Keys.GetEnumerator;
whileks.MoveNextdoShowMessageFmt('%s',[ks.Current]);
{n2 n3 n1}
vs:
=Dictionary.Values.GetEnumerator;
whilevs.MoveNextdoShowMessageFmt('%d',[vs.Current]);
{222 333 111}
{ExtractPair应是提取元素,但它的返回值有些问题;该函数源码有待修改}
Dictionary.ExtractPair('n1');
ShowMessage(IntToStr(Dictionary.Count));{2}
Dictionary.Free;
end;
详测GenericsCollectionsTDictionary(4):
OnKeyNotify、OnValueNotify
interface
uses
Generics.Collections;
implementation
procedureTForm1.KeyNotify(Sender:
TObject;constItem:
string;
Action:
TCollectionNotification);
begin
caseActionof
cnAdded :
ShowMessageFmt('Key_Add:
%s',[Item]);
cnRemoved :
ShowMessageFmt('Key_Remove:
%s',[Item]);
cnExtracted:
ShowMessageFmt('Key_Extract:
%s',[Item]);
end;
end;
procedureTForm1.ValueNotify(Sender:
TObject;constItem:
Integer;
Action:
TCollectionNotification);
begin
caseActionof
cnAdded :
ShowMessageFmt('Value_Add:
%d',[Item]);
cnRemoved :
ShowMessageFmt('Value_Remove:
%d',[Item]);
cnExtracted:
ShowMessageFmt('Value_Extract:
%d',[Item]);
end;
end;
procedureTForm1.Button1Click(Sender:
TObject);
var
Dictionary:
TDictionary;
begin
Dictionary:
=TDictionary.Create();
Dictionary.OnKeyNotify:
=KeyNotify;
Dictionary.OnValueNotify:
=ValueNotify;
Dictionary.Add('n1',111);{Key_Add:
n1; Value_Add:
111}
Dictionary.Add('n2',222);{Key_Add:
n2; Value_Add:
222}
Dictionary.AddOrSetValue('n1',123);{Value_Remove:
111; Value_Add:
123}
Dictionary.Remove('n1'); {Key_Remove:
n1; Value_Remove:
111}
Dictionary.ExtractPair('n2');{Key_Extract:
n2; Value_Extract:
222}
Dictionary.OnKeyNotify:
=nil;
Dictionary.OnValueNotify:
=nil;
Dictionary.Free;
end;
详测GenericsCollectionsTDictionary(5):
多种Create手段
usesGenerics.Collections,Generics.Defaults;
//Create可以指定元素数,这样可以提前分配空间以加快速度
procedureTForm1.Button1Click(Sender:
TObject);
var
Dictionary:
TDictionary;
begin
Dictionary:
=TDictionary.Create(3);
Dictionary.Add('n1',111);
Dictionary.Add('n2',222);
Dictionary.Add('n3',333);
Dictionary.Free;
end;
//Create可以有一个IEqualityComparer参数,用于判断Key怎样才是相同
procedureTForm1.Button2Click(Sender:
TObject);
var
Dictionary:
TDictionary;
EqualityComparer:
IEqualityComparer;{相等对比器}
begin
{通过IEqualityComparer让TDictionary的Key忽略大小写}
EqualityComparer:
=TEqualityComparer.Construct(
function(constLeft,Right:
string):
Booleanbegin
Result:
=LowerCase(Left)=LowerCase(Right);
end,
function(constValue:
string):
Integerbegin
Result:
=StrToIntDef(Value,0);{我暂时不知道这个函数的作用,随便写的}
end
);
Dictionary:
=TDictionary.Create(EqualityComparer);
Dictionary.Add('n1',111);
Dictionary.Add('n2',222);
Dictionary.Add('n3',333);
{如果不是如上建立,下面这句将会产生一个新元素,而不是更新n1的值}
Dictionary.AddOrSetValue('N1',123);
ShowMessage(IntToStr(Dictionary['n1']));{123}
Dictionary.Free;
end;
//Create可以同时指定上面两个参数
procedureTForm1.Button3Click(Sender:
TObject);
var
Dictionary:
TDictionary;
EqualityComparer:
IEqualityComparer;
begin
EqualityComparer:
=TEqualityComparer.Construct(
function(constLeft,Right:
string):
Booleanbegin
Result:
=LowerCase(Left)=LowerCase(Right);
end,
function(constValue:
string):
Integerbegin
Result:
=StrToIntDef(Value,0);
end
);
Dictionary:
=TDictionary.Create(9,EqualityComparer);
Dictionary.Add('n1',111);
Dictionary.AddOrSetValue('N1',123);
ShowMessage(IntToStr(Dictionary['n1']));{123}
{指定元素数后,用不了的可以释放}
Dictionary.TrimExcess;
Dictionary.Free;
end;
//可通过另一个TDictionary建立一个新的TDictionary
procedureTForm1.Button4Click(Sender:
TObject);
var
Dictionary,DictionaryTmp:
TDictionary;
pair:
TPair;
begin
DictionaryTmp:
=TDictionary.Create();
DictionaryTmp.Add('n1',111);
DictionaryTmp.Add('n2',222);
DictionaryTmp.Add('n3',333);
{通过另一个TDictionary建立}
Dictionary:
=TDictionary.Create(DictionaryTmp);
{遍历看看}
forpairinDictionarydoShowMessage(IntToStr(Dictionary[pair.Key]));
{222333111}
DictionaryTmp.Free;
Dictionary.Free;
end;
//通过另一个TDictionary建立时,可同时再指定"相等对比器"
procedureTForm1.Button5Click(Sender:
TObject);
var
Dictionary,DictionaryTmp:
TDictionary;
pair:
TPair;
EqualityComparer:
IEqualityComparer;
begin
DictionaryTmp:
=TDictionary.Create();
DictionaryTmp.Add('n1',111);
DictionaryTmp.Add('n2',222);
DictionaryTmp.Add('n3',333);
{再做个对比器}
EqualityComparer:
=TEqualityComparer.Construct(
function(constLeft,Right:
string):
Booleanbegin
Result:
=LowerCase(Left)=LowerCase(Right);
end,
function(constValue:
string):
Integerbegin
Result:
=