【问题标题】:Not able to iterate List using Iterator, but working fine when using for each无法使用迭代器迭代列表,但在为每个使用时工作正常
【发布时间】:2014-01-04 13:45:46
【问题描述】:

使用 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>

}

这是问题的 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());
            }*/
        }

    }**

}

【问题讨论】:

  • 为什么说有错误而不贴错误?
  • 你在循环中调用了next() 两次...
  • 是的,现在意识到了,谢谢你的评论。

标签: java spring collections iterator


【解决方案1】:

您对Iterator 的使用是错误的-next() 将迭代器移动到下一个位置,并且您从while 循环的主体中调用它两次,并且只检查@ 987654324@一次。

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

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

【讨论】:

    【解决方案2】:

    那个循环应该是这样的

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

    你在每个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
    
                }
    

    这显然会在打印时导致奇怪的结果。

    【讨论】:

    • 现在工作正常。谢谢。
    猜你喜欢
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2021-03-29
    • 2013-09-17
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多