我試圖有條件地分配一個本地開放陣列變數,以便它指向一個 const 開放陣列引數:
uses
System.Generics.Collections,
System.Generics.Defaults;
type
TArray = class(System.Generics.Collections.TArray)
public
class function SameValues<T>(const AValuesA: array of T; AValuesB: array of T; const AComparer: IEqualityComparer<T>; AOrderMatters : Boolean = True) : Boolean; overload; static;
end;
...
class function TArray.SameValues<T>(const AValuesA: array of T; AValuesB: array of T; const AComparer: IEqualityComparer<T>; AOrderMatters : Boolean = True) : Boolean;
var
ArrA : TArray<T>;
ArrB : TArray<T>;
i : integer;
begin
//checking sizes
if(Length(AValuesA) <> Length(AValuesB)) then
begin
Result := False;
Exit;
end;
if(AOrderMatters) then
begin
//I don't need to change the arrays, so I could directly point to the open array parameters
ArrA := AValuesA;
ArrB := AValuesB;
end else
begin
//copying to local arrays
SetLength(ArrA, Length(AValuesA));
TArray.Copy<T>(AValuesA, ArrA, Length(AValuesA));
SetLength(ArrB, Length(AValuesB));
TArray.Copy<T>(AValuesB, ArrB, Length(AValuesB));
//sorting local arrays
TArray.Sort<T>(ArrA);
TArray.Sort<T>(ArrB);
end;
//comparing elements
i := 0;
while(i < Length(ArrA)) do
begin
if(not AComparer.Equals(ArrA[i], ArrB[i])) then
begin
Result := False;
Exit;
end;
Inc(i);
end;
Result := True;
end;
在編譯時,它會在以下位置引發 E2010 錯誤:
//I don't need to change the arrays, so I could directly point to the open array parameters
ArrA := AValuesA;
ArrB := AValuesB;
E2010 不兼容的型別:“動態陣列”和“T 陣列”
我嘗試了以下方法:
ArrA := @AValuesA;
ArrB := @AValuesB;
它編譯但隨后在運行時引發 AV 例外(最重要的是,我不知道這是否是一種安全的方法)。
這是我的測驗應用程式代碼:
uses
System.Generics.Defaults;
procedure TForm1.FormCreate(Sender: TObject);
var
ArrA : array of string;
ArrB : array of string;
begin
SetLength(ArrA, 2);
ArrA[0] := 'hello';
ArrA[1] := 'world';
SetLength(ArrB, 2);
ArrB[0] := 'world';
ArrB[1] := 'hello';
if(TArray.SameValues<string>(ArrA, ArrB, TEqualityComparer<string>.Default, True)) then
ShowMessage('Same values and same order')
else if(TArray.SameValues<string>(ArrA, ArrB, TEqualityComparer<string>.Default, False)) then
ShowMessage('Same values but different order')
else
ShowMessage('Different values');
end;
uj5u.com熱心網友回復:
您嘗試做的事情是不可能的,因為開放陣列和動態陣列與您的代碼嘗試的方式不兼容。
不過,有一種簡單的方法可以實作您想要的目標。您可以使用簡單的遞回來實作您所需要的。像這樣:
class function TArray.SameValues<T>(const AValuesA: array of T; AValuesB: array of T; const AComparer: IEqualityComparer<T>; AOrderMatters : Boolean = True) : Boolean;
var
ArrA : TArray<T>;
ArrB : TArray<T>;
i : integer;
begin
//checking sizes
if Length(AValuesA) <> Length(AValuesB) then
begin
Result := False;
Exit;
end;
if not AOrderMatters then
begin
//copying to local arrays
SetLength(ArrA, Length(AValuesA));
TArray.Copy<T>(AValuesA, ArrA, Length(AValuesA));
SetLength(ArrB, Length(AValuesB));
TArray.Copy<T>(AValuesB, ArrB, Length(AValuesB));
//sorting local arrays
TArray.Sort<T>(ArrA);
TArray.Sort<T>(ArrB);
Result := SameValues<T>(ArrA, ArrB, AComparer, True);
Exit;
end;
//comparing elements
for i := 0 to High(AValuesA) do
begin
if not AComparer.Equals(AValuesA[i], AValuesB[i]) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/340212.html
標籤:数组 德尔福 delphi-xe7
上一篇:在ubuntu終端中以純文本形式從url讀取java類檔案(?)
下一篇:如何轉義加速器字符?[復制]
