I am having format problems which are causing a reject of Java compilation.
This code works in Netbeans, but the Web compile checker refuses to accept it.
I am making an iterator where I want an array of randomly sorted nodes in a linked list.
This iterator is type checked using < Item>. To check it I'm using a < String > for < Item>
Inside the code, for example there is "int[] vals = new int[numEntries]" and that works without complaints. This is a list of intergers. I want a list of Node< Item> objects. You can see that the complaint is it is looking for Node< Item> and finding Node.
[unchecked] unchecked conversion
required: RandomizedQueue<Item>.Node<Item>[]
found: RandomizedQueue.Node[]
where Item is a type-variable:
Item extends Object declared in class RandomizedQueue
The line in question is Node< Item>[] ret1 = new Node[numEntries]. Again, the code runs fine in Netbeans and gives correct results. It is just this warning is killing the Web compiler, and I can't figure out how to get around it.
I tried Node< Item>[] = new Node< Item>[numEntries], but that gave an error instead of a warning. I also tried removing the < Item> tags within the subroutine, but that gives me an unchecked conversion at a different point. What is the correct format which will produce no warnings??
public Iterator<Item> iterator() {
Iterator<Item> it = new Iterator<Item>() {
private final Node<Item>[] elements = init1();
private int indx = 0;
private Node<Item>[] init1() {
Node<Item>[] ret1 = new Node[numEntries];
Node<Item> curr;
int[] vals = new int[numEntries];
int i, j, val;
int[] free = new int[numEntries];
for( i=0; i<numEntries; i++) {
free[i] = i;
}
for( i=0; i<numEntries; i++) {
j = numEntries-i;
val = StdRandom.uniform(j);
vals[i] = free[val];
free[val] = free[j-1];
}
for( i=0; i<numEntries; i++) {
curr = head;
j = vals[i];
while( j-- > 0) curr = curr.next;
ret1[i] = curr;
}
return ret1;
}