procedure TForm1.Button1Click(Sender: TObject);
var
ArraySize: Integer;
IntAr: array of Integer;
begin
ArraySize := 0;
ArraySize := ArraySize + 1;
setLength(IntAr, ArraySize);
IntAr[0] := 115;
ArraySize := ArraySize + 1;
setLength(IntAr, ArraySize);
IntAr[1] := 160;
ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding +
'IntAr[1]: ' + IntToStr(IntAr[1]));
end;
That's ... wasteful, to be mild. If you know the exact number of items in the array do it like this:
You could be more polite and less arrogant.
How could you know if Pixy needs a limited or dynamic quantity of elements by the array on his question?
I am using this:
var
IntAr: array of integer;
begin
IntAr[0] := 115;
IntAr[1] := 160;
...
end.
but I get an access violation error. Any idea how I can add individual integers to the array?
I had provided some wiki information about limited and dynamic arrays and then a very simple solution that could address both cases.
I was not trying to be comprehensive, but simply educative. I still don't know how much Pixy knows about Pascal or Arrays.
procedure TForm1.Button1Click(Sender: TObject);
const
ArraySize = 2;
var
IntAr: array of Integer;
begin
setLength(IntAr, ArraySize);
IntAr[0] := 115;
IntAr[1] := 160;
ShowMessageFmt('IntAr[0]: %d' + LineEnding + 'IntAr[1]: %d', ,[IntAr[0], IntAr[1]);
end;
Each time you use SetLength() a new block of memory has to be allocated, the existing array copied over and the old memory block disposed. Call SetLength() as few times as you possible can.
You are correct, but again I believe Pixy is searching for a basic solution, so far. As times goes by, he will learn better solutions.