简体   繁体   English

无法使用Iterator迭代List,但在每个迭代器上使用都可以正常工作

[英]Not able to iterate List using Iterator, but working fine when using for each

Populating the Question Bean with Answer Bean from applicationContext.xml and error is thrown when am iterating List of Answer using Iterator, but working fine with For Each loop. 使用Iterator迭代Answer List时,使用applicationContext.xml中的Answer Bean填充Question Bean并引发错误,但对于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>

}

This is the Bean Class of Question. 这是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());
            }*/
        }

    }**

}

Your use of the Iterator is wrong - next() moves the iterator to the next position, and you're calling it twice from the while loop's body, and only cheking hasNext() once. 您对Iterator使用是错误的Iterator next()将迭代器移至下一个位置,并且您从while循环的正文中对其调用了两次 ,并且仅使一次hasNext()一次。

You should only call next() once, extract its value to a local variable and use it how many times you need: 您只应调用next() ,将其值提取到局部变量中,并使用几次即可:

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

That loop should be like this 该循环应该是这样的

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

            }

You are calling next() method in each System.out.println() 您正在每个System.out.println()中调用next()方法

 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

            }

Which obviously leads to odd results while printing them. 在打印它们时显然会导致奇怪的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM