删除项目时列出索引超出范围 [英] List index out of bounds whilst deleting items

查看:24
本文介绍了删除项目时列出索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解决了我的问题,但我需要知道为什么会向我提出这个问题?!

I solved my problem but I need to know why this problem raised to me ?!

我写了一个项目,将文件加载到listBox,然后一一删除字符串,

I write a project that load file to listBox then delete the strings one by one,

但是当我删除 listBox 字符串时,我会遇到这个异常!

but when I delete listBox strings this exception raised to me!

列表索引越界 (5) !

我输入这个 for 循环来读取列表框并删除字符串:

I type this for loop to read list box and delete strings:

for i := 0 to ListBox3.Count -1  do
  begin
      ShowMessage(ListBox3.Items[i]);
       ListBox3.items.Delete(i);
  end;

我的问题通过在 for-loop 语句中做一点改变来解决

and my problem solved by do a little change in for-loop statement

for i := ListBox3.Items.Count - 1 downto 0 do
  begin
      ShowMessage(ListBox3.Items[i]);
       ListBox3.items.Delete(i);
  end;

为什么第一条语句引发了异常,而第二条则正常?

Why the first statement raised an exception, and the second one work fine ?

推荐答案

通过删除向前移动的项目,您正在切断您所站立的分支.:-) 循环的上限仅在循环开始之前计算一次,如果您删除项目,则列表中的项目现在比计算边界时的数量要少.

By deleting items moving forward, you're cutting the branch off that you're standing on. :-) The upper bounds of the loop is only evaluated once, before the loop begins, and if you delete items there are now fewer in the list than there were when the bound was calculated.

  • 评估循环限制(例如,List.Count - 1 = 5).它的有效索引是 [0..4]
  • 循环开始,您检索 List[0] 并将其删除.列表计数 = 4,bounds 仍然是 5
  • 索引递增,检索并删除 List[1].List Count = 3,bounds 仍然是 5
  • 索引递增,检索并删除 List[2].List Count = 2,bounds 仍然是 5.
  • 索引递增,您检索 List[3] - 糟糕!列表中只有 2 个项目,现在位于索引 [0..1] - 列表索引超出范围(3).
  • Loop limit is evaluated (for example, List.Count - 1 = 5). Valid indexes into it are [0..4]
  • The loop starts, and you retrieve List[0] and delete it. List Count = 4, bounds is still 5
  • The index is incremented, you retrieve and delete List[1]. List Count = 3, bounds is still 5
  • The index is incremented, you retrieve and delete List[2]. List Count = 2, bounds is still 5.
  • The index is incremented, you retrieve List[3] - Oops! There are only 2 items in the list, now at indexes [0..1] - List index out of bounds(3).

通过向后迭代,即使边界仍然只在开始时计算,您也会从末尾删除项目并同时减少计数.

By iterating backwards, even though the bounds is still only calculated at the beginning, you're removing the items from the end and decrementing the count at the same time.

  • Bounds 为 5,您检索 List[4] 并将其删除.计数现在是 4,边界仍然是 5
  • 索引递减,您检索 List[3] 并将其删除.现在计数为 3,边界仍为 5
  • 索引递减,您检索 List[2] 并将其删除.现在计数为 2,边界仍为 5.
  • 索引递减,您检索并删除 List[1].现在计数为 1,边界仍为 5.
  • 索引递减,您检索并删除 List[0].List 现在是空的,但是我们已经达到了循​​环的终止条件(downto 0)并且循环安全退出了.
  • Bounds is 5, and you retrieve List[4] and delete it. Count is now 4, bounds is still 5
  • Index is decremented, and you retrieve List[3] and delete it. Count is now 3, bounds is still 5
  • Index is decremented, and you retrieve List[2] and delete it. Count is now 2, bounds is still 5.
  • Index is decremented, and you retrieve and delete List[1]. Count is now 1, bounds is still 5.
  • Index is decremented, and you retrieve and delete List[0]. List is now empty, but we've reached the terminating condition of the loop (downto 0) and the loop exits safely.

这篇关于删除项目时列出索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆