I wrote a project to load the file into the listBox and then delete the strings one by one,
But when I delete the listBox string, this exception triggered my interest !
List index is out of range (5)!
I enter this for loop to read the list box and delete the string:
for i := 0 to ListBox3.Count -1 do
begin
ShowMessage(ListBox3.Items[i]);
ListBox3.items.Delete(i);
end;
And by doing in the for loop statement A little change to solve my problem
for i := ListBox3.Items.Count-1 downto 0 do
begin
ShowMessage(ListBox3.Items[ i]);
ListBox3.items.Delete(i);
end;
Why does the first statement raise an exception and the second statement works fine?
>Evaluate the loop limit (e.g., List.Count – 1 = 5). The effective index is [0..4]
>The loop starts, you retrieve List[0] and delete it. List count = 4,
The limit is still 5
>The index is incremented, you retrieve And delete List[1]. List count = 3, the limit is still 5
>The index is incremented, you retrieve and delete List[2]. The list count=2, the boundary is still 5.
>The index is incremented, and you Retrieve List [3]-Oops! There are only 2 items in the list, now at index [0..1]-the list index is out of range (3).
By iterating backwards, even if the boundary is still only calculated at the beginning, you will be from the end Delete the item and reduce the count at the same time.
> Bounds is 5, you retrieve List [4] and delete it. The earl is now 4, and the limit is still 5> The index is decrementing, you retrieve List [3] and Its delete. The earl is now 3, the limit is still 5> the index is decreasing, you retrieve the list [2] and delete it. The earl is now 2, and the limit is still 5.> the index is decreasing, you search and delete List [1]. The earl is now It is 1, the limit is still 5.>The index is decreasing, you retrieve and delete List [0]. The List is now empty, but we have reached the termination condition of the loop (downto 0) and the loop exits safely.
I solved my problem, but I need to know why this problem occurs?
I wrote a project to load the file into the listBox and then delete the strings one by one,
But when I delete the listBox string, this exception triggered my interest !
List index is out of range (5)!
I enter this for loop to read the list box and delete the string:
for i := 0 to ListBox3.Count -1 do
begin
ShowMessage(ListBox3.Items[i]);
ListBox3.items.Delete(i);
end;
And by doing in the for loop statement A little change to solve my problem
for i := ListBox3.Items.Count-1 downto 0 do
begin
ShowMessage(ListBox3.Items[ i]);
ListBox3.items.Delete(i);
end;
Why does the first statement raise an exception and the second statement works fine?
By deleting the items that move forward, you are cutting off the branch you are standing on. :-)The upper limit of the loop is calculated only once before the loop starts, if deleted Item, the items in the list are now fewer than when calculating the boundary.
>Evaluate the loop limit (for example, List.Count – 1 = 5). The effective index is [0..4]
>The loop starts, you retrieve List[0] and delete it. List count = 4,
The limit is still 5
>The index is incremented, you retrieve and delete List[1]. List count = 3, The limit is still 5
>The index is incremented, you retrieve and delete List[2]. The list count = 2, the boundary is still 5.
>The index is still incremented, and you retrieve List[3]-Oops! There are only 2 items in the list, now at index [0..1]-the list index is out of range (3).
By iterating backwards, even if the boundary is still only calculated at the beginning, you will be from the end Delete the item and reduce the count at the same time.
> Bounds is 5, you retrieve List [4] and delete it. The earl is now 4, and the limit is still 5> The index is decremented, you retrieve List [3] and Its delete. The earl is now 3, the limit is still 5> the index is decreasing, you retrieve the list [2] and delete it. The earl is now 2, and the limit is still 5.> the index is decreasing, you search and delete List [1]. The earl is now It is 1, and the limit is still 5.>The index is decreasing, you retrieve and delete List [0]. The List is now empty, but we have reached the termination condition of the loop (downto 0) and the loop exits safely.