Bar*_*lay -1 arrays delphi tlist
我在这里Tlist进入字符串数组并没有太多问题,但是我正在抛出数组我希望列出所有值将持续.
这是每次都列出的最后一个值.我想那样
Value1q
Value2q
Value3q
bla bla.所以我想在列表中的TList列出所有元素.
抱歉我的英语不好
public var
{ Public declarations }
Address : Array Of String;
AddressList: Tlist;
//Form Create
AddressList := Tlist.Create;
SetLength(Address, 3);
//Copy Listview Button
var
i : integer;
AItem: TlistItem;
begin
AddressList.Clear;
for i := 0 to SelectedListView.Items.Count - 1 do
begin
AItem := SelectedListView.Items.Item[i];
Address[0] := AItem.SubItems[0];
Address[1] := AItem.Caption;
Address[2] := AItem.SubItems[1];
AddressList.Add(Address);
end;
//Always being counted towards the last value
for i := 0 to AddressList.Count -1 do
MultiUser.Text := TArrayStr(AddressList[i])[1]);
Run Code Online (Sandbox Code Playgroud)
我在这里看到的问题是动态数组是托管类型,并且依赖于引用计数.只要数组由正确类型的变量引用,引用计数才有效.当您存储为无类型时Pointer,TList编译器无法正确计算引用.
除了基本的设计缺陷之外,你的程序中实际上只有一个数组.您TList每次调用时都只需向该对象添加相同的指针Add.请记住,动态数组是引用类型,没有任何写入时复制.所以他们表现得像真正的参考.
如果您有一个现代的Delphi,那么您可以使用类型安全的通用容器来解决这个问题.例如TList<TArray<string>>,TList<T>来自哪里Generics.Collections.
对于旧版本的Delphi,您不能指望将动态数组存储在TList.那根本就不会飞.你可以自己破解引用计数,但你需要清楚地掌握并理解.遇到代码的下一个编码器会鄙视你.
因此,德尔福的旧版本,我建议您更换TList同TObjectList.设置OwnsObjects为True.并string用a 替换动态数组TStringList.
传统Delphi的另一种解决方案是使用多维数组:array of array of string.
| 归档时间: |
|
| 查看次数: |
2943 次 |
| 最近记录: |