public class iterator {
public static void main(String[] args) {
Collection c=new HashSet();
c.add(new Name("f1","l1"));//Name是一个类,有两个私有属性firstName、lastName;
c.add(new Name("f2","l2"));
c.add(new Name("f3","l3"));
Iterator i=c.iterator();
while(i.hasNext()) {c.add(new Name("fff1","lll1"));
c.add(new Name("f2","l2"));
c.add(new Name("fff3","lll3"));
for(Iterator j=c.iterator();j.hasNext();) {
Name name=(Name)j.next();
if(name.getFirstName().length()<3) {
j.remove();
}
}
System.out.println(c);
}
}
}请问这个输出结果是什么呢?为什么?
public static void main(String[] args) {
Collection c=new HashSet();
c.add(new Name("f1","l1"));//Name是一个类,有两个私有属性firstName、lastName;
c.add(new Name("f2","l2"));
c.add(new Name("f3","l3"));
Iterator i=c.iterator();
while(i.hasNext()) {c.add(new Name("fff1","lll1"));
c.add(new Name("f2","l2"));
c.add(new Name("fff3","lll3"));
for(Iterator j=c.iterator();j.hasNext();) {
Name name=(Name)j.next();
if(name.getFirstName().length()<3) {
j.remove();
}
}
System.out.println(c);
}
}
}请问这个输出结果是什么呢?为什么?
c.add(new Name("f2","l2"));//没次都添加一个长度小于3的。这个删不了
c.add(new Name("fff3","lll3"));
for(Iterator j=c.iterator();j.hasNext();) {
Name name=(Name)j.next();
if(name.getFirstName().length()<3) {
j.remove();
}
}
c.add(new Name("f2","l2"));
c.add(new Name("fff3","lll3"));每次添加3 个, 你只能把
if(name.getFirstName().length()<3) {
j.remove();
第二个 删除
你的c是个对象所以打印出来的是对象的内存地址
要想打印他的成员变量去重写他的toString()方法