Reputation:
My code is below. I just have a warning in Eclipse that says "Iterator is a raw type. References to generic type Iterator should be parameterized". What does this mean, how do I fix this properly?
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class EmployeeList
{
Queue<Employee> empList = new LinkedList<Employee>();
Employee find (String nm)
{
Iterator it = empList.iterator(); //Iterator is a raw type. References to generic type Iterator<E> should be parameterized
while(it.hasNext())
{
Employee em = (Employee)it.next();
if(em.name.equals(nm))
{
return em;
}
}
return null;
}
Upvotes: 1
Views: 2221
Reputation: 66677
Add to Iterator will clear the warning and when you add generic type to Iterator you don't need to explicitly cast on next() call because it is guaranteed that now Iterator points to type of Employee..
Iterator<Employee> it = empList.iterator();
while(it.hasNext())
{
Employee em = it.next();
if(em.name.equals(nm))
{
return em;
}
}
Upvotes: 5