- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 Delphi XE5 中使用带有子列表的通用记录 TList:
type
TMyRecord=record
Value1: Real;
SubList: TList<Integer>;
end;
TMyListOfRecords=TList<TMyRecord>;
var
MyListOfRecords: TMyListOfRecords;
不可能对记录字段进行赋值:
MyListOfRecords[0].Value1:=2.24;
或
MyListOfRecords[0].SubList:=TList<Integer>.Create;
将导致编译器出现“左侧无法分配给”错误。
另请参阅:How to modify TList<record> value?
以下解决方法有效:
AMyRecord:=MyListOfRecords[0];
AMyRecord.Value1:=2.24;
AMyRecord.SubList:=TList<Integer>.Create;
AMyRecord.SubList.Add(33);
MyListOfRecords[0]:=AMyRecord;
由于性能问题,我想避免将数据复制到临时 AMyrecord。我宁愿直接访问记录字段和子列表。
处理这个问题的最佳方法是什么?
最佳答案
列表通过 List
公开其内部存储,这是一个动态数组。属性(property)。所以你可以写:
MyListOfRecords.List[0].Value1 := 2.24;
与具有值(value)副本的替代方案相比,这是否会在性能上产生任何可测量的差异,我无法判断。值得检查一下。
正如@LURD正确所说,List
返回内部存储。而这可能不止Count
元素。具体来说有Capacity
元素。因此,如果您使用它,则必须使用数组索引访问元素 0
至Count-1
。还要记住,对列表大小的修改可能涉及重新分配,因此内部存储可能会移动。您对List
的任何引用仅在下次重新分配之前有效。
这些警告应该建议您只考虑使用 List
如果性能限制需要的话。即便如此,也要谨慎使用。
在我的代码库中,我有 TList<T>
的替代方案谁的Items[]
属性返回指向该元素的指针。容器仍然存储为动态数组,以实现高效的内存布局。与 List
相比,我更喜欢此选项属性(property),因为我觉得它可以带来更清晰的代码。
好的,您要求查看我的列表类,该列表类返回指向元素的指针。这是:
type
TReferenceList<T> = class(TBaseValueList<T>)
type
P = ^T;
private
function GetItem(Index: Integer): P;
public
property Items[Index: Integer]: P read GetItem; default;
public
// .... helper types for enumerators excised
public
function GetEnumerator: TEnumerator;
function Enumerator(Forwards: Boolean): TEnumeratorFactory;
function ReverseEnumerator: TEnumeratorFactory;
function IndexedEnumerator: TIndexedEnumeratorFactory;
end;
现在,需要一些解释。基类,TBaseValueList<T>
是我对 TList<T>
的替代方案。您可以替换 TList<T>
如果你希望。我没有,因为我的基类没有 Items
属性(property)。因为我想要专门的类(class)来介绍它。我的其他专业是:
type
TValueList<T> = class(TBaseValueList<T>)
private
function GetItem(Index: Integer): T;
procedure SetItem(Index: Integer; const Value: T);
public
property Items[Index: Integer]: T read GetItem write SetItem; default;
end;
我的TBaseValueList<T>
的实现是很明显的。它与 TList<T>
非常相似。我认为您真的不需要看到任何实现。这一切都非常明显。
作为获取元素引用的简单方法,您可以将 List
包装起来。像这样:
type
TMyList<T> = class(TList<T>)
public
type
P = ^T;
private
function GetRef(Index: Integer): P;
public
property Ref[Index: Integer]: P read GetRef;
end;
function TMyList<T>.GetRef(Index: Integer): P;
begin
Result := @List[Index];
end;
如果您想要比 Delphi 提供的更丰富的容器集,您可能会关注 Spring4D。虽然我不确定他们是否有像我的容器一样返回引用的东西。
关于delphi - 带有子列表的通用记录 TList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22212783/
假设我有多个对象存储在 TO1:TList 中,然后我创建多个 TO1 并将它们全部放在 TO2:TList 中。如何在 TO2 中的选定 TO1 中获取选定对象的值? 最佳答案 由于TList为您提
我想知道是否有任何安全的方法可以将 TList 元素复制到任何其他 TList 中特定的位置和特定的长度。我应该将 list1 的元素分配给 list2 还是有没有我不知道的功能可以更准确地处理? 感
我有一个 TList 类型的列表.我需要将其转换为 TList像这样: procedure mainForm.testCast; var listT: TList; listW: TList;
我有一个 TList。它包含相同类型的对象的集合。这些对象是 TPersistent 的后代,并且具有大约 50 个不同的已发布属性。 在我的应用程序中,用户可以对这些对象进行搜索,搜索结果显示在 T
我有一个智能指针的实现,并且我尝试在通用 TList 上实现它。 program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUt
我想声明一个通用记录类型,例如 TMyGenericRecord = record X: ; Y: ; end; 然后我想声明一个 TMyGenericRecord 的 TList,但似乎
我想在 Delphi XE5 中使用带有子列表的通用记录 TList: type TMyRecord=record Value1: Real; SubList: TList;
我在使用 TList 时遇到内存泄漏问题。我正在尝试通过填充的列表填充 Tlist 循环并使用数据。下面的代码只是填充列表的代码,而不是使用它。 private { Private Form Va
我有一个包含以下声明的 Delphi 移动应用程序: TWJInformationList = class(TList) 当我为任何移动设备(Android、iOS 设备 32 位、iOS 设备 64
我正在使用 Generics.Collections.TList 和 Sort 方法。它工作正常,但我想最后对空值或空值进行排序。按升序和降序排序。如何实现? 这是我的排序函数: function T
我的项目(在 Delphi 6 上运行!)需要一个内存分配列表(TMemoryAllocation),我将其存储在一个对象中,该对象还保存有关分配大小(FSize)以及分配是否正在使用或空闲(FUse
我正在尝试统计每个 ProductCode 在我的数据库中使用的次数。问题是,我不知道所有代码是什么(或者可能会添加更多代码)。 我假设我可以使用带有值对(productCode 和 count)的
代码大致如下所示,它会为创建的每个 TMyRecord 中的每个字符串实例泄漏内存。我想我必须访问每条记录并以某种方式释放它——这可以在不清除每个单独字符串的情况下完成吗? function TMyF
使用TList作为记录容器。在应用过程中,TList添加和删除大量记录。但在delete之后,属性capacity永远不会减少,内存也不会被释放。怎么解决这个问题呢?简单的代码示例: type T
我正在尝试了解 Delphi 中的泛型,但 TList 有一个非常基本的问题。 我已经成功创建了一个整数列表并用 1000 个奇数填充它。我想将列表中所有能被 3 整除的数字更改为 0。我想我可以做这
有人可以向我解释一下这是否可能吗,或者我完全误解了这个 Delphi 功能。 假设我有一个类,我创建了其中一些类,然后将它们添加到 ObjectList 中。通常我这样做: Type TMyClass
我创建了一个简单的类,将对象保存在通用列表中。我设法让它工作。但我不明白为什么这不起作用。 for Monster in MonsterList do begin Monster.
我在 Windows 10 中使用 Delphi 10.1 Berlin。 我有两条不同大小的记录。我编写了循环遍历两个 TList 的代码这些记录来测试耗时。循环遍历较大记录的列表运行速度要慢得多。
我在 Delphi 4 中有这段代码。我创建了 10 个 T_Charge 类型的对象(见下文),并将其添加到 TList 中,名为 myList。 T_Charge 类型引用了三个 T_Platte
关于通用TList的一切。我有这样的结构: Type TExtract = record Wheel: string; Extract: array [1..5] of Byte;
我是一名优秀的程序员,十分优秀!