【发布时间】:2020-10-19 08:14:54
【问题描述】:
我正在尝试在 create 方法中创建一个带有 2d 数组(arrscore)的类,我对此进行了一些研究,目前为止。
constructor create(spriority, sSelOne, sSeltwo, sSelthree: string; arrcountry: array of string; arrscore: array of real);
这是我的类变量声明
type
tMap = class
private
// variables
priority, selone, seltwo, selthree: string;
country: array of string;
score: array of real;
这是我的创建代码
begin
priority := spriority;
selone := sSelOne;
seltwo := sSeltwo;
selthree := sSelthree;
country := arrcountry;
score := arrscore;
end;
这不起作用,因为它是动态数组和实数数组的不兼容类型。 提前致谢。
【问题讨论】:
-
我没有看到任何二维数组。我只看到两个一维数组。
-
不要按值传递打开的数组。如果你这样做了,那么就会制作一个副本并在堆栈上传递。这是低效的并且可能导致堆栈溢出。使用
const参数。然后您需要做的是用分配新数组的代码(使用SetLength)替换对动态数组的分配,然后在循环中将参数中的值复制到动态数组。在现代 Delphi 中,您可以使用System.Generics.Collections.TArray.Copy代替循环
标签: arrays class delphi multidimensional-array