Arreglos dinámicos en Delphi
Duraciones de pistas en un array of Integer que se dimensiona, se llena y luego crece.
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.
Cómo funciona
SetLength(Seconds, 3)reserva tres espacios, yLength(Seconds)acota el bucle de llenado.- Un segundo
SetLength(Seconds, 4)conserva los valores y liberaSeconds[3]para escribir. for Track in Seconds dovisita cada elemento sin índice.
Palabras clave y builtins usados aquí
IntegerLengthSetLengthWriteWriteLnarraybegindoendforinofprogramtovar
El intento, en números
- Líneas
- 15
- Caracteres a escribir
- 461
- Tokens
- 95
- Ritmo de tres estrellas
- 70 tpm
Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 81 segundos.
Paso 2 de 3 en Arreglos y registros; paso 14 de 27 en Fundamentos del lenguaje.