0

When trying to implement Fibonacci Heap in Java I'm getting a generic array creation error even though I'm not using generics.

private void consolidate() {
    Node[] degrees = new Node[45];
}

The Node class is this:

private class Node {

    public E val;
    public Node left, right, child;
    public int degree;
    public boolean mark;

    public Node(E _val) {
        val = _val;
    }

    public void insert(Node newNode) {
        if(left == this) {
            left = newNode;
            right = newNode;
            left.right = this;
            left.left = this;
        } else {
            Node temp = left;
            left = newNode;
            left.right = this;
            left.left = temp;
            temp.right = left;
        }
    }

    public void unlink() {
        left.right = right;
        right.left = left;

        if(child != null) {
            Node dummy = child;

            do {
                left.insert(dummy);
                dummy = dummy.left;
            } while(dummy != child);
        }
    }

}

I thought that maybe this part was causing the error:

public E val;

and

    public Node(E _val) {
        val = _val;
    }

however I changed both of them to Object, and it still didn't work. If it helps, E extends Comparable<E>.

public class FibonacciHeap<E extends Comparable<E>>
3
  • I'm guessing Node is an inner class of FibonacciHeap. But an inner class of a generic is effectively a generic, because it's within the scope of the type parameter. You knew this, because you used E within the Node class. Commented May 5, 2020 at 3:39
  • How do you suggest I fix this? Commented May 5, 2020 at 4:08
  • I'd use an ArrayList instead of an array. Commented May 5, 2020 at 6:21

1 Answer 1

1

Your class FibonacciHeap is generic actually, so its inner private class Node is generic too as mentioned in the first comment.

If you think your Node class should not be generic, you can either make it inner static class using Object val:

public class FibonacciHeap<E extends Comparable<E>> {
    private static class Node {
        Object val;
        // ...
    }
}

or move its definition outside FibonacciHeap:

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

}

private class Node {
    Object val;
    // ...
}

There's also an option to remove generic from FibonacciHeap too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.