Fabrizio
Fabrizio

Reputation: 8043

Setting bounds for dynamic arrays

Static arrays allow to define their low and high bounds:

StaticArray : array[5..7] of Integer;

I don't know how to do the same thing with dynamic arrays at runtime.

For example, if I need to copy the elements from a static array, keeping the same indexes, I don't know how to set its low bound to Low(StaticArray) and high bound to High(StaticArray):

var
  StaticArray : array[5..7] of Integer;
  DynamicArray : array of Integer;
  i : integer;
begin
  //initializing static array
  StaticArray[5] := 1;
  StaticArray[6] := 2;
  StaticArray[7] := 3;

  //setting the same length and bounds to the dynamic array
  //?

  //copying elements
  i := 0;
  for i := Low(StaticArray) to High(StaticArray) do
    DynamicArray[i] := StaticArray[i];
end;

Is there any way for setting the low/high bounds of a dynamic array, or do they always have 0 as low bound and Length(Array) - 1 as high bound?

Upvotes: 1

Views: 484

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596111

Is there any way for setting the low/high bounds of a dynamic array

No.

or do they always have 0 as low bound and Length(Array) - 1 as high bound?

Yes. Which means you can do this:

var
  StaticArray : array[5..7] of Integer;
  DynamicArray : array of Integer;
  i : integer;
begin
  //initializing static array
  StaticArray[5] := 1;
  StaticArray[6] := 2;
  StaticArray[7] := 3;

  //setting the same length and bounds to the dynamic array
  SetLength(DynamicArray, Length(StaticArray));

  //copying elements
  for i := Low(StaticArray) to High(StaticArray) do
    DynamicArray[i-Low(StaticArray)] := StaticArray[i];
end;

Or, just use two separate index variables:

var
  StaticArray : array[5..7] of Integer;
  DynamicArray : array of Integer;
  i, j : integer;
begin
  //initializing static array
  StaticArray[5] := 1;
  StaticArray[6] := 2;
  StaticArray[7] := 3;

  //setting the same length and bounds to the dynamic array
  SetLength(DynamicArray, Length(StaticArray));

  //copying elements
  j := 0;
  for i := Low(StaticArray) to High(StaticArray) do begin
    DynamicArray[j] := StaticArray[i];
    Inc(j);
  end;
end;

Or, since your arrays contain non-managed trivial types, just replace the whole loop with a single Move() call instead:

var
  StaticArray : array[5..7] of Integer;
  DynamicArray : array of Integer;
begin
  //initializing static array
  StaticArray[5] := 1;
  StaticArray[6] := 2;
  StaticArray[7] := 3;

  //setting the same length and bounds to the dynamic array
  SetLength(DynamicArray, Length(StaticArray));

  //copying elements
  Move(StaticArray[Low(StaticArray)], DynamicArray[0], Length(DynamicArray) * SizeOf(Integer));
end;

Upvotes: 1

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

Every time you have a question about the Delphi language, you should consult the official documentation. In this case, the section named Dynamic Arrays on Structured Types says the following:

Dynamic arrays are always integer-indexed, always starting from 0.

Upvotes: 7

Related Questions