发信人: redfox (Z掉了几乎所有的版...), 信区: BorlandDev
标 题: Delphi编程 -- 15.2 如何Resize 一个数组
发信站: 哈工大紫丁香 (2000年09月01日17:37:24 星期五), 站内信件
DELPHI中怎样建立一个动态数组? 如何Resize 一个数组?
问 How Do I Create A Dynamic Arrays In Delphi? And How Can I Resize A
Array?
答 You cannot resize a non-dynamic array in Pascal. You can
create and resize a dynamically created array. To do this, you
must create the dynamic array, turn range checking off,
and access the array members via a variable only, or you
will receive runtime and compile time errors. Since you will
access the array through a pointer variable, you can dynamically
resize the array by creating a new array in memory, then copy all the
valid elements of the original array to the new array, then free the
memory
for the original array, and assign the new array's pointer back to the
original array pointer.
Example:
type
TSomeArrayElement = integer;
PSomeArray = ^TSomeArray;
TSomeArray = array[0..0] of TSomeArrayElement;
procedure CreateArray(var TheArray : PSomeArray;
NumElements : longint);
begin
GetMem(TheArray, sizeof(TSomeArrayElement) * NumElements);
end;
procedure FreeArray(var TheArray : PSomeArray;
NumElements : longint);
begin
FreeMem(TheArray, sizeof(TSomeArrayElement) * NumElements);
end;
procedure ReSizeArray(var TheArray : PSomeArray;
OldNumElements : longint;
NewNumElements : longint);
var
TheNewArray : PSomeArray;
begin
GetMem(TheNewArray, sizeof(TSomeArrayElement) * NewNumElements);
if NewNumElements > OldNumElements then
Move(TheArray^,
TheNewArray^,
OldNumElements * sizeof(TSomeArrayElement)) else
Move(TheArray^,
TheNewArray^,
NewNumElements * sizeof(TSomeArrayElement));
FreeMem(TheArray, sizeof(TSomeArrayElement) * OldNumElements);
TheArray := TheNewArray;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
p : PSomeArray;
i : integer;
begin
{$IFOPT R+}
{$DEFINE CKRANGE}
{$R-}
{$ENDIF}
CreateArray(p, 200);
for i := 0 to 199 do
p^[i] := i;
ResizeArray(p, 200, 400);
for i := 0 to 399 do
p^[i] := i;
ResizeArray(p, 400, 50);
for i := 0 to 49 do
p^[i] := i;
FreeArray(p, 50);
{$IFDEF CKRANGE}
{$UNDEF CKRANGE}
{$R+}
{$ENDIF}
end;
-----------------
另外,在使用动态实数数组的时候,数组元素类型最好声明为 single 或
double
因为 single 和 double 类型使用的是 FPU 运算,而 real 不是
---------------------
回答2
It seems that the "best" way to deal with this is not necessarily by
declaring a pointer type and doing dynamic memory allocation etc.(as
most of
you approached this problem) but (as one of you pointed out) to use
Delphi's
"Variant support routines" (see Help) where you can create dynamic
arrays of
Variants and manipulate them using the SYSTEM unit. Some of the
function
names are:
VarArrayCreate -- Creates a variant array of given bounds and type.
VarArrayDimCount -- Returns the number of dimensions of a given
variant.
VarArrayHighBound -- Returns the high bound of a given dimension of
a given variant array.
VarArrayLock -- Locks a given variant array.
VarArrayLowBound -- Returns the low bound of a given dimension of a
given variant array.
VarArrayOf -- Returns a given variant array with specified elements.
VarArrayRedim -- Resizes a given variant array by changing the high
bound.
VarArrayUnLock -- Unlocks a given variant array.
--
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.227.107]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.228毫秒