package com.crf.java.c7;
import java.util.*;public class TestIterator
{
public static void main(String[] args) 
{
//创建一个集合
Collection books = new HashSet();
books.add("11111");
books.add("22222");
books.add("33333");
//获取books集合对应的迭代器
Iterator it = books.iterator();
while(it.hasNext())
{
String book = (String)it.next();
System.out.println(book);
if (book.equals("22222"))
{
it.remove();
}

book= "44444";
}
System.out.println(books);
}
}
运行结果  :
22222
33333
11111
[33333, 11111]书上说Iterotor()并不是集合元素本身传给了迭代变量   而是把集合元素的值传给了迭代变量      既然如此  那么为什么最后不输出[22222 33333 11111]呢?求解????

解决方案 »

  1.   


    你可以自己去看看API中Iterator关于remove函数的介绍remove
    void remove()从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。每次调用 next 只能调用一次此方法。如果进行迭代时用调用此方法之外的其他方式修改了该迭代器所指向的 collection,则迭代器的行为是不确定的。 抛出: 
    UnsupportedOperationException - 如果迭代器不支持 remove 操作。 
    IllegalStateException - 如果尚未调用 next 方法,或者在上一次调用 next 方法之后已经调用了 remove 方法。
      

  2.   

    HashSet其实就是一个只有键没有value的HashMap,HashMap中的迭代器是有内部类HashIterator实现的,看看源码中的remove方法就知道了 private abstract class HashIterator<E> implements Iterator<E> {
            Entry<K,V> next;    // next entry to return
            int expectedModCount;    // For fast-fail 
            int index;        // current slot 
            Entry<K,V> current;    // current entry        HashIterator() {
                expectedModCount = modCount;
                Entry[] t = table;
                int i = t.length;
                Entry<K,V> n = null;
                if (size != 0) { // advance to first entry
                    while (i > 0 && (n = t[--i]) == null)
                        ;
                }
                next = n;
                index = i;
            }        public boolean hasNext() {
                return next != null;
            }        Entry<K,V> nextEntry() { 
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
                Entry<K,V> e = next;
                if (e == null) 
                    throw new NoSuchElementException();
                    
                Entry<K,V> n = e.next;
                Entry[] t = table;
                int i = index;
                while (n == null && i > 0)
                    n = t[--i];
                index = i;
                next = n;
                return current = e;
            }        public void remove() {
                if (current == null)
                    throw new IllegalStateException();
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
                /*Use the Entry get method instead of direct access to the key ibm@117216 */
                Object k = current.getKey(); /*ibm@117216*/
                current = null;
                HashMap.this.removeEntryForKey(k);
                expectedModCount = modCount;
            }    }
    另外。迭代器在进行迭代的时候不允许另外的迭代器或原集合本身进行了修改
    看下面代码Collection books = new HashSet();
    books.add("11111");
    books.add("22222");
    books.add("33333");
    //获取books集合对应的迭代器
    Iterator it = books.iterator();
    while(it.hasNext())
    {
    String book = (String)it.next();
    System.out.println(book);
    if (book.equals("11111"))
    {
    //it.remove();
    books.remove("11111");
    } book= "44444";
    }
    System.out.println(books);就会报上面的ConcurrentModificationException();
      

  3.   

    你执行了
    if (book.equals("22222"))
    {
    it.remove();//这里指迭代器指向的集合当前元素,对该元素进行删除
    }
    最后怎么可能还会有”22222“
      

  4.   

    set是一个无序不重复集合,遍历的时候以hsacode为参照,所以不一定是按照写入数据的循序读取的,iterator是对set的引用,所以remove可以删除set里面数据
      

  5.   

    it.remove();是关键地方。lz可以多了解下,set,list,map的知识。
      

  6.   

    三个字符串 
    222222它的index实际是0
    333333它的index实际是1
    111111它的index实际是2
    这个迭代器它刚开始的位置是0的前面。当第一次调next();时,它的指针才指向index==0
    于是这时你把222222给删除了。所以最后打印出333333和111111
      

  7.   

    remove 是把“222222”对应的索引删除了。即删除了map中的KEY 所以value也随之删除了。即“222222”就不存在这个list中了啊。
      

  8.   

    都it.remove()啦~
    当然没有22222这个元素啦~