二维数组的赋值(30分)

  • 主题发起人 主题发起人 goease
  • 开始时间 开始时间
G

goease

Unregistered / Unconfirmed
GUEST, unregistred user!
在过程里,我接收了一个传过来的二维数组(还不知道怎么正确传递动态数组),在程序
语句里,我想一个一个赋值时,报错时这样的,array type required,我看过了,类型都是
integer型的,要不是传数组的时候出错了,请高手看看,嘻嘻,本来还想把数组里的值放到
数据库里面去的,现在卡住了
 
要用动态数组,首先要setlength(数组名,数组长度),然后才能用。
接受二维数组?是不是这样:
type
YourType=record
a:integer;
b:string
end;
function kk:YourType.
begin
.......
end;
这个函数就返回YourType类型的值。
接受这个函数的返回值可以这样。
var
Temp:YourType;
begin
Temp:=kk;
//接下来就可以用kk的返回值了.
edit1.text:=inttostr(Temp.a);
edit2.text:=Temp.b;
end;
 
摘自delphi的help.key word是Dynamic Arrays
To declare multidimensional dynamic arrays, use iterated array of ... constructions. For example,

type TMessageGrid = array of array of string;

var Msgs: TMessageGrid;

declares a two-dimensional array of strings. To instantiate this array, call SetLength with two integer arguments. For example, if I and J are integer-valued variables,

SetLength(Msgs,I,J);

allocates an I-by-J array, and Msgs[0,0] denotes an element of that array.
You can create multidimensional dynamic arrays that are not rectangular. The first step is to call SetLength, passing it parameters for the first n dimensions of the array. For example,

var Ints: array of array of Integer;

SetLength(Ints,10);

allocates ten rows for Ints but no columns. Later, you can allocate the columns one at a time (giving them different lengths); for example

SetLength(Ints[2], 5);

makes the third column of Ints five integers long. At this point (even if the other columns haven抰 been allocated) you can assign values to the third column梖or example, Ints[2,4] := 6.
The following example uses dynamic arrays (and the IntToStr function declared in the
SysUtils unit) to create a triangular matrix of strings.

var

A : array of array of string;
I, J : Integer;
begin
SetLength(A, 10);
for I := Low(A) to High(A) do
begin
SetLength(A, I);
for J := Low(A) to High(A) do
A[I,J] := IntToStr(I) + ',' + IntToStr(J) + ' ';
end;
end;
 
后退
顶部