0

Edit: Noticed the post was flagged as duplicate; I read through the other, similar, question but it really didn't treat the same problem as mine. I'm working with a class that is guaranteed to be of the type Comparable, yet I can't cast it.

I have the following class, which is an implementation of an interface Queue. I make sure that the type which HeapPriorityQueue object works with implements Comparable, so that the method E.compareTo(E other) is available.

I create an instance of HeapPriorityQueue in my main method. Where Patient is a class that implements Comparable

HeapPriorityQueue<Patient> queue = new HeapPriorityQueue<>();

At compilation however, I get an exception ClassCastException which points towards the last line in the code below

public class HeapPriorityQueue<E extends Comparable<E>> implements Queue<E>
{

    private final int DEFAULT_CAPACITY = 10;

    private int size;
    private E[] array;

    public HeapPriorityQueue()
    {
        size = 0;
        array = (E[]) new Object[DEFAULT_CAPACITY + 1];
    }
}

The exception:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;

5
  • Is there some reason (e.g. teacher said so) that it needs to be an array specifically? There are other data structures (e.g. List<E>) that might better suit you... Commented May 14, 2015 at 17:33
  • @dcsohl I am limited in my choices. It's a course about data structures, so I imagine my choices are either singly linked lists or arrays for the heap structure. Commented May 14, 2015 at 17:38
  • I would definitely go for some sort of List here, even if you have to write it yourself. As you have found, you cannot do new E[size]; and you cannot cast from an Object[] to an E[] (which you would not want to do, even if you could, since your new Object[] is full of Objects and not Es!). Your best bet is probably ArrayList. Whether or not that's allowed is a different question. Although I do have to ask... if it's a HeapPriorityQueue, shouldn't you be setting up a Heap and not an Array/List? Commented May 14, 2015 at 17:48
  • @dcsohl I'm under the impression that a heap can be built up from an array. That you could place a bunch of elements in an array and say that k:th's left child node is 2*k, and k:th's right child node is 2*k + 1. Is this not how it works? Commented May 14, 2015 at 18:00
  • 1
    Huh. Ok, yeah, sure, I suppose you could do that, if you felt like being masochistic (or had a sadistic teacher making you do that). I'd personally create a quick little Node<E> class that had a value and right and left children. I think the data structure manipulations would be easier that way. But this is veering off-topic for this question... Commented May 14, 2015 at 18:07

1 Answer 1

1

I'm working with a class that is guaranteed to be of the type Comparable, yet I can't cast it.

new Object[DEFAULT_CAPACITY + 1]; doesn't guarantee anything instead of the fact that it's an object array. An object array can't be safely cast to a Comparable array.

You'd have to use something like:

Class<E> clazz;
array = (E[]) Array.newInstance(clazz, size);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.