I have one ListView, it contains check boxes. By default all check boxes are checked, if we uncheck any check box the position will be added to mCheckedArrayList. Now I want remove all unchecked positions from the Listview. I used the following code:
for(int i=0;i<=mCheckedArrayList.size;i++){
int removePosition=mCheckedArrayList.get(i);
mDisplayArrayList.remove(removePosition);
}
But it is giving an ArrayIndexOutOfBounds exception.
You are removing objects in the wrong order. You should always reverse the order of the loop if you are removing elements within the loop:
As a side note, if you wanted to loop through front first (in cases where you are not deleting elements), you would have to change your loop end condition to the following:
Thanks to Martín for the comment. To be aware of modifications you could always use a ‘final ref’ of the array.