Delete a set of elements to avoid a null pointer

1, using the original for loop

for (int I = 0; I <intList.size (); I ++) { 
    IF (intList.het (I) 13 is ==) { 
        intList.remove (I); 
        I -; // updated to delete the contents on the set, leading to a final point to be empty. So the loop variable - can be. 
    } 

2, using an iterator (the official recommended)

Iterator <Integer> = intList.iterator IT (); 
WHERE (it.hasNext ()) { 
    IF (it.next () == 13 is) {//it.next () method to return the current element 
        it.remove ( ); 
    }

Guess you like

Origin www.cnblogs.com/CatcherLJ/p/11314493.html