Is there any way to make the following code work in Java?
public class RandomizedQueue<Item> implements Iterable<Item> {
private static final int ARRAYSIZE = 8; // default array size
private Node[] nodeArray; // contains pointers to the randomized nodes
private int size; // current size of queue
private class Node
{
private Item item;
private Node next;
private Node previos;
}
@SuppressWarnings("unchecked")
public RandomizedQueue() // construct an empty randomized queue
{
nodeArrays = (Node[]) new Object[ARRAYSIZE]; // fix this and everything works!
}
// unimportant randomized queue implementation details
// ...
}
It crashes on runtime at nodeArray initialization line with the following error:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LRandomizedQueue$Node;
at RandomizedQueue.<init>(RandomizedQueue.java:18)
at Subset.main(Subset.java:6)
.
The problem is that I cannot use library data structures such as ArrayList<>, because I got to have custom re-size rules. In addition, I cannot implement ArrayList analogue myself (no additional classes allowed in my task).
Code nodeArray = new Node[ARRAYSIZE];
does not compile in Eclipse with error Cannot create a generic array of RandomizedQueue<Item>.Node
.