哪些列表可以用作临时列表? [英] Which lists could serve as temporary lists?
问题描述
在使用仅用作临时容器的项目列表时,您会建议我使用哪种列表类型?
When working with lists of items where the lists just serve as a temporary container - which list types would you recommend me to use?
我
- 不想手动销毁列表
- 想要使用内置列表类型(无框架,库等)
- 想要泛型
- don't want to destroy the list manually
- would like to use a built-in list type (no frameworks, libraries, ...)
- want generics
在不引起泄漏的情况下使之成为可能的事情:
Something which would make this possible without causing leaks:
function GetListWithItems: ISomeList;
begin
Result := TSomeList.Create;
// add items to list
end;
var
Item: TSomeType;
begin
for Item in GetListWithItems do
begin
// do something
end;
end;
我有什么选择?这是关于Delphi 2009的信息,但是出于知识的考虑,还请提及2010+年在这方面是否有新的东西.
What options do I have? This is about Delphi 2009 but for the sake of knowledge please also mention if there is something new in this regard in 2010+.
推荐答案
标准列表类(例如TList
,TObjectList
,TInterfaceList
等)没有实现自动生命周期,因此您必须手动释放它们当您完成使用它们时.如果要通过接口访问列表类,则必须自己实现,例如:
The standard list classes, like TList
, TObjectList
, TInterfaceList
, etc, do not implement automated lifecycles, so you have to free them manually when you are done using them. If you want a list class that is accessible via an interface, you have to implement that yourself, eg:
type
IListIntf = interface
...
end;
TListImpl = class(TInterfacedObject, IListIntf)
private
FList: TList;
...
public
constructor Create; override;
destructor Destroy; override;
...
end;
constructor TListImpl.Create;
begin
inherited;
FList := TList.Create;
end;
destructor TListImpl.Destroy;
begin
FList.Free;
inherited;
end;
function GetListWithItems: IListIntf;
begin
Result := TListImpl.Create;
// add items to list
end;
这篇关于哪些列表可以用作临时列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!