Dynamic Array of Integer (Length, SetLength methods)
Dynamic Array of Integer / Dynamic array of array of Integer
Smart pascal source code
var
a: array of Integer;
i: Integer;
begin
Writeln(Length(a)); // 0
a := New Integer[3];
Writeln(Length(a)); // 3
for i := Low(a) to High(a) do
a[i] := i * i;
a.SetLength(4);
Writeln(a.Length); // 4
WriteLn('--------------');
for i := a.Low to a.High do
Writeln(a[i]); // 0, 1, 4, 0
{ OUTPUT DISPLAY
a[0]; // 0
a[1]; // 1
a[2]; // 4
a[3]; // 0
}
var a = [], i = 0;
WriteLn(a.length);
a = [0,0,0];
WriteLn(a.length);
var $temp1;
for(i=0,$temp1=a.length;i<$temp1;i++) {
a[i]=i*i;
}
$ArraySetLen(a,4,0);
WriteLn(a.length);
WriteLn("--------------");
var $temp2;
for(i=0,$temp2=a.length;i<$temp2;i++) {
WriteLn(a[i]);
}
Smart pascal source code
var
a : array of array of Integer;
i, j : Integer;
begin
a.SetLength(3);
for i:=a.Low to High(a) do begin
a[i].SetLength(4);
for j:=Low(a[i]) to a[i].High do begin
a[i][j]:=(i+1)*10+j;
end;
end;
for i:=0 to 2 do begin
for j:=0 to 3 do
WriteLn(a[i][j]);
WriteLn('');
end;
{
10,11,12,13
20,21,22,23
30,31,32,33
}
function $ArraySetLenC(a, n, d) {
var o = a.length;
if (o == n)
return;
if (o > n)
a.length = n;
else
for (; o < n; o++)
a.push(d());
}
function $ArraySetLen(a, n, v) {
var o = a.length;
if (o == n)
return;
if (o > n)
a.length = n;
else
for (; o < n; o++)
a.push(v);
}
var a = [],
i = 0;
var j = 0;
$ArraySetLenC(a, 3, function () {
return []
});
var $temp1;
for (i = 0, $temp1 = a.length; i < $temp1; i++) {
$ArraySetLen(a[i], 4, 0);
var $temp2;
for (j = 0, $temp2 = a[i].length; j < $temp2; j++) {
a[i][j] = (i + 1) * 10 + j;
}
}
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 3; j++) {
WriteLn(a[i][j]);
}
WriteLn("");
}