import java.util.*;
public class Test{

public static void main(String[] args) {

Collection c = new HashSet();

c.add(new Name("F11", "l11"));
c.add(new Name("F2", "l2"));
c.add(new Name("F31", "l31"));

for(Iterator i=c.iterator(); i.hasNext();){
Name n = (Name)i.next();
if(n.getFirstName().length()<3){
n.remove();
}

}
System.out.println(c); }

}

class Name {

String firstName, lastName;

public Name(String firstName, String lastName) {
this.firstName = firstName; this.lastName = lastName;
}

public String getFirstName() {return firstName;}


}

解决方案 »

  1.   

    楼主没有理解方法的用法吧,呵呵,先不说别的,你看看你设计的Name类里有remove()方法没有呢?肯定没有吧,呵呵,在看看API,这个remove()方法是谁的呢?可以发现是Iterator类里的,所以只能是i.remove(),至于为什么是这样,看看API和容器的只是就知道了