typestar

Dynamic arrays in Delphi

Track lengths in an array of Integer that is sized, filled, and then grown.

program DynamicArrays;
var
  Seconds: array of Integer;
  Track, I: Integer;
begin
  SetLength(Seconds, 3);        // SetLength allocates; slots start at zero
  for I := 0 to Length(Seconds) - 1 do
    Seconds[I] := 180 + I * 15;
  SetLength(Seconds, 4);        // growing keeps the values already stored
  Seconds[3] := 244;
  WriteLn('tracks on the tape: ', Length(Seconds));
  for Track in Seconds do       // for-in walks every element in order
    Write(Track, ' ');
  WriteLn;
end.

How it works

  1. SetLength(Seconds, 3) allocates three slots, and Length(Seconds) bounds the fill loop.
  2. A second SetLength(Seconds, 4) keeps the stored values and frees Seconds[3] for a write.
  3. for Track in Seconds do visits every element without an index.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
461
Tokens
95
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 81 seconds.

Type this snippet

Step 2 of 3 in Arrays & records, step 14 of 27 in Language basics.

← Previous Next →

Dynamic arrays in other languages