对JAVA集合进行遍历删除时务必要用迭代器
来源:互联网 发布:php contains 编辑:程序博客网 时间:2024/06/03 03:16
点击打开链接
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遍历集合添加值表格进行删除
- 04-树5. File Transfer (25)
- 【BZOJ3996】【TJOI2015】线性代数 最小割
- 数据存储格式
- linux 命令系列之 用户和组信息文件(22)
- goldengate translations 的捕捉
- 对JAVA集合进行遍历删除时务必要用迭代器
- .9图的绘制
- 玩命牛的成长记录(十二)——会话
- 百度地图Android POI检索
- 一句话记录:Visual Studio 把需引用的文件自动复制到程序输出窗口中的命令
- 改造DuContact,实现动态加载
- linux 命令系列之 用户目录(23)
- 常用awk命令(转)
- (转载)Android Fragment 真正的完全解析(上)