2

Possible Duplicate:
How do I address unchecked cast warnings?

My program compiles and works correctly when I compile with -Xlint:unchecked, but I am looking for assistance to remove this warning. Any help is appreciated! Thank you

This is the warning:

java: warning: [unchecked] unchecked cast found : E required: java.lang.Comparable<E> ^^^^ 

The code the gives the warning is:

public boolean contains(E obj) { Node<E> curr = head; while (curr != null) { return true; } curr = curr.next; } return false; } 
3
  • Check this question: stackoverflow.com/questions/509076/… Commented Oct 10, 2012 at 4:50
  • @PetrAbdulin in this case, it's not the same. This could end in a ClassCastException if the E class doesn't implement the Comparable<E> interface when calling the contains method. Commented Oct 10, 2012 at 4:56
  • 1
    Please don't edit your question to delete all its content - even if it's been closed as a duplicate (which I don't agree with in this case, I think @LuiggiMendoza's answer is most likely the appropriate one) it should be left in situ so the answers that were written before the close still make sense. Commented Oct 10, 2012 at 23:17

3 Answers 3

1

If you're assuming all your data in your class must implement the Comparable interface, then you should add it in the class declaration, or else you could get a ClassCastException when using this method in a class that doesn't implement this interface.

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... } 
Sign up to request clarification or add additional context in comments.

Comments

0

You should change the signature of contains method as follows to avoid any type-casting while invoking compareTo method on obj:

public <E extends Comparable<E>> boolean contains(E obj) { return true; } 

This will also mandate any E passed to contains method to already implement Comparable interface.

8 Comments

I don't think this will work, since he appears to be implementing the List<E> interface, so he can't change the method signature.
I couldn't figure out from the question that he is implementing List<E>?
@JakeKing OP doesn't says that his class is implementing List interface.
@Vikdor based in OP's actual code, the <E extends Comparable<E>> is clumsy, because the class has the <E> template.
Eh, I suppose not. Fair enough.
|
0

It would seem that this is a flawed implementation of a contains(...) method. Objects should not have to be comparable, they must only be equatable. Simply use equals(...) instead:

public boolean contains(E obj) { Node<E> curr = head; while (curr != null) { if (curr.data.equals(obj)) { return true; } curr = curr.next(); } return false; } 

5 Comments

This isn't a good solution, it forces to the <E> class to implement the public boolean equals(Object o) method, maybe OP doesn't need this at all.
@LuiggiMendoza Object contains a default implementation for equals(Object), which compares by reference (aka ==) by default. This will use that implementation unless a subclass overrides it. This is a better solution since it doesn't require unnecessary use of the Comparable interface and it uses a built-in method for all objects.
Maybe OP classes don't implement the equals method, instead by looking his/her code the E class should implement the Comparable<E>. We should guide OP to the better solution, not to add restrictions like this to the actual code.
@LuiggiMendoza A class that implements Comparable should implement equals(...), though not perhaps the other way around. If a comparable object doesn't implement equals I consider it a design flaw and illogical.
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)), from Comparable<T>. It could be a flaw, or maybe not, but that's out of the scope of this question.