一、静态数组
1、定义
一维数组定义:arrA:array[下标..上标] of 数据类型
二维数组定义:arrB:array [下标1..上标1,下标2..上标2] of 数据类型
备:arrA,arrB为定义的数组变量。
2、引用
一维数组引用:arrA[n],n为从下标至上标的一个量
二、动态数组
1、定义
一维数组定义:arrA:array of 数据类型
二级数组定义:arrB:array of array of 数据类型
2、重置长度
一维:setlength(arrA,长度);
二维:setlength(arrB,长度1,长度2);
3、引用
一维:arrA[n];
二维:arrB[m,n];
三、例子:
unit Unit1;
interface
uses
// The System unit does not need to be defined
SysUtils,
Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);
var
singleArray : array of string;
multiArray : array of array of Word;
i, j : Integer;
begin
// Set the length of a single dimension array
SetLength(singleArray, 4);
// Now fill it up : note that dynamic arrays start at 0
ShowMessage('Single dimensional array :');
for i := 0 to 3 do
begin
singleArray[i] := 'String '+IntToStr(i);
ShowMessage('Element '+IntToStr(i)+' = '+singleArray[i]);
end;
// Set the length of a multi dimensional array
SetLength(multiArray, 2, 3);
// Now fill it up
ShowMessage('Multi-dimensional array :');
for i := 0 to 1 do
for j := 0 to 2 do
begin
multiArray[i,j] := i + j;
ShowMessage('Element '+IntToStr(i)+','+IntToStr(j)+' = '+
IntToStr(multiArray[i,j]));
end;
end;
end.
阅读(2547) | 评论(0) | 转发(0) |