18

I want to do an ArrayList in Delphi 5.0. So I found a solution doing this code:

var arr: array of String;

OK, but every time I add something I do this:

var
    Form1: TForm1;
    var arr : array of String;

procedure TForm1.Button1Click(Sender: TObject);
var aux :string;
var len:integer;
begin
    len := Length(arr) + 1;
    SetLength(arr, len);
    arr[len-1] := 'abc' + IntToStr(len);
    Button1.Caption := arr[len-1]; // just to writeout something
end;

I'm a C++ programmer, and I do not know anything about Pascal. I always heard a Pascal index begins from 1, not 0. As in the above procedure I do arr[len-1] because of 0 index begin.

Is there a better way than Pascal arrays? Like with C++'s std::vector?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
okami
  • 2,093
  • 7
  • 28
  • 40

5 Answers5

46

Dynamic arrays' indexes begin with zero

var
  a: array of Integer;
begin
  SetLength(a, 500);
  a[0] := 0;

Static arrays can have arbitrary indexes

var
  i: Integer;
  b: array [50..100] of Integer;
  c: array[-10..10] of Integer; 
begin
  for i := 50 to 100 do b[i] := i * i;

  // Note negative starting index above in declaration
  for i := -10 to 10 do c[i] := i * i;

Strings' indexes begin with one

var
  c: String;
begin
  c := 'Zap!';
  c[1] := 'W';
  ShowMessage(c); /// shows 'Wap!'

Anyway you can always use Low() and High() functions which return the lower and higher index of an array.

For handling a list of strings the most commonly used class is TStringList which is found in unit Classes.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Trinidad
  • 2,756
  • 2
  • 25
  • 43
  • 6
    I haven't used Pascal for a long time but if memory serves well, the reason for string indexes start from 1 is because the first element is string length, and you can edit directly to change the length – phuclv May 27 '14 at 04:43
  • 8
    +jachguate How come this answer is outdated when the questions is about "Delphi 5.0 Pascal"? :-) – Trinidad Sep 19 '14 at 17:19
13

What you're using is known as a dynamic array which is different from a Pascal classic array. Dynamic arrays are variable in size and the index is 0 based.

Classic Pascal arrays are not 0 nor 1 based... It's up to the programmer where the index start or ends. The only compiler restriction is that the index must be an ordinal type. You can declare

procedure x;
    var
        IntArr: array[50..75] of Integer;
        StrArr: array[0..49] of string;
        DblArr: array[1..10] of Double;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jachguate
  • 16,976
  • 3
  • 57
  • 98
7

Delphi Pascal also has a nice feature that helps iterating through an array of any dimension:

Simply use for i:= Low(Array) to High(Array) do.... which is completely transparent to starting offset i.e. 0,1 or 5 or whatever.

Thaddy
  • 121
  • 1
  • 1
2

I tried to edit the above answer to improve it but the editor keeps rejecting my posting. Arrays can have negative indexes.

var
    A:array[-20..9] of integer;
    B:array[-30..-10] of integer;

These are both the same, an array of 20 integers but will not be treated the same by the compiler because the index range is different. Allows you to make the data fit the problem domain, not the other way around.

Now, a string like var S:string[200]; is technically equivalent to var s:packed array[0..200] of char where byte 0 is the length except when you use a string with no length or the specified length is greater than 255, then the string is 1 to whatever current size it is. Because strings can be dynamic it's not good to depend on the 0th element to contain length.

2

When you use SetLength(array, length) it is worth mentioning that it has indexes starting from 0 as mentioned up to length-1. Also in pascal index on array can be character from ANSI table. So you can define array like a:array['A'..'Z'] of integer. This comes in handy when you need to count all characters in your Strings or Char Array.