Reputation: 35
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.
{
<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.
{
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());
}*/
}
}**
}
Upvotes: 2
Views: 85
Reputation: 311853
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.
You should only call next()
once, extract its value to a local variable and use it how many times you need:
Iterator<Answer> ans = answers.iterator();
while(ans.hasNext()) {
Answer answer = ans.next();
System.out.print(answer.getId());
System.out.println(": " + answer.getAnswerText());
}
Upvotes: 2
Reputation: 122008
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()
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.
Upvotes: 1