SetLength and Array



Using SetLength with Arrays.
Pas2JS pascal source code
type TRec = record F1 : Integer; F2 : String; end; procedure WriteLnArray(a : array of TRec); var i : Integer; begin WriteLn(IntToStr(Length(a))+':{'); for i:=0 to High(a) do begin if i>0 then WriteLn(','); WriteLn('('); WriteLn(IntToStr(a[i].F1)); WriteLn(','); WriteLn(a[i].F2); WriteLn(')'); end; WriteLn('}'); end; { TApplication } procedure TApplication.RunApp; var i : Integer; a : array of TRec; Begin WriteLnArray(a); // 0:{} a.SetLength(3); WriteLnArray(a); // 3:{(0,),(0,),(0,)} a[0].F1:=1; a[0].F2:='one'; a[1].F1:=2; a[1].F2:='two'; a[2].F1:=3; a[2].F2:='three'; WriteLnArray(a); // 3:{(1,one),(2,two),(3,three)} a.SetLength(3); WriteLnArray(a); // 3:{(1,one),(2,two),(3,three)} a.SetLength(2); WriteLnArray(a); // 2:{(1,one),(2,two)} a.SetLength(3); WriteLnArray(a); // 3:{(1,one),(2,two),(0,)} a.SetLength(1); WriteLnArray(a); // 1:{(1,one)} a.Clear; WriteLnArray(a); // 0:{} a.SetLength(3); WriteLnArray(a); // 3:{(0,),(0,),(0,)}