You should use an Iterator to iterate and remove an iterator (not a list) function:
Iterator<Pulse> iter = pulseArray.iterator(); while (iter.hasNext()) { Pulse p = iter.next(); if (p.getCurrent()==null) iter.remove(); }
Note that the Iterator # remove function is called optional, but it is implemented by the ArrayList iterator.
Here is the code for this particular function from ArrayList.java:
765 public void remove() { 766 if (lastRet < 0) 767 throw new IllegalStateException(); 768 checkForComodification(); 769 770 try { 771 ArrayList.this.remove(lastRet); 772 cursor = lastRet; 773 lastRet = -1; 774 expectedModCount = modCount; 775 } catch (IndexOutOfBoundsException ex) { 776 throw new ConcurrentModificationException(); 777 } 778 } 779 780 final void checkForComodification() { 781 if (modCount != expectedModCount) 782 throw new ConcurrentModificationException(); 783 } 784 }
String expectedModCount = modCount; - this is why it will not throw an exception when you use it during iteration.
Denys seguret
source share