delphi动态数组问题?

zwl232 2007-04-26 12:05:59
我写的程序为:
var
I:integer;
begin
I:=ComboBox1.Items.Count-1;
SetLength(str,I);
I:=0;
for I:=0 to ComboBox1.Items.Count-1 do
begin
str[i]:=ComboBox1.Items[I];
end;
为什么运行之后出错?
...全文
693 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
HsWong 2007-04-29
  • 打赏
  • 举报
回复
lihuasoft正解
小呆之家 2007-04-29
  • 打赏
  • 举报
回复
var
I:integer;
str:array of string;//.....?
begin
I:=ComboBox1.Items.Count;
SetLength(str,I);//如果不是上面的定义,你只分配了一个字符串,并不是字符串数组。
I:=0;
for I:=0 to ComboBox1.Items.Count-1 do
begin
str[i]:=ComboBox1.Items[I];
end;
chenzhuo 2007-04-26
  • 打赏
  • 举报
回复
var
I:integer;
begin
I:=ComboBox1.Items.Count; //不要-1
SetLength(str,I);
I:=0;
for I:=0 to ComboBox1.Items.Count-1 do
begin
str[i+1]:=ComboBox1.Items[I]; //str[i+1]
end;
Rophy_Liu 2007-04-26
  • 打赏
  • 举报
回复
字符串的下标引用不能从0开始,应该写为str[i+1]:=ComboBox1.Items[I];
lihuasoft 2007-04-26
  • 打赏
  • 举报
回复
楼主已说了是"动态数组", 不是字符串 . 动态数组的下标是从0开始的.

所以,只改这一句就可以了:

I:=ComboBox1.Items.Count; //不要-1

另外, 最好把这句for I:=0 to ComboBox1.Items.Count-1 do

改为 for I:=Low(Str) to High(Str) do

如果还出问题, 那么就可能是运行过程中ComboBox1.Items.Count变化, 下标越界了.

16,742

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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