0

I'm trying to learn constructor injection with collections. My Question class as below.

public class Question {  
private int id;  
private String name;  
private List<String> answers;  

public Question() {}  
public Question(int id, String name, List<String> answers) {  
    super();  
    this.id = id;  
    this.name = name;  
    this.answers = answers;  
}  

public void displayInfo(){  
    System.out.println(id+" "+name);  
    System.out.println("answers are:");  
    Iterator<String> itr=answers.iterator();  
    while(itr.hasNext()){  
        System.out.println(itr);  
    }  
}  

}  

My Test class as below.

public class Test {  
public static void main(String[] args) {  
    Resource r=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(r);  

    Question q=(Question)factory.getBean("q");  
    q.displayInfo();  

}  
} 

This is my applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="q" class="DependencyInjection.Question">  
<constructor-arg value="111"></constructor-arg>  
<constructor-arg value="What is java?"></constructor-arg>  
<constructor-arg>  
<list>  
<value>Java is a programming language</value>  
<value>Java is a Platform</value>  
<value>Java is an Island of Indonasia</value>  
</list>  
</constructor-arg>  
</bean>  

</beans> 

Out put as below.

java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3 java.util.AbstractList$Itr@15aaf0b3

This is infinite loop. How can I solve this issue?

6
  • what is the contents of answers? I think you mean to print something else than itr Commented Feb 25, 2019 at 9:47
  • by the way : you arent using constructor injection at all in your example. Commented Feb 25, 2019 at 9:57
  • @specializt I edited my question. Commented Feb 25, 2019 at 10:04
  • you still are'nt using constructor injection Commented Feb 25, 2019 at 14:38
  • @specializt can you explain why? because I'm new for spring Commented Feb 26, 2019 at 8:50

1 Answer 1

4

You are not advancing the Iterator, so your loop never ends.

Change:

while(itr.hasNext()){  
    System.out.println(itr);  
}

to:

while(itr.hasNext()){  
    System.out.println(itr.next());  
}
Sign up to request clarification or add additional context in comments.

1 Comment

And also @Lasa you can use System.out.println(String.join(", ", list)); instead of iterating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.