2

所以我正在尝试制作一个基于数组的通用堆,我可以在我的测试器类中使用它。我所拥有的大部分内容是基于我对树木的理解和一些在线研究以及我的教科书;两者都对我正在寻找的信息非常有限。但是,我确实设法获得了所有需要的方法,当我运行它时,我收到了这个错误:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    at q1.Heaps.<init>(Heaps.java:23)
    at q1.createGui.<init>(Gui.java:46)
    at q1.Gui.main(Gui.java:18)  

我猜这与我如何声明和初始化我的 Comparable 数组有关,我无法弄清楚如何去做。

package q1;

import java.util.Arrays;


public class Heaps<E extends Comparable<E>> {

    Comparable[] data;
    int size;

    /**
     * Constructor with s as size
     */
    @SuppressWarnings("unchecked")
    public Heaps(int s) {
        size = 0;
        data = (E[]) new Object[s];
    }

    /**
     * Adds a value to the heap
     */
    public void add(E value) {
        if (full()) // expand array
            ensureCapacity(2*size);
        size++;
        data[size] = value;
        if (size > 1)
            heapifyUp();
    }

    /**
     * Checks if the array is full
     */
    private boolean full()
    { 
        return (size == data.length-1);
    }

    private void heapifyUp()
    {
        Comparable<E> temp;
        int next = size;
        while (next != 1 && data[next].compareTo(data[next/2]) > 0)
        {
            temp = data[next];
            data[next] = data[next/2];
            data[next/2] = temp;
            next = next/2;
        }
    }

    private void heapifyDown()
    {
        Comparable<E> temp;
        int next = 0;
        while (next*2 <= size) // node has a child
        {
            int child = 2*next; // left child
            if (child < size &&
                    data[child].compareTo(data[child+1]) > 0)//left smaller than right
                child++; // right child instead
            if (data[next].compareTo(data[child]) > 0)
            {
                temp = data[next];
                data[next] = data[child];
                data[child] = temp;
                next = child;
            }
            else;
            next = size; // stop loop
        }//end while
    }

    /**
     * Removes all occurrence of element
     */
    public boolean removeAll(E element) {
        if (contains(element) && !(isEmpty())){
            for (int i = 0; i < size; i++){
                if(element.equals(data[i])){
                    data[i] = data[size-1];
                }
                heapifyDown();
            }
            return true;
        }
        return false;
    }

    /**
     * Removes 1st occurrence of element
     */
    public boolean remove(E element) {
        if (contains(element) && !(isEmpty())){
            for (int i = 0; i < size; i++){
                if(element.equals(data[i])){
                    data[i] = data[size-1];
                    heapifyDown();
                    return true;
                }   
            }
        }
        return false;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public Comparable<E>[] ensureCapacity(int s) {
        return Arrays.copyOf(data, 2*s);
    }

    /**
     * Converts the heap into its String representation.
     *   @return the String representation
     */

    public Comparable<E>[] iteratorPreOrder() 
    {
        Comparable<E>[] temp = (E[]) new Object[size];
        temp[0] = data[0];
        int i = 1;
        int count = 1;
        while(data[2*i] != null){
            temp[count] = data[2*i];
            ++i;
            ++count;
        }
        i = 1;
        while(data[(2*i) +1] != null){
            temp[count] = data[(2*i) +1];
            ++i;
            ++count;
        }
        return temp;
    }


    public int countOccurance(E element){
        int count = 0;
        for (int i =0; i < size; i++){
            if(element.equals(data[i])){
                count++;
            }
        }
        return count;
    }

    public boolean contains (E element) 
    {
        for (int i=0; i<size; i++){
            if (element.equals(data[i])){
                return true;
            }
        }
        return false;
    }
}

如果你能告诉我如何解决这个问题,我将不胜感激。谢谢

编辑:所以我编辑了我的课程,现在我这样做了data = (E[]) new Comparable[s]。那么为什么 java 不允许泛型 Array 类型,是什么使它与可以泛型的 Arraylist、Stacks、Queues 和/或 LinkedList 不同呢?

4

1 回答 1

1

您正在创建一个 Object[],然后尝试将其转换为 Comprable[]。编译器告诉你你在 unchecked cast 错误上做错了什么。

您希望数据E[] data和行是:

data = new E[s];

注意:这可能会遇到 Java 如何处理泛型的问题。

于 2014-11-24T21:36:35.753 回答