Dynamic Array (Swap, Reverse methods)



Swap method
Pas2JS pascal source code
var a: array of Integer; function MakeArray(n: Integer): array of Integer; begin Result := New Integer[n]; while n > 0 do begin Dec(n); Result[n] := n; end; end; procedure WriteLnArray(a: array of Integer); var i: Integer; begin Writeln(IntToStr(a.Length) + ' elements'); for i := a.Low to a.High do begin Writeln(i); Writeln(': '); Writeln(a[i]); end; end; begin a := MakeArray(0); a.Reverse; WriteLnArray(a); // 0 elements a := MakeArray(1); a.Reverse; WriteLnArray(a); // 0 elements a.Swap(0,0); WriteLnArray(a); { 1 elements 0: 0 } a := MakeArray(2); a.Reverse; WriteLnArray(a); { 2 elements 0: 1 1: 0 } a.Swap(0,1); WriteLnArray(a); { 2 elements 0: 0 1: 1 } a := MakeArray(3); a.Reverse; WriteLnArray(a); { 3 elements 0: 2 1: 1 2: 0 } a.Swap(0,2); WriteLnArray(a); { 3 elements 0: 0 1: 1 2: 2 }