For reference later,
the original code is
Code: Select all
import java.util.*;
class Person{
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() { return name; }
}
public class Helper{
public void helpPeople(Queue people, Queue<String> helped){
do{
Person p = (Person)people.poll();
System.out.println("Help : " + p + " ");
helped.offer(p.getName());
}while(!people.isEmpty());
}
public static void main(String[] args){
Queue<Person> q = new LinkedList<Person>();
q.offer(new Person("Pope"));
q.offer(new Person("John"));
Queue<Person> helpedQ = new LinkedList<Person>();
Helper h = new Helper();
h.helpPeople(q, helpedQ);
}
}
When you run from command line with -Xlint:unchecked, the warning message will be
Code: Select all
Helper.java:24: warning: [unchecked] unchecked call to offer(E) as a member of the raw type Queue
helped.offer(p.getName());
^
where E is a type-variable:
E extends Object declared in interface Queue
1 warning
One way to fix is , for helpedQ, change to the definition to
Code: Select all
Queue<String> helpedQ = new LinkedList<>();
(Or LinkedList<String>, doesn't matter).
Also change the type of parameters in helpPeople to
Code: Select all
public void helpPeople(Queue people, Queue<String> helped)
This way it works because original helpedQ, it suppose contains the Person object, but offer takes String as a parameter. Also change the type of parameter of helpPeople because you need to let the compiler know helped queue is actually contains the String object.
Correct me if I am wrong