首頁  >  問答  >  主體

java代码如下,输出结果中为什么s对象?

Collection    c2 =new ArrayList();
        Student s= new Student();
        c2.add(s);
        c2.add("我");
        Iterator it = c2.iterator();
        while (it.hasNext()) {
            it.next();
            Object object = (Object) it.next();
            System.out.println(object);
        }
输出结果为“我”,并没有s对象啊?
大家讲道理大家讲道理2744 天前1006

全部回覆(4)我來回復

  • 阿神

    阿神2017-04-18 10:50:25

    while (it.hasNext()) {
                it.next(); //第一个对象 Student 
                Object object = (Object) it.next(); //第二个对象 String 
                System.out.println(object);
    }

    while循環裡一次next了兩次

    回覆
    0
  • PHPz

    PHPz2017-04-18 10:50:25

    沒有定義str方法的類別不能println出想要的東西來。

    回覆
    0
  • 高洛峰

    高洛峰2017-04-18 10:50:25

    it.next()循環了兩次

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-18 10:50:25

    it.next()循環了倆次哦!

    回覆
    0
  • 取消回覆