import java.util.Collection; import java.util.HashSet; import java.util.Iterator;public class TestIterator {public static void main(String[] args){Collection books=new HashSet();books.add("java讲义");books.add("java的Ajax宝典");books.add("java 权威指南");//获取books集合对应的迭代器Iterator it=books.iterator();while(it.hasNext()){//it.hasnext() 返回的数据类型是Object的,所以要进行强制类型转换String book=(String) it.next();System.out.println(book);if(books.equals("java讲义")){it.remove();}// 对book复制,不会改变books的本身book="测试字符串";}System.out.println(books);} }
import java.util.Collection; import java.util.HashSet; import java.util.Iterator;public class TestIterator {public static void main(String[] args){Collection books=new HashSet();books.add("java讲义");books.add("java的Ajax宝典");books.add("java 权威指南");//获取books集合对应的迭代器Iterator it=books.iterator();while(it.hasNext()){//it.hasnext() 返回的数据类型是Object的,所以要进行强制类型转换String book=(String) it.next();System.out.println(book);if(book.equals("java讲义")){//使用Iterator迭代过程中,不可修改集合元素,下面代码引发了异常 books.remove(book);}}//System.out.println(books); } }
java的Ajax宝典
java讲义
java 权威指南
[java的Ajax宝典, java讲义, java 权威指南]