- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个简单的类,将对象保存在通用列表中。我设法让它工作。但我不明白为什么这不起作用。
for Monster in MonsterList do
begin
Monster.Free;
MonsterList.Remove(Monster);
end;
如果我尝试从 MonsterList 中释放和删除这样的项目,它并没有真正删除,在我的例子中,名称消失了,但强度值保留在那里。如果我随后尝试列出 MonsterList 的内容,我总是会留下一项。所以我用谷歌搜索了一下,并在 Stack-overflow 上找到了一个好的解决方案,那就是简单地倒计时。
另一件事是,当我将 Monsters 添加到 MonsterList 时,我添加了 3 个 Items ,但是如果我调试,我会看到 MonsterList 实际上有 0,1,2,3 3 为 NULL仅当我添加所有三个对象时才会发生这种情况,如果我只添加两个对象,它不会创建最终的 NULL 指针。这是某种优化的开始吗?
整个代码(不多)
unit MainForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Generics.Collections, Generics.Defaults,
System.Types;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMonster = class
private
fName : string;
fStrength : integer;
fisDead : boolean;
public
constructor Create(Name : string; Strength : integer);
destructor Destroy; override;
property Name : string read fName write fName;
property Strength : integer read fStrength write fStrength;
property isDead : boolean read fisDead write fisDead;
end;
var
Form1: TForm1;
MonsterList : TList<TMonster>;
MonsterInstances : integer = 0;
implementation
{$R *.dfm}
constructor TMonster.Create(Name: string; Strength: integer);
begin
inc(MonsterInstances);
fName := Name;
fStrength := Strength;
fisDead := false;
end;
destructor TMonster.Destroy;
begin
dec(MonsterInstances);
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
var Monster : TMonster;
i : integer;
begin
MonsterList := TList<TMonster>.Create;
Memo2.Lines.Add(inttostr(MonsterInstances));
MonsterList.Add(TMonster.Create('Jill',10));
MonsterList.Add(TMonster.Create('Jane',1));
MonsterList.Add(TMonster.Create('Rob',20));
Memo2.Lines.Add(inttostr(MonsterInstances));
for Monster in MonsterList do
begin
Memo1.Lines.Add(Monster.fName+ ' Strenght '+inttostr(Monster.fStrength)+' IsDead= '+booltostr(Monster.fisDead))
end;
MonsterList[1].isDead:=true;
// not working
{for Monster in MonsterList do
begin
Monster.Free;
MonsterList.Remove(Monster);
end; }
// works
for i := MonsterList.Count-1 downto 0 do
begin
if MonsterList[i].isDead = true then
begin
MonsterList[i].Free;
MonsterList.Delete(i);
MonsterList.Capacity:=MonsterList.Capacity-1;
end;
end;
Memo1.Lines.Add('Survivors :');
for Monster in MonsterList do
Memo1.Lines.Add(Monster.Name+' Strenght '+inttostr(Monster.Strength));
ShowMessage(inttostr(MonsterInstances));
end;
end.
谢谢!
最佳答案
您无法在像这样迭代列表时修改列表。相反,释放所有成员,然后清除列表。
for Monster in MonsterList do
Monster.Free;
MonsterList.Clear;
这还有一个额外的好处,就是不调用 Remove
这会花时间搜索该项目。
也许更简单的是使用 TObjectList<T>
并允许集合管理其成员的生命周期。那么您只需调用Clear
即可只要 OwnsObjects
是 True
所有成员将被销毁,列表被清除。
就你的第二个问题而言,如果你添加三个项目,则有索引为 0、1 和 2 的项目。没有索引为 3 的项目。现在,集合在内部很可能使用一个已结束的内部数组分配。因此私有(private)内部数组可以有一个索引 3。但是该内部数组的内容对您来说应该不重要。
关于class - Delphi 类 TList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50830198/
假设我有多个对象存储在 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;
我是一名优秀的程序员,十分优秀!