Reputation: 6826
In a java method, I must return the following:
Iterator<List<Person>>
I wrote the method that creates a new list, adds the Persons to it:
LinkedList<Person> list = new LinkedList<Person>();
...
list.add(new Person(...))
...
But when I need to return the iterator as I was obligated in method signature, I'm confused.
returning:
return list.iterator();
causes compilation error:
Type mismatch: cannot convert from Iterator<Person> to Iterator<List<Person>>
What should I change in my method in order to follow the method signature?
Thanks
Upvotes: 1
Views: 620
Reputation: 44
This method can not help it but do not think so.
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Exam2 {
public static Iterator<List<Person>> getIterator(){
LinkedList<Person> list = new LinkedList<Person>();
list.add(new Person());
LinkedList<List<Person>> Parentlist = new LinkedList<List<Person>>();
Parentlist.add(list);
return Parentlist.iterator();
}
public static void main(String[] args) {
System.out.println( getIterator() );
}
}
class Person{
}
Upvotes: 1
Reputation: 86774
The type parameter for an Iterator
is the type it will return, not the type of the collection over which the iterator will be used.
In your case, return Iterator<Person>
Upvotes: 0