无法使用Iterator迭代List,但在使用Iterator时工作正常

时间:2014-01-04 13:45:47

标签: java spring collections iterator

使用来自applicationContext.xml的Answer Bean填充问题Bean 使用Iterator迭代List of Answer时会引发错误,但是对于For Each循环正常工作。

 {        


<bean id="answer1" class="com.core.spring.Answer">  
<property name="id" value="1"></property>  
<property name="answerText" value="Java is a programming language"></property>  
<property name="by" value="Ravi Malik"></property>  
</bean>  
<bean id="answer2" class="com.core.spring.Answer">  
<property name="id" value="2"></property>  
<property name="answerText" value="Java is a platform"></property>  
<property name="by" value="Sachin"></property>  
</bean>  

<bean id="answer3" class="com.core.spring.Answer">  
<property name="id" value="3"></property>  
<property name="answerText" value="Java is a platform"></property>  
<property name="by" value="Sachin"></property>  
</bean>  

<bean id="question" class="com.core.spring.Question">
<property name="id" value="1"></property>
<property name="questionText" value="What is Java?"></property>
<property name="answers">
<list>
<ref bean="answer1"/>
<ref bean="answer2"/>
<ref bean="answer3"/>

</list>
</property>
</bean>

}

这是Bean类问题。

{
  package com.core.spring;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    public class Question {

        private int id;
        String questionText;
        List<Answer> answers=new ArrayList<Answer>();


        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getQuestionText() {
            return questionText;
        }
        public void setQuestionText(String questionText) {
            this.questionText = questionText;
        }
        public List<Answer> getAnswers() {
            return answers;
        }
        public void setAnswers(List<Answer> answers) {
            this.answers = answers;
        }

        public void displayInfo(){

            System.out.println("Question: "+id);
            System.out.println(questionText);
            System.out.println("Answers");

            for(Answer ans:answers){
                System.out.print(ans.getId());
                System.out.println(": "+ans.getAnswerText());
            }

            for (Iterator<Answer> ans = answers.iterator(); ans.hasNext();){
                System.out.print(ans.next().getId());
                System.out.println(": "+ans.next().getAnswerText());

            }
            /*Iterator<Answer> ans=answers.iterator();
            while(ans.hasNext()){
                System.out.print(ans.next().getId());
                System.out.println(": "+ans.next().getAnswerText());
            }*/
        }

    }**

}

2 个答案:

答案 0 :(得分:2)

您使用Iterator是错误的 - next()将迭代器移动到下一个位置,并且您从while循环中将其称为两次身体,只有一次咀嚼hasNext()

您应该只调用next()一次,将其值提取到局部变量并使用它需要多少次:

Iterator<Answer> ans = answers.iterator();
while(ans.hasNext()) {
    Answer answer = ans.next();
    System.out.print(answer.getId());
    System.out.println(": " + answer.getAnswerText());
}

答案 1 :(得分:1)

那个循环应该是这样的

 for (Iterator<Answer> ans = answers.iterator(); ans.hasNext();){
                Answer ans = ans.next();
                System.out.print(ans.getId());
                System.out.println(": "+ans.getAnswerText());

            }

您在每个next()

中调用System.out.println()方法
 for (Iterator<Answer> ans = answers.iterator(); ans.hasNext();){
                System.out.print(ans.next().getId());  //moving one element
                System.out.println(": "+ans.next().getAnswerText());  //moving one element

            }

这显然会在打印时产生奇怪的结果。