//在remove一條信息時(shí),
ArrayList的大小已經(jīng)改變(即list.size()已經(jīng)改變);
在i大于等于list.size()時(shí),循環(huán)跳出,后便的ArrayList不能再執(zhí)行;
所以必須在remove的同時(shí),執(zhí)行i--,即i=i-1;
現(xiàn)在才能遍歷所有List中的信息。也不能在用Iterator遍歷時(shí)使用remove,會(huì)拋異常。
public class TrickyAL {
publicstatic LinkedList<User> llu=newLinkedList<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(LinkedList<User>urlist,String auser){
Iterator itr=urlist.iterator();
int c=urlist.size();