如何在Delphi中使用多维动态数组?

Small_C 2004-06-13 05:50:31
如何在Delphi中使用多维动态数组?
用SetLength不能多维,
也只能 0..255 !
...全文
1069 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chizengkun 2004-09-06
  • 打赏
  • 举报
回复
关注此问题,我很想了解的
Small_C 2004-09-06
  • 打赏
  • 举报
回复
谢了。
shifan 2004-07-03
  • 打赏
  • 举报
回复
在D7中用过array of array of ...一段时间,好像没什么问题
shifan 2004-07-03
  • 打赏
  • 举报
回复
同意StreamOne(梦醒)
毕竟array of array 是官方推荐的
Multidimensional dynamic arrays

以下来自Object Pascal Reference

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’t been allocated) you can assign values to the third column, for 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], I);

for J := Low(A[I]) to High(A[I]) do

A[I,J] := IntToStr(I) + ',' + IntToStr(J) + ' ';

end;

end;

Arockroll 2004-07-03
  • 打赏
  • 举报
回复
多维的的动态数组在DELPHI6中可能有点问题,在7中不知道会不会,
可以SETLENGTH,但在使用的时候会出错,建议用指针 用NEW 和DISPOSE来分配和释放
coollife 2004-07-03
  • 打赏
  • 举报
回复
type
A1=record
b:integer;
end;
A2=record
a:integer;
MySub: array of A1;
end; //这样你要多少维都可以,这里是二维
var
A:array of A2;
setlength(A,10);//分配
//赋值
for i:=0 to 9 do
begin
A[i].a:=i;
setlength(A[i].MySub,10)
for j:=0 to 9 do
A[i].Mysub[j].b:=j;
end;

//这样释放二维数组:(依次类推)
for i := 0 to 9 do SetLength(A[i], 0);
SetLength(A, 0);
StreamOne 2004-07-03
  • 打赏
  • 举报
回复
这样写

var
A: array of array of integer;

begin
setlength(A,100,100);
....................
end;
aibeyond2003 2004-07-03
  • 打赏
  • 举报
回复
var
v:variant
begin
v:=vararraycreate([1,4],varinteger);
v[1]:=1;
.....
end;
2.v:=vararraycreate([1,4,1,5],varinteger);
vararraylock(),vararrayunlock()可以初始化大数组!
v:=vararraycreate([1,10000],varbyte);
p:=vararraylock(v);
try
..........
finally
vararrayunlock(v);
end;
mediawizard 2004-07-03
  • 打赏
  • 举报
回复
在Delphi 7/8 中SetLength已经可以处理动态多维数组了。至于Delphi以前的版本我就不清楚了,抱歉。
yfyn 2004-07-03
  • 打赏
  • 举报
回复
说明文字在博客。 摘录: 在学习的时候曾经百度了一下,想查找关于delphiSafeArray的用法,没想到资料竟然如此之少,甚至连一篇相对完整的都没有。也许正如田师傅所说,现在高手们都在实行“技术封锁”了? 在CSDN技术心有一篇关于C++的使用,说的比较详细。鉴于手有田师傅写的代码,特拿来一部分对照自己学习的,翻译为Delphi版本。留作笔记,日后翻阅。 =============================================================================== SAFEARRAY的主要目的是用于automation的数组型参数的传递。因为在网络环境,数组是不能直接传递的,而必须将其包装成SafeArray。实质上SafeArray就是将通常的数组增加一个描述符,说明其维数、长度、边界、元 素类型等信息。SafeArray也并不单独使用,而是将其再包装到VARIANT类型的变量,然后才作为参数传送出去。在VARIANT的vt成员的 值如果包含VT_ARRAY|...,那么它所封装的就是一个SafeArray,它的parray成员即是指向SafeArray的指针。 SafeArray元素的类型可以是VARIANT能封装的任何类型,包括VARIANT类型本身。 Delphicom组件或外部调用需要传递一个对象或者数组,这个时候可以考虑使用SafeArray。比如三层架构通常需要将使用的ClientDataSet数据集传出,那么就可以将DataSet转换为SafeArray,再由其他模块转换接收。 ===============================================================================

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧