cantiaozi 2017-03-23 02:10 采纳率: 50%
浏览 937
已采纳

写了个java小程序,编译通过了,运行时却没有任何输出,是为什么,求教!

import java.util.*;
public class TestIterator {
public static void main(String []args) {
Name name1 = new Name("n1","f1");
Name name2 = new Name("n2","f2");
Name name3 = new Name("n3","f3");

  Collection c = new HashSet();
  Iterator i = c.iterator();
  c.add(name1);
  c.add(name2);
  c.add(name3);

  while (i.hasNext()) {
     Name name = (Name)i.next();
     String firstName = name.firstName;
     System.out.println(firstName + "");
  }

}
}

class Name {
public String firstName;
public String lastName;

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

编译通过了,运行时没有任何的输出,我明明是用了System.out.println(),这是为什么

  • 写回答

6条回答 默认 最新

  • 迷茫猿小明 2017-03-23 02:19
    关注

    把 c.add()这3句放到,Iterator i = c.iterator()前面,就有输出了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?