search

Home  >  Q&A  >  body text

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对象啊?
大家讲道理大家讲道理2802 days ago1047

reply all(4)I'll reply

  • 阿神

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

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

    Next is executed twice once in the while loop

    reply
    0
  • PHPz

    PHPz2017-04-18 10:50:25

    A class that does not define the str method cannot println what you want.

    reply
    0
  • 高洛峰

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

    it.next() loops twice

    reply
    0
  • PHP中文网

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

    it.next() loops twice!

    reply
    0
  • Cancelreply