From: http://blog.sina.com.cn/s/blog_4914a33b0100xtyj.html
// When removing a message,
The size of arraylist has changed (that is, list. Size () has changed );
When I is greater than or equal to list. Size (), the loop jumps out, And the arraylist can no longer be executed;
So you must execute I --, that is, I = I-1;
Now we can traverse all the information in the list. You cannot use the remove feature in the iterator traversal. An exception is thrown. (Java. util. concurrentmodificationexception; At java. util. arraylist $ arraylistiterator. Next)
Public class trickyal {
Publicstatic upload list <user> LlU = newclientlist <user> ();
Publicstatic hashmap <string, user> hmu = newhashmap <string, user> ();
Publicstatic arraylist <user> ulist = newarraylist <user> (arrays. aslist
(Newuser [] {
New User (2, "Eric "),
New User (3, "Eric "),
New User (1, "Aaron "),
New User (4, "Eric ")
}
));
Static {
LlU. Add (new user (1, "Aaron "));
LlU. Add (new user (2, "Eric "));
LlU. Add (new user (3, "Tom "));
Hmu. Put ("1", new user (3, "Tom "));
Hmu. Put ("2", new user (2, "Eric "));
Hmu. Put ("3", new user (1, "Leo "));
Hmu. Put ("3", new user (4, "Scott "));
}
Publicstatic voiddeleteuser (arraylist <user> urlist, string auser ){
Iterator itr = urlist. iterator ();
Int c = urlist. Size ();
For (INT I = 0; I <C; I ++ ){
If (auser. Equals (urlist. Get (I). Name )){
Urlist. Remove (I );
I --;
C --;
}
}
}
Public static voiddeleteuser (partition list <user> urlist, string auser ){
Iterator itr = urlist. iterator ();
Int c = urlist. Size ();