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>>
Nodeis an inner class ofFibonacciHeap. 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 usedEwithin theNodeclass.ArrayListinstead of an array.