No, as other answers have suggested, ConcurrentModificationException
will not occur in the given code.
So when does ConcurrentModificationException occur?
In single threaded environment it will usually occur when you use Iterators to loop over collection and modify it simultaneously.
Why?
If you check the source code of Iterator implementations, it always does checkForComodification() whenever you use any method of iterator(e.g next(), remove()). The code for that method in ArrayList.java for instance is:
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
Where modCount is a static variable maintained at ArrayList class level which signifies the number of times this list has been structurally modified.
And expected modification count expectedModCount, is maintained at Iterator class level, which is initially equal to modCount.
Thus whenever the expected number of modifications do not match the modifications actually done, you get the exception.
As in your case you have not used any of this, there will be no check for Co-modification, and hence you will not get ConcurrentModificationException
.
Note:
As stated in this answer, you will get IndexOutOfBoundsException
since you have checked index < len
in your for loop and len
is initialized to initial size of array.
A simple solution to this would be:
for(int index=0;index < nums.size;index++)