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
SetLength(Seconds, 3)allocates three slots, andLength(Seconds)bounds the fill loop.- A second
SetLength(Seconds, 4)keeps the stored values and freesSeconds[3]for a write. for Track in Seconds dovisits every element without an index.
Keywords and builtins used here
IntegerLengthSetLengthWriteWriteLnarraybegindoendforinofprogramtovar
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.
Step 2 of 3 in Arrays & records, step 14 of 27 in Language basics.