对JAVA集合进行遍历删除时务必要用迭代器
来源:互联网 发布:淘宝排名靠前技巧2017 编辑:程序博客网 时间:2024/06/06 10:52
1
2
3
4
5
6
7
8
9
10
11
12
public
static
void
main(String args[]) {
List<String> famous =
new
ArrayList<String>();
famous.add(
"liudehua"
);
famous.add(
"madehua"
);
famous.add(
"liushishi"
);
famous.add(
"tangwei"
);
for
(String s : famous) {
if
(s.equals(
"madehua"
)) {
famous.remove(s);
}
}
}
1
2
3
4
5
6
for
(Iterator<String> it = famous.iterator();it.hasNext();){
String s = it.next();
if
(s.equals(
"madehua"
)){
famous.remove(s);
}
}
1
2
3
4
5
6
for
(
int
i =
0
; i < famous.size(); i++) {
String s = famous.get(i);
if
(s.equals(
"madehua"
)) {
famous.remove(s);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
private
class
Itr
implements
Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int
cursor =
0
;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int
lastRet = -
1
;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int
expectedModCount = modCount;
public
boolean
hasNext() {
return
cursor != size();
}
public
E next() {
checkForComodification();
try
{
E next = get(cursor);
lastRet = cursor++;
return
next;
}
catch
(IndexOutOfBoundsException e) {
checkForComodification();
throw
new
NoSuchElementException();
}
}
public
void
remove() {
if
(lastRet == -
1
)
throw
new
IllegalStateException();
checkForComodification();
try
{
AbstractList.
this
.remove(lastRet);
if
(lastRet < cursor)
cursor--;
lastRet = -
1
;
expectedModCount = modCount;
}
catch
(IndexOutOfBoundsException e) {
throw
new
ConcurrentModificationException();
}
}
final
void
checkForComodification() {
if
(modCount != expectedModCount)
throw
new
ConcurrentModificationException();
}
}
0 0
- 对JAVA集合进行遍历删除时务必要用迭代器
- 对JAVA集合进行遍历删除时务必要用迭代器
- 对JAVA集合进行遍历删除时务必要用迭代器
- 对JAVA集合进行遍历删除时务必要用迭代器
- 对JAVA集合进行遍历删除时务必要用迭代器
- 对JAVA集合进行遍历删除时务必要用迭代器
- 对JAVA集合进行遍历删除时务必要用迭代器
- java里面在遍历集合的时候对集合进行添加或者删除修改时的并发修改异常
- Java 对集合进行倒序
- ConcurrentModificationException遍历集合时,不能对集合进行修改操作
- 遍历集合的同时对集合进行操作
- 集合map 对集合map进行取值 遍历循环
- 【Java】Java遍历删除集合中的元素
- Java集合怎么一边删除一边遍历
- Java 基础 之 集合遍历删除
- Java集合怎么一边删除一边遍历
- java android 对一个集合进行连续有选择的删除
- angularjs遍历集合添加值表格进行删除
- AppStore提交审核添加各个机型的尺寸
- JFinal中使用QuartzPlugin报ClassCastException解决方法
- 筑基-C语言中变量的作用域
- jsp九大内置对象
- 微信js-sdk 地理位置接口实例
- 对JAVA集合进行遍历删除时务必要用迭代器
- Nginx 安装配置
- 聊聊并发(三)——JAVA线程池的分析和使用
- 关于WEB-INF的目录下问题
- MVC框架的封装(五)控制器中连接数据库
- Sublime text 3 (Build 3114) license key
- HomeBrew的安装和简单使用
- 使用npm批量、快速删除没有用的fork-repos
- placeholder兼容