How to fix code to remove -Xlint:unchecked warning while compiling
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How do I address unchecked cast warnings?
当我使用-xlint:unchecked编译时,我的程序编译并正常工作,但我正在寻求帮助以删除此警告。感谢您的帮助!谢谢你
这是警告:
1 2 3 4 5 | java: warning: [unchecked] unchecked cast found : E required: java.lang.Comparable<E> ^^^^ |
给出警告的代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public boolean contains(E obj) { Node<E> curr = head; while (curr != null) { return true; } curr = curr.next; } return false; } |
如果假设类中的所有数据都必须实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class SomeClass<E extends Comparable<E>> { public boolean contains(E obj) { Node<E> curr = head; while (curr != null) { if (obj.compareTo(curr.data) == 0) { return true; } curr = curr.next; } return false; } //the rest of your implementation... } |
这似乎是
1 2 3 4 5 6 7 8 9 10 11 12 | public boolean contains(E obj) { Node<E> curr = head; while (curr != null) { if (curr.data.equals(obj)) { return true; } curr = curr.next(); } return false; } |
在obj上调用
1 2 3 4 | public <E extends Comparable<E>> boolean contains(E obj) { return true; } |
这还将要求传递给contains方法的任何e都已经实现可比较的接口。