????ArrayList?????????java????????????????????????ArrayList?????????????м??????????:
??????????????????±??????
????ArrayList<String>al=new ArrayList<String>();
????al.add("a");
????al.add("b");
????//al.add("b");
????//al.add("c");
????//al.add("d");
????for(int i=0;i<al.size();i++){
????if(al.get(i)=="b"){
????al.remove(i);
????i--;
????}
????}
??????????У??????????????±????????????????????????ArrayList?????沿??????????????????λ?????copy???????????????????????±??????±????????ü?????????????????????
???????????????????
????ArrayList<String>al=new ArrayList<String>();
????al.add("a");
????al.add("b");
????al.add("b");
????al.add("c");
????al.add("d");
????for(String s:al){
????if(s.equals("a")){
????al.remove(s);
????}
????}
????????????????????????????????????????????????????????????????????????????????????????????????
????Exception in thread"main"java.util.ConcurrentModificationException
????at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:886)
????at java.util.ArrayList$Itr.next(ArrayList.java:836)
????at com.mine.collection.TestArrayList.main(TestArrayList.java:17)
??????????????????????ArrayList??????????????????????????????????????????????????????з?????????????????????????????????????????????д????????????
????final void checkForComodification(){
????if(modCount!=expectedModCount)
????throw new ConcurrentModificationException();
????}
????modCount????????????????remove?????????1?????????????С??????????????????????????????????ж??????????????????????????????????????????????????????????????remove??????
????private void fastRemove(int index){
????modCount++;
????int numMoved=size-index-1;
????if(numMoved>0)
????System.arraycopy(elementData??index+1??elementData??index??
????numMoved);
????elementData[--size]=null;//clear to let GC do its work
????}
????????????????????modCount????????????expectModCount???????????????????????????????????????????????????????????????????????????????????????????????????
????ArrayList<String>al=new ArrayList<String>();
????al.add("a");
????al.add("b");
????al.add("b");
????al.add("c");
????al.add("d");
????Iterator<String>iter=al.iterator();
????while(iter.hasNext()){
????if(iter.next().equals("a")){
????iter.remove();
????}
????}
?????????????????????????????????????????????????expectedModCount?modCount??????????????????
????public void remove(){
????if(lastRet<0)
????throw new IllegalStateException();
????checkForComodification();
????try{
????ArrayList.this.remove(lastRet);
????cursor=lastRet;
????lastRet=-1;
????expectedModCount=modCount;
????}catch(IndexOutOfBoundsException ex){
????throw new ConcurrentModificationException();
????}
????}
?????????????????????????????????????????????????????????????modCount??expectedModCount??????????????????????????????????в???????????????????????????????????????????ж?????????????????????????е?????
?????????????????????????????ConcurrentModificationException.??????????????????????
????List<String>list=new ArrayList<String>();
????//Insert some sample values.
????list.add("Value1");
????list.add("Value2");
????list.add("Value3");
????//Get two iterators.
????Iterator<String>ite=list.iterator();
????Iterator<String>ite2=list.iterator();
????//Point to the first object of the list and then??remove it.
????ite.next();
????ite.remove();
????/*The second iterator tries to remove the first object as well.The object does not exist and thus??a ConcurrentModificationException is thrown.*/
????ite2.next();
????ite2.remove();
???????????????ConcurrentModificationException??