关于加入 TObjectlists
我想我需要朝正确的方向轻推:
我有两个相同数据类型的Tobjectlist,我想将它们连接到一个新列表中,其中list1将被复制(未修改),然后是list2(相反)
type
TMyListType = TobjectList<MyClass>
var
list1, list2, resList : TMyListtype
begin
FillListWithObjects(list1);
FillListWithOtherObjects(list2);
list2.reverse
//Now, I tried to use resList.Assign(list1, list2, laOr),
//but Tobjectlist has no Assign-Method. I would rather not want to
//iterate over all objects in my lists to fill the resList
end;
delphi是否有任何将两个 Tobjectlist 合并为一个的内置函数?
I think i need a nudge in the right direction:
I have two Tobjectlists of the same datatype, and i want to concatenate these into a new list into which list1 shall be copied (unmodified) followed by list2 (in reverse)
type
TMyListType = TobjectList<MyClass>
var
list1, list2, resList : TMyListtype
begin
FillListWithObjects(list1);
FillListWithOtherObjects(list2);
list2.reverse
//Now, I tried to use resList.Assign(list1, list2, laOr),
//but Tobjectlist has no Assign-Method. I would rather not want to
//iterate over all objects in my lists to fill the resList
end;
Does delphi have any built-in function to merge two Tobjectlists into one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
TObjectList.AddRange()
并将OwnsObjects
设置为False
以避免双重释放LRes
中的项目。Use
TObjectList.AddRange()
and setOwnsObjects
toFalse
to avoid double-freeing of the items inLRes
.唉,提供的答案看起来只在 XE 中有效。在 2010 年(大概在下面),AddRange 函数没有以 TObjectList 作为参数的重载(编译上面的代码片段在 AddRange 行上给出了 E2250) 。
目前花了一天时间研究泛型是否有助于简化大型项目中的代码,但缺乏分配函数(或任何可用的等效函数)是一个令人震惊的问题。在 D2009 中发布一些东西然后需要 2 个主要版本才能真正发挥作用似乎很奇怪!
Alas, the answer supplied looks like it only works in XE.. in 2010 (and presumably below) the AddRange function does not have an overload that takes a TObjectList as its parameter (compiling the above code snippet gives an E2250 on the AddRange lines).
Currently spending a day working if generics will help simplify code in a large project but the lack of an assign function (or any usable equivalent) is a showstopper. Seems odd to release something in D2009 then require 2 major releases before it actually works!