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

前端 未结 3 967
难免孤独
难免孤独 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:35

    Having two dynamic arrays arr1 and arr2

    var
      arr1, arr2: array of Integer;
    . . .
    SetLength(arr1, 3);
    arr1[0] := 1;
    arr1[1] := 2;
    arr1[2] := 3;
    
    SetLength(arr2, 3);
    arr2[0] := 4;
    arr2[1] := 5;
    arr2[2] := 6;
    

    you can append the first to the second like this:

    SetLength(arr2, Length(arr2) + Length(arr1));
    Move(arr1[0], arr2[3], Length(arr1) * SizeOf(Integer));
    

    See System.Move.


    As Uwe Raabe's comment points out, you can do as follows for managed types:

    SetLength(arr2, Length(arr2) + Length(arr1));
    for i := Low(arr1) to High(arr1) do
      arr2[3+i] := arr1[i];
    

提交回复
热议问题