日期:2014-05-20 浏览次数:20673 次
import java.util.*; public class IteratorTest{ public static void main(String[] args){ Collection c = new HashSet();// Collection c = new HashSet(); c.add(new Name("f1","l1")); c.add(new Name("f2","l2")); c.add(new Name("f3","l3")); //System.out.println(c); Iterator i = c.iterator(); if(i.hasNext()){ Name n = (Name)i.next(); // 谁能帮我解释下这个语句的作用 System.out.println(n.getFname() + " "); } for(Iterator l = c.iterator();l.hasNext();){ Name n = (Name)l.next(); // 谁能帮我解释下这个语句的作用 if(n.getLname() == "l2"){ l.remove(); } } System.out.println(c); } static class Name{ private String fname; private String lname; public Name(String fname,String lname){ this.fname = fname; this.lname = lname; } public String getFname(){ return fname; } public String getLname(){ return lname; } public String toString(){ return fname +" " + lname; } } }