Delphi 5.0 Pascal 中数组从 0 或 1 开始?

发布于 2024-09-30 08:45:26 字数 711 浏览 9 评论 0原文

我想在 Delphi 5.0 中创建一个 ArrayList。所以我找到了一个用这段代码实现它的解决方案:

var arr: array of String;

好的,但是每次我添加一些东西时我都会这样做:

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;

我是一名 C++ 程序员,我对 Pascal 一无所知。我总是听说 Pascal 数组索引是从 1 开始的,而不是从 0 开始的。因此,在上述过程中,我执行了 arr[len-1],因为(假定的)索引从  0 开始。

还有比 Pascal 数组更好的方法吗?就像 C++ 的 std::vector 一样?

I want to create an ArrayList in Delphi 5.0. So I found a solution achieving it with 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 array index started from 1, not 0. Therefore in the above procedure I do arr[len-1] because of a (presumed) index beginning at 0.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

邮友 2024-10-07 08:45:26

动态数组的索引以 0 开头

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

静态数组可以有任意索引

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;

字符串的索引以 1 开头

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

无论如何,你总是可以使用 Low() 和 High () 函数返回数组的较低和较高索引。

为了处理字符串列表,最常用的类是TStringList,它位于Classes单元中。

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.

顾忌 2024-10-07 08:45:26

您使用的称为 动态数组,它与 Pascal 经典数组不同。动态数组的大小可变,索引从 0 开始。

经典 Pascal 数组不是基于 0 也不是 1...这取决于程序员索引的开始或结束位置。唯一的编译器限制是索引必须是序数类型。您可以声明

procedure x;
    var
        IntArr: array[50..75] of Integer;
        StrArr: array[0..49] of string;
        DblArr: array[1..10] of Double;

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;
随波逐流 2024-10-07 08:45:26

Delphi Pascal 还有一个很好的功能,可以帮助迭代任何维度的数组:

只需使用 for i:= Low(Array) to High(Array) do....
这对于起始偏移量(即 0,1 或 5 或其他值)是完全透明的。

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.

星軌x 2024-10-07 08:45:26

我试图编辑上面的答案来改进它,但编辑一直拒绝我的帖子。数组可以有索引。

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

它们都是相同的,都是 20 个整数的数组,但编译器不会以相同的方式处理它们,因为索引范围不同。允许您使数据适合问题域,而不是相反。

现在,像 var S:string[200]; 这样的字符串在技术上等同于 var s:packed array[0..200] of char 其中字节 0 是长度除非您使用没有长度的字符串或指定的长度大于 255,否则该字符串将为 1 到当前大小。因为字符串可以是动态的,所以依赖第 0 个元素来包含长度是不好的。

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.

我早已燃尽 2024-10-07 08:45:26

当您使用 SetLength(array, length) 时,值得一提的是,它的索引从前面提到的 0 开始,一直到 length-1。另外,在 pascal 中,数组上的索引可以是 ANSI 表中的字符。因此,您可以定义像 a:array['A'..'Z'] of integer 这样的数组。当您需要计算字符串或字符数组中的所有字符时,这会派上用场。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文