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;
    }
Community
  • 1
  • 1
  • Check this question: http://stackoverflow.com/questions/509076/how-do-i-address-unchecked-cast-warnings – Petr Abdulin Oct 10 '12 at 04: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` interface when calling the `contains` method. – Luiggi Mendoza Oct 10 '12 at 04: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. – Ian Roberts Oct 10 '12 at 23:17

3 Answers3

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...
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
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.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
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;
}
Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • This isn't a good solution, it forces to the `` class to implement the `public boolean equals(Object o)` method, maybe OP doesn't need this at all. – Luiggi Mendoza Oct 10 '12 at 04:59
  • @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. – Alexis King Oct 10 '12 at 05:01
  • Maybe OP classes don't implement the `equals` method, instead by looking his/her code the `E` class should implement the `Comparable`. We should guide OP to the better solution, not to add restrictions like this to the actual code. – Luiggi Mendoza Oct 10 '12 at 05:03
  • @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. – Alexis King Oct 10 '12 at 05:07
  • *It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))*, from [`Comparable`](http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html). It could be a flaw, or maybe not, but that's out of the scope of this question. – Luiggi Mendoza Oct 10 '12 at 05:10