我想知道以下通用代码(使用 Delphi 10.4.1 编写)有什么问题。此外,是否有另一种实现相同目标的方法?我的意思是在数组内搜索(我知道应该使用集合)。提前感谢!祝好!
type
TDelphiExtention = class
public
class function inside<T>(const value : T; const arr : array of T) : boolean;
end;
class function TDelphiExtention.inside<T>(const value : T; const arr : array of T) : boolean;
var
i : integer;
begin
result := true;
for i := Low(arr) to High(arr) do begin
if (arr[i] = value) then begin // E2015 Operator not applicable to this operand type !!!
exit;
end;
end;
result := false; // Not found
end;
// This one compiles like a charm... But is not generic :(
function inside(const value : integer; const arr : array of integer) : boolean;
var
i : integer;
begin
result := true;
for i := Low(arr) to High(arr) do begin
if (arr[i] = value) then begin
exit;
end;
end;
result := false; // Not found
end;