如何在delphi3中定义一个可变长度的数组类型(20分)

牛龙

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在delphi3中定义一个可变长度的数组类型
谢谢
急!!
 
请参阅Delphi Help中的"Dynamic arrays":

var A, B: array of Integer;

SetLength(A, 10);
 
D3还不内置支持可变长度的数组类型,可以用
function VarArrayCreate(const Bounds: array of Integer
VarType: Integer): Variant;创建,
并用 procedure VarArrayRedim(var A: Variant
HighBound: Integer);扩充.
 
可以这样来定义:

例如:

type

IntArray = [0..0] of Integer;
PIntArray = ^IntArray;


var
dynamicArray: PIntArray;

要用的时候,
FreeMem(dynamicArray);
GetMem(dynamicArray,Count);// count为

应用时
dynamicArray^ 就可以

注意:
改变大小时,一定要先FreeMem

 
>>" 注意:
改变大小时,一定要先FreeMem"

这样原有的数据不会丢掉吗?
 
在.../lib/mxarrays.dcu中有很多动态数组对象(无源码, 无说明):
TDoubleArray, TIntArray, TRealArray, TSmallIntArray, TCurrencyArray,
TWordArray, TPointerArray, TStringArray...

TIntArray = class(TBaseArray);
public
constructor Create(itemcount,dummy:integer);
destructor destory;
//or constructor Create;
procedure Clear;
procedure InsertAt(Index: integer
Value: integer);
procedure Insert(Index: integer
Value: integer);
procedure Delete(Index: integer);
procedure Exchange(Index1, Index2: integer);
function IndexOf(Item: integer): integer;
function FindItem(var Index: integer
Value: integer): boolean;
procedure Sort(Compare: TCompareProc);
property CompareProc: TCompareProc;
property Duplicates: TDuplicates;
property SortOrder: TSortOrder;
property Capacity: integer;
property Limit: integer;
property Count: integer;
end;

其他类似
 
parray改变大小时用reallocmem不就行了? 除非缩小, 原始数据不会改变(缩小也只
去掉后面的, 和delete last 一样效果)
 
使用Tlist
 
多人接受答案了。
 
顶部