Array begin from 0 or 1 in Delphi 5.0 Pascal?

20,375

Solution 1

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.

Solution 2

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;

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
20,375
okami
Author by

okami

Updated on July 03, 2020

Comments

  • okami
    okami almost 4 years

    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?