I wanted to create an Iterator for a generic class which worked fine. I thought the iterator would try to iterate using the TypeParameter of the generic class, but apparently that's not the case because Eclipse tells me that an Object is expected.
If someone knows what I've done wrong, I would be very happy.
public class GenericClass<T extends OtherClass> implements Comparable, Iterable
{
private ArrayList<T> list = new ArrayList<T>();
[...]
@Override
public Iterator<T> iterator()
{
Iterator<T> iter = list .iterator();
return iter;
}
[...]
}
public class Main
{
public static void main(String[] args)
{
GenericClass<InstanceOfOtherClass> gen = new GenericClass<InstanceOfOtherClass>("Aius");
for(InstanceOfOtherClass listElement : gen) // This is the problem line; gen is underlined and listElement is expected to be an Object
{
System.out.println(listElement.getName());
}
}
}