TMyArray=class helper for tarray function Contains(Item:string):Boolean;overload; function Contains(Item:integer):Boolean;overload; end;
//TMyArray=class helper for array //TMyArray=class helper for array of string //TMyArray=class helper for tarray<string> 为什么上面写法三种通不过,是因为它们不是类吗?
function TMyArray.Contains(Item: string): Boolean; var i: Integer; begin for i := 0 to Length(Self) - 1 do if string(Self[i]) = Item then result := True; end;
function TMyArray.Contains<t>(arr: tarray<t>; Item: t): Boolean; var i: Integer; begin for i := 0 to Length(arr) - 1 do if TValue.From<t>(arr[i]).ToString = TValue.From<t>(Item).ToString then result := True; result := false; end; 但是这样的话使用的时候比如 var ss:tarray<string>;b:Boolean; b:=TArray.contains<string>( ss,'123'); 是这样用吗 会报错
----------------------------------------------
-
TMyHlpTArray_1 = record helper for TMyArrayString_1 function ContainsThis(AValue: string): boolean; overload; function ContainsThis<T>(AValue: T): boolean; overload; end;
{ TMyHlpTArray<T> }
function TMyHlpTArray_1.ContainsThis(AValue: string): boolean; begin result := false; // for var V in Self do if (V = AValue) then // CompareStr(s1,s2) begin result := true; break; end; end;
function TMyHlpTArray_1.ContainsThis<T>(AValue: T): boolean; begin result := false; // for var V in Self do if (V = TValue.From<T>(AValue).AsString) then // CompareStr(s1,s2) begin result := true; break; end; end;
procedure TForm1.Button1Click(Sender: TObject); var LValues_1: TMyArrayString_1; begin LValues_1 := ['hello', 'world', 'delphi']; // if LValues_1.ContainsThis('delphi') then ShowMessage('Delphi found') else ShowMessage('Delphi NOT found'); // if LValues_1.ContainsThis<string>('delphi') then ShowMessage('Delphi found') else ShowMessage('Delphi NOT found'); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
type TMyHelper = class public class function contains<T>(arr:array of T; item:T) :boolean; end;
TMyArray<T> = array of T;
TMyArray=class(tarray) class function contains<T>(arr:array of T; item:T) :boolean; end;
THelpArray=class helper for tarray class function contains<T>(arr:array of T; item:T) :boolean; end;
{ TMyHelper }
class function TMyHelper.contains<T>(arr: array of T; item: T): boolean; var i:integer; begin Result:=false; for I := Low(arr) to High(arr) do begin if arr[i]=item then begin Result := true; Break; end; end; end;
{ TMyArray }
class function TMyArray.contains<T>(arr: array of T; item: T): boolean; var i:integer; begin Result:=false; for I := Low(arr) to High(arr) do begin if arr[i]=item then begin Result := true; Break; end; end;
end;
{ THelpArray }
class function THelpArray.contains<T>(arr:array of T; item:T): boolean; var i:integer; begin Result:=false; for I := Low(arr) to High(arr) do begin if arr[i]=item then begin Result := true; Break; end; end; end;
begin try Writeln(TMyHelper.contains<integer>([1,2,3,4],2)); Writeln(TMyHelper.contains<char>('12345'.ToCharArray(0,4),'2')); except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.
procedure TForm1.Button1Click(Sender: TObject); var LValues_1: TMyArrayString_1; begin LValues_1 := ['hello', 'world', 'delphi']; // if LValues_1.ContainsThis('delphi') then ShowMessage('Delphi found') else ShowMessage('Delphi NOT found'); // if LValues_1.ContainsThis<string>('delphi') then ShowMessage('Delphi found') else ShowMessage('Delphi NOT found'); end;
此帖子包含附件: 大小:446.4K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
type TArrayHelper = class helper for TArray public class function IndexOf<T>(const theArray: array of T; const theItem: T): Integer; static; class function Contains<T>(const theArray: array of T; const theItem: T): Boolean; static; end;
class function TArrayHelper.Contains<T>(const theArray: array of T; const theItem: T): Boolean; begin Result := IndexOf(theArray, theItem) > -1; end;
class function TArrayHelper.IndexOf<T>(const theArray: array of T; const theItem: T): Integer; var nIndex: Integer; begin /// 分类型判断,具体可以参照tvalue的tostring方法。 case PTypeInfo(System.TypeInfo(T))^.Kind of tkString, tkUString, tkLString, tkWString: for nIndex := Low(theArray) to High(theArray) do if (TValue.From(theItem).ToString() = TValue.From(theArray[nIndex]).ToString()) then Exit(nIndex);
else for nIndex := Low(theArray) to High(theArray) do if (CompareMem(@theItem, @(theArray[nIndex]), SizeOf(T))) then Exit(nIndex); end;
Exit(-1); end;
调用: var ai: TArray<Integer>; ac: TArray<Char>; &as: TArray<String>; i: Integer; begin SetLength(ai, 1000); SetLength(ac, 26); SetLength(&as, 100); for i := Low(ai) to High(ai) do ai[i] := i; for i := Low(ac) to High(ac) do ac[i] := Chr(i + Ord('A')); for i := Low(&as) to High(&as) do &as[i] := Format('08x', [i]);
if TArray.Contains(ai, 123) then ShowMessage('yes');
if TArray.Contains<Char>(ac, 'Z') then ShowMessage('yes');
if TArray.Contains<String>(&as, 'aaabbb') then ShowMessage('yes');