h-r*_*rai 0 java collections iterator arraylist
是否可以执行以下操作?
List<String> list = new ArrayList<String>();
Iterator<String> iterator = list.listIterator();
populateList(list);
Run Code Online (Sandbox Code Playgroud)
我在使用数据填充列表之前创建一个列表并获取其迭代器.我能使用迭代器获取列表的元素吗?获取迭代器和填充数组的顺序是否会有所不同?
任何帮助将不胜感激.
是的,这很重要.当您在迭代器上使用或时,您将获得ConcurrentModificationException.迭代器是快速失败的,这意味着只要通过添加或删除元素,arraylist的基础结构发生变化,它就会抛出CME.hasNext()
next()
ArrayList
编辑:
Iterator checkForComodification()
先调用hasNext()和next()
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
112 次 |
最近记录: |