Delphi 5.0 Pascal 中数组从 0 或 1 开始?
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
动态数组的索引以 0 开头
静态数组可以有任意索引
字符串的索引以 1 开头
无论如何,你总是可以使用 Low() 和 High () 函数返回数组的较低和较高索引。
为了处理字符串列表,最常用的类是TStringList,它位于Classes单元中。
Dynamic arrays' indexes begin with zero
Static arrays can have arbitrary indexes
Strings' indexes begin with one
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.
您使用的称为 动态数组,它与 Pascal 经典数组不同。动态数组的大小可变,索引从 0 开始。
经典 Pascal 数组不是基于 0 也不是 1...这取决于程序员索引的开始或结束位置。唯一的编译器限制是索引必须是序数类型。您可以声明
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
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.
我试图编辑上面的答案来改进它,但编辑一直拒绝我的帖子。数组可以有负索引。
它们都是相同的,都是 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.
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 tovar 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.当您使用 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 likea:array['A'..'Z'] of integer
. This comes in handy when you need to count all characters in your Strings or Char Array.