How to append one array to another array of same type in Delphi?

前端 未结 3 971
难免孤独
难免孤独 2021-01-20 14:05

How to append one array to another array of the same type without using iterative statements (for or while loops) in Delphi?

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-20 14:42

    In the last Delphi versions (XE7+) you can just use + operator or Concat routine to append arrays. Link. Official help (doesn't mention +)

    Otherwise write your own procedure (use generic arrays if possible). Quick example (checked in XE3):

    type 
    TArrHelper = class
      class procedure AppendArrays(var A: TArray; const B: TArray);
    end;
    
    
    class procedure TArrHelper.AppendArrays(var A: TArray;
      const B: TArray);
    var
      i, L: Integer;
    begin
      L := Length(A);
      SetLength(A, L + Length(B));
      for i := 0 to High(B) do
        A[L + i] := B[i];
    end;
    

    usage:

    var
      A, B: TArray;
    begin
      A := TArray.Create('1', '2', '3');
      B := TArray.Create('4', '5');
      TArrHelper.AppendArrays(A, B);
    

提交回复
热议问题