@pcplayer 有时还是需要用一下的,比如这样 for var i: Integer := 0 to High(lbs1) do begin lbs1[i].Text := ida1[i + 1].ToString; ..... end; 如果用donwnto的话就要改i+1比较麻烦,不然次序就颠倒了. @wang_80919 好的 明白了
----------------------------------------------
-
for var i := Low(Value) to High(Value) div 2 do begin var tmp := Value[i]; Value[i] := Value[High(Value) - i]; Value[High(Value) - i] := tmp; end;
----------------------------------------------
-
type TArrayUtil = class strict private class procedure Swap<T>(var A, B: T); static; inline; public class procedure Reverse<T>(var Value: TArray<T>); overload; static; class procedure Reverse<T>(var Value: Array of T); overload; static; end;
{ TArrayUtil }
class procedure TArrayUtil.Reverse<T>(var Value: TArray<T>); begin if Length(Value) > 0 then for var i := Low(Value) to High(Value) div 2 do Swap<T>(Value[i], Value[High(Value) - i]); end;
class procedure TArrayUtil.Reverse<T>(var Value: Array of T); begin if Length(Value) > 0 then for var i := Low(Value) to High(Value) div 2 do Swap<T>(Value[i], Value[High(Value) - i]); end;
class procedure TArrayUtil.Swap<T>(var A, B: T); begin var C: T := A; A := B; B := C; end;
----------------------------------------------
-
function 返回数组倒置(Value: tarray<integer >): tarray<integer>; var temp:integer; begin for var i := Low(Value) to High(Value) div 2 do begin var tmp := Value[i]; Value[i] := Value[High(Value) - i]; Value[High(Value) - i] := tmp; end; Result := Value; end;
下面两种都可以,变参和引用 //1 procedure fun1(var Value: tarray<integer>); begin for var i := Low(Value) to High(Value) div 2 do begin var tmp := Value[i]; Value[i] := Value[High(Value) - i]; Value[High(Value) - i] := tmp; end; end; //2 procedure fun2(Value: tarray<integer>); begin for var i := Low(Value) to High(Value) div 2 do begin var tmp := Value[i]; Value[i] := Value[High(Value) - i]; Value[High(Value) - i] := tmp; end; end;
----------------------------------------------
-
function ArrIntEndToStart(AValue: TArray<integer>): TArray<integer>; begin for var i: integer := high(AValue) downto 0 do result := result + [AValue[i]]; end; ----------
procedure TForm1.Button1Click(Sender: TObject); var LArrInt: TArray<integer>; A, B : TArray<integer>; LText : string; begin LArrInt := [1, 2, 3, 4, 5]; // [] // ArrIntEndToStart( [11,4,51,57,2 ] ); // A := ArrIntEndToStart(LArrInt); B := ArrIntEndToStart(A); //
// MEMO can needs many time to process, then, it's a just for tests!!!
LText := ''; for var V in A do LText := LText + ',' + V.ToString; // Memo1.Text := LText.Remove(0, 1); LText := ''; // for var V in B do LText := LText + ',' + V.ToString; // Memo1.Lines.Add(LText.Remove(0, 1)); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
@qiuqiu77 有个新的问题来了,我想加个找出元素在数组中的序号的函数. class function TArrayUtil.Getindx<T>(var SourceArr: array of T; Item: T): integer; var id: Integer; begin id := -1; for var i := Low(SourceArr) to High(SourceArr) do begin if SourceArr[i] = Item then begin id := i; break; end; end;
Result := id; end; 比如找出数组['1','2','3']中的'2',应该返回序号1,但就始终找不到呢,调试的时候i=1的时候监视列表里SourceArr[i] = Item是true,但程序就是不按这个走,怎么回事呢?
----------------------------------------------
-
c: array of string; begin c := ['1', '2', '3']; // for var i: integer := 0 to high(c) do if c[i] = '2' then showmessage( i.ToString ); // 1
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
look this https://bbs.2ccc.com/topic.asp?topicid=675330
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
Delphi checks whether the "type" of the object is compatible or not! If the types are compatible then you can do something. Otherwise, you'll need to do a "casting" or nothing!
Thus, you cannot compare the value (initially), but only after evaluating the involved types!
Delphi looks at the "type name", not the definition itself!
type TMyTypeArrInteger = array of integer; // always "type name" for your "types"
procedure TForm1.Button1Click(Sender: TObject); var Arr1: TMyTypeArrInteger; Arr2: array of integer; Arr3: TMyTypeArrInteger; Arr4: array of integer; begin
TTest<T1, T2> = class public class function TheValuesIsEqual_01(ValueA: T1; ValueB: T2): boolean; end;
{ TTest<T1, T2> }
class function TTest<T1, T2>.TheValuesIsEqual_01(ValueA: T1; ValueB: T2): boolean; begin // // same "type name" => string=string, integer=integer, TMyType=TMyType // // ...Kind => TTypeKind.tk... // if TValue.From<T1>(ValueA).Kind = TValue.From<T2>(ValueB).Kind then begin if T1(ValueA) = T2(ValueB) then ShowMessage('ValueA = ValueB') else ShowMessage('ValueA <> ValueB'); end else ShowMessage('ValueA type is not the same than ValueB'); end;
procedure TForm1.Button1Click(Sender: TObject); begin TTest<integer, integer>.TheValuesIsEqual_01(123, 123); end;
end.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
class function TArrayUtil.GetCount<T>(var SourceArr: TArray<T>; Item: T): integer; var id: Integer; begin id := 0; for var i := Low(SourceArr) to High(SourceArr) do if T(SourceArr[i]) = T(Item) then begin inc(id) end; Result := id; end;
procedure TForm1.btn3Click(Sender: TObject); var ss:tarray<string>;id :integer;
begin ss:=['a','b','a']; id:= TArrayUtil.GetCount<string>(ss,'a'); ShowMessage(id.ToString ); end; //结果应该是2,但结果是0
----------------------------------------------
-
type TArrayUtil = class public class function GetCount<T>(var SourceArr: TArray<T>; Item: T): integer; static; end;
{ TArrayUtil }
class function TArrayUtil.GetCount<T>(var SourceArr: TArray<T>; Item: T): integer; var LIsString: boolean; a, b : string; begin result := 0; // LIsString := (TValue.From<T>(Item).Kind = TTypeKind.tkUString {... tkString, tkWString... + + +} ); // for var V in SourceArr do begin if LIsString then begin a := TValue.From<T>(V).AsString; b := TValue.From<T>(Item).AsString; if SameText(a, b) then // comparing "string" inc(result); end { } else // another types... if (V = Item) then // comparing another types inc(result); end; end;
procedure TForm1.Button1Click(Sender: TObject); var LArrValues: TArray<string>; begin // LArrValues := [1, 5, 3, 4, 5, 6, 7, 5, 9]; LArrValues := ['1', '5', '3', '4', '5', '6', '7', '5', '9']; // LArrValues := [Button1, Button2, Button3, Button4, Button5, Button2]; // ShowMessage(TArrayUtil.GetCount<string>(LArrValues, '5').ToString + ' was found and counting...'); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
var a : string; b : string; begin if a = b then ... -> if A-address = B-address then...
if SameText(a, b) then --> if ValueA = ValueB then ...
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
var PTI: PTypeInfo; begin PTI:=TypeInfo(T); Print(PTI.NameFld.ToString);//输出的时string,integer,int64,TGUID,TDateTime等文本。 PTI:=TypeInfo(U); Print(PTI.NameFld.ToString);
you always need test what is type of the "T", for that exists RTTI records/classes
this information should be verifyed in run-time... there is not other way!
Class/Object/Record/Interfaces
using TValue is a easy way to get the information, but you can use another RTTI ways.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
class function TArrayUtil.GetCount<T>(const ASourceArr: TArray<T>; const AItem: T; const AComparer: IComparer<T> = nil): NativeInt; begin Result := 0; if Length(ASourceArr) > 0 then begin var LComparer := AComparer; if LComparer = nil then LComparer := TComparer<T>.Default;
for var I := Low(ASourceArr) to High(ASourceArr) do if LComparer.Compare(ASourceArr[I], AItem) = 0 then Inc(Result); end; end;
----------------------------------------------
-