I think I may see the issue. Are you depopulating within the loop? This is dangerous because you’re incrementing through it at the same time. Look at it like this, say we populate an array like this (all pseudo-code mind you):
0 => a
1 => b
2 => c
3 => d
Then we loop through it and remove entries:
for (i = 0; i < array.length; i++)
{
print(i + " -> " + array[i])
remove(i);
}
As we cycle through, we keep resizing the array and rearranging our list.
After one cycle we see “0 → a” printed, and we’ve removed element 0, but the array looks like this now:
0 => b
1 => c
2 => d
Notice that everything has shifted up. In addition, the array length has shrunk by 1 which will be important later. But now our “i” variable has incremented to the value of “1”, so on our second cycle it prints “1 → c”, skipping element 0 (value “b”) altogether. Our array now looks like:
0 => b
1 => d
The “i” variable is now set to the value of 2. Additionally, the array has shrunk to a length of 2. Now when it re-evaluates the loop it checks if “i” is less than the array length (is 2 < 2), the answer is “no”, so it skips your last entry.
You have two options: rewrite your loop to look like this:
var originalLength = array.length;
for (i = 0; i < originalLength; i++)
{
remove(0); //always remove the "first" element because the array keeps sizing down
}
Or loop in reverse:
for (i = array.Length - 1; i >= 0; i--)
{
remove(i); //keeps removing the "last" entry as the array shrinks
}
Hopefully that makes sense; kinda hard for me to explain.
EDIT: Generally, for simplicity’s sake, you can opt not to alter the array while looping through it and wait until after to depopulate it:
for (i = 0; i < array.Length; i++)
{
doStuff(array[i])
}
for (i = array.Length; i >= 0; i--)
{
remove(i);
}
Kind of inefficient, but might make things easier to separate the operations.