Reputation: 81
I'm trying to code up SkipLists as I just learned about the data structure.
Main Question: How do I properly setup a Generic ArrayList for SkipLists?
So far, I've learned java does not like generic arrays for example:
SkippyList.java:138: error: generic array creation
SkipNode<T>[] criticalPoints = (SkipNode<T>[]) new SkipNode<T>[height];
So now I'm trying to use ArrayLists to hold these generic types. I've been scouring the internet and haven't been able to find a solution that works.
I've tried setting up the ArrayList (as a global variable) like ArrayList<SkipNode<AnyType>>[] next;
Then in a function such as this one declare it like such:
// Constructor for a node with data, but not wired in.
public SkipNode(AnyType data, int height)
{
this.data = data;
this.height = height;
this.next = new ArrayList<SkipNode<AnyType>>[height];
for (int i = 0; i <= height; i++)
this.next[i] = null;
}
But then I end up with another error such as this one:
SkippyList.java:14: error: generic array creation
this.next = new ArrayList<SkipNode<AnyType>>[height];
For reference the (not updated) constructor for (some) of the nodes:
// Constructor for the head node
public SkipNode(int height)
{
this.data = null;
this.next = (SkipNode<AnyType>[]) new SkipNode[height];
for (int i = 0; i <= height; i++)
this.next[i] = null;
}
I know it is possible, do I need to include the <AnyType>
(because I've tried to leave it off and still recieve the same result) or am I just improperly creating a Generic ArrayList?
Thank you for any insight!
Upvotes: 0
Views: 99
Reputation: 1151
Wrong brackets :-)
You are trying to create an array "ArrayList[arraySize]" instead of an instance of ArrayList with an initial size
new ArrayList<SkipNode<AnyType>>[arraySize];
I think what you are trying to do is
new ArrayList<SkipNode<AnyType>>(someInitialListSize);
But without knowing exactly what type "this.next" is (maybe it really is SkipNode[]?), it is hard to be sure.
Upvotes: 1