我需要做这样的事情......
Collection<T> myCollection; ///assume it is initialized and filled
for(Iterator<?> index = myCollection.iterator(); index.hasNext();)
{
Object item = index.next();
myCollection.remove(item);
}
显然这会抛出 ConcurrentModificationException ...
所以我已经尝试过了,但看起来并不优雅/高效,并引发了 Type safety: Unchecked cast from Object to T 警告
Object[] list = myCollection.toArray();
for(int index = list.length - 1; index >= 0; index--) {
myCollection.remove((T)list[index]);
}