如何通过Delphi中的Move过程高效填充TList列表元素实现复杂数据处理?

2026-04-10 02:481阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计541个文字,预计阅读时间需要3分钟。

如何通过Delphi中的Move过程高效填充TList列表元素实现复杂数据处理?

我需要将存储在一个字节数组中的数据移动到TList中的一组记录中,但是遇到了错误:常量对象不能作为var参数传递。此代码使用了System.Generics.Collections。

我需要将存储在一个字节数组中的数据移动到位于TList中的一组记录,但是我收到了这个错误

E2197 Constant object cannot be passed as var parameter

此代码重现了该问题.

uses System.Generics.Collections, System.SysUtils; type TData = record Age : Byte; Id : Integer; end; //this code is only to show the issue, for simplicity i'm filling only the first //element of the TList but the real code needs fill N elements from a very big array. var List : TList<TData>; P : array [0..1023] of byte; begin try List:=TList<TData>.Create; try List.Count:=1; //here i want to move the content of the P variable to the element 0 Move(P[0],List[0], SizeOf(TData)); finally List.Free; end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

我如何将缓冲区的内容复制到TList元素

在XE2中,TList的内部存储器< T>.是不透明和隐藏的.您无法通过正常方式访问它.将复制对列表元素的所有访问 – 对底层存储的引用不可用.所以你不能使用Move搞定它.如果你想要一个可以blit的结构,你应该考虑一个动态数组TArray< T>.

你可以随时使用为TList实现类助手的技巧< TData>这会暴露私有变量FItems.这非常hacky但会做你要求的.

type __TListTData = TList<TData>; //defeat E2086 Type 'TList<T>' is not yet completely defined type TListTDataHelper = class helper for TList<TData> procedure Blit(const Source; Count: Integer); end; procedure TListTDataHelper.Blit(const Source; Count: Integer); begin System.Move(Source, Pointer(FItems)^, Count*SizeOf(Self[0])); end;

我想你可能想在TListTDataHelper.Blit中进行一些参数检查,但我会留给你.

如果您使用的是XE3,则可以访问TList< T>的私人存储空间.使用List属性.

Move(P, Pointer(List.List)^, N*SizeOf(List[0]));

如果你不需要blit并且可以使用for循环,那么就这样做:

如何通过Delphi中的Move过程高效填充TList列表元素实现复杂数据处理?

type PData = ^TData; var i: Integer; Ptr: PData; .... List.Count := N; Ptr := PData(@P); for i := 0 to List.Count-1 do begin List[i] := Ptr^; inc(Ptr); end;

但我解释你的问题,你希望避免这个选项.

本文共计541个文字,预计阅读时间需要3分钟。

如何通过Delphi中的Move过程高效填充TList列表元素实现复杂数据处理?

我需要将存储在一个字节数组中的数据移动到TList中的一组记录中,但是遇到了错误:常量对象不能作为var参数传递。此代码使用了System.Generics.Collections。

我需要将存储在一个字节数组中的数据移动到位于TList中的一组记录,但是我收到了这个错误

E2197 Constant object cannot be passed as var parameter

此代码重现了该问题.

uses System.Generics.Collections, System.SysUtils; type TData = record Age : Byte; Id : Integer; end; //this code is only to show the issue, for simplicity i'm filling only the first //element of the TList but the real code needs fill N elements from a very big array. var List : TList<TData>; P : array [0..1023] of byte; begin try List:=TList<TData>.Create; try List.Count:=1; //here i want to move the content of the P variable to the element 0 Move(P[0],List[0], SizeOf(TData)); finally List.Free; end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

我如何将缓冲区的内容复制到TList元素

在XE2中,TList的内部存储器< T>.是不透明和隐藏的.您无法通过正常方式访问它.将复制对列表元素的所有访问 – 对底层存储的引用不可用.所以你不能使用Move搞定它.如果你想要一个可以blit的结构,你应该考虑一个动态数组TArray< T>.

你可以随时使用为TList实现类助手的技巧< TData>这会暴露私有变量FItems.这非常hacky但会做你要求的.

type __TListTData = TList<TData>; //defeat E2086 Type 'TList<T>' is not yet completely defined type TListTDataHelper = class helper for TList<TData> procedure Blit(const Source; Count: Integer); end; procedure TListTDataHelper.Blit(const Source; Count: Integer); begin System.Move(Source, Pointer(FItems)^, Count*SizeOf(Self[0])); end;

我想你可能想在TListTDataHelper.Blit中进行一些参数检查,但我会留给你.

如果您使用的是XE3,则可以访问TList< T>的私人存储空间.使用List属性.

Move(P, Pointer(List.List)^, N*SizeOf(List[0]));

如果你不需要blit并且可以使用for循环,那么就这样做:

如何通过Delphi中的Move过程高效填充TList列表元素实现复杂数据处理?

type PData = ^TData; var i: Integer; Ptr: PData; .... List.Count := N; Ptr := PData(@P); for i := 0 to List.Count-1 do begin List[i] := Ptr^; inc(Ptr); end;

但我解释你的问题,你希望避免这个选项.