D3中如何实现动态数组?(50分)

  • 主题发起人 主题发起人 form2
  • 开始时间 开始时间
F

form2

Unregistered / Unconfirmed
GUEST, unregistred user!
如题
我知道D3以上能使用这种形式:tmpStr:array of string;
而D3不行,不要跟我说使用tstrings,因为如果是integer数组怎么办?
 
没有用过d3
 
tstrings怎么不能用? 那里面还可以附一个Object啊.
 
to kkyy:
我说不一定仅仅是串型数组,当然解决办法还是有的:
1:提前预测数组大小,开一个富裕空间
2:直接进行内存操作

不过好像有更好的办法吧!我等。。。
 
贴一篇文章(出处忘了 :( ):

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;
ResizeArray(p, 200, 400);
for i := 0 to 399 do
p^ := i;
ResizeArray(p, 400, 50);
for i := 0 to 49 do
p^ := i;
FreeArray(p, 50);

{$IFDEF CKRANGE}
{$UNDEF CKRANGE}
{$R+}
{$ENDIF}
end;

-----------------
另外,在使用动态实数数组的时候,数组元素类型最好声明为 single 或 double
因为 single 和 double 类型使用的是 FPU 运算,而 real 不是



看了上面的文章,应该比较清楚了吧。
 
good!我这边也在实验,代码如下,谢谢creation-zy,我看看你的ReSizeArray
type
TintArray=array [0..0] of integer;
PintArray=^TintArray;

implementation

{$R *.DFM}
procedure gets(s:string);
var
a,lg:integer;
myint:PintArray;
begin
lg:=length(s);
GetMem(myint,(lg+1)*sizeof(integer));
myint[0]:=lg;
for a:=1 to lg do myint[a]:=ord(s[a])-48;
for a:=1 to myint[0] do Form1.Memo1.Lines.Add(inttostr(myint[a]));
FreeMem(myint,myint[0]);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
if edit1.text='' then edit1.text:='1234567890';
gets(edit1.text);
end;

 
接受答案了.
 
后退
顶部