Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array begin from 0 or 1 in Delphi 5.0 Pascal?

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?

like image 743
okami Avatar asked Nov 03 '10 00:11

okami


People also ask

How do you initialize an array in Pascal?

In Pascal, arrays are initialized through assignment, either by specifying a particular subscript or using a for-do loop. a: integer; a: = alphabet['A']; The above statement will take the first element from the array named alphabet and assign the value to the variable a.

How do you assign values to an array in Pascal?

To declare an array in Pascal, a programmer may either declare the type and then create variables of that array or directly declare the array variable. type array-identifier = array[index-type] of element-type; Where, array-identifier − indicates the name of the array type.

What is array type in Delphi?

In Delphi, there are two types of arrays: a fixed-size array which always remains the same size--a static array--and a dynamic array whose size can change at runtime.


2 Answers

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.

like image 116
Trinidad Avatar answered Sep 18 '22 01:09

Trinidad


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;
like image 30
jachguate Avatar answered Sep 18 '22 01:09

jachguate