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();
}
}
|