Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List index out of bounds whilst deleting items

Tags:

listbox

delphi

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

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

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

list index out of bounds (5) !

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;

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 ?

like image 247
K.MuS Avatar asked Dec 15 '22 07:12

K.MuS


1 Answers

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.

  • 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 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.
like image 112
Ken White Avatar answered Dec 21 '22 23:12

Ken White