-1

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;
        }
Ilan Tal
  • 457
  • 2
  • 8
  • 21
  • Maybe [this answer](https://stackoverflow.com/questions/9542076/array-of-parameterized-types) will clear things up for you. You can use a parametrized List rather than an array. – pafau k. May 21 '20 at 15:36
  • One problem is that I'm not allowed to include any libraries, anything outside java.lang. I am allowed to use things inside algs4.jar, but I failed to find anything which might solve the problem. What I'm basically trying to do is make an array of objects. – Ilan Tal May 21 '20 at 15:53
  • And the provided link explains why you cannot do that without warnings in Java. Also, if what you wrote is true, you also can't use the Iterator, since it's (along with ArrayList) in the java.util package. – pafau k. May 21 '20 at 15:58
  • As a special case for this problem java.util.Iterator is allowed. ArrayList and LinkedList are specifically forbidden, but in general things in java.util and most everything else is out. – Ilan Tal May 21 '20 at 16:03
  • I have a crreping feeling that your Node class is supposed to be a node of a linked list. In this case you should be able to implement the Iterator just by walking the list, without resorting to array creation. Maybe you could edit the post and clarify what exactly you're trying to accompilsh here. And also create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – pafau k. May 21 '20 at 16:11
  • You are correct that this is my version of a singly linked list. It has to get to next in constant time, not list length time. I just thought of creating a second randomly linked list, but that will probably violate the memory requirements. I'll continue to think about it and maybe I will come up with something. – Ilan Tal May 21 '20 at 16:17
  • the link to the problem is https://coursera.cs.princeton.edu/algs4/assignments/queues/specification.php – Ilan Tal May 21 '20 at 16:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214355/discussion-between-pafau-k-and-ilan-tal). – pafau k. May 21 '20 at 16:31

1 Answers1

0

https://www.javarticles.com/2014/12/arrays-of-parameterized-type.html

Basically you can't have typed arrays, you'll have to use an ArrayList or something else.

That being said, it seems to me like you have some confusion about the purpose of linked lists

So in java Arrays are fixed in size but allow you to instantly look up a value given it's position in the array. This is great if you don't expect the array to change a lot, but if you had an array of 5 characters a,b,c,d,e and wanted to add g in the middle, what you'd have to do is create a new array of size 6, and then copy over all the values and insert the one you wish wherever is needed. Not too bad, but imagine the array had 100,000 characters instead of 5, copying all that can cause performance problems, especially if it happens often

To solve this problem there are linked lists, since values are linked together it's simple to create a new Node for g, point c.next to g, then g.next to d, you don't have to copy a crazy sized array, all is good. Unfortunately you don't have the ability to instantly look up the x'th item, you'd have to get the first item and then call node.next() x times, but in a lot of instances looking up a values based on it's position isn't really needed.

You're storing all the nodes of a linked list in an array, which is redundant and makes the linked list pointless because you might as well just have an array of Items and get the next item by simply incrementing whatever index variable you're using to access the array.

Think about what an Iterator needs to do, it needs to start at the beginning of a list and then iterate through all the values in the list. This is pretty much exactly what a linked list is for, but an array can also do the job. If you wanted to go with linked list, instead of storing an array of Nodes private final Node<Item>[] elements = init1(); all you need to store is the current node private Node currentNode = init1(); -- init1 needs to be changed to return the FIRST node. Then in the iterators next() function you simply do something like

Node<Item> toReturn = currentNode;
currentNode = currentNode.next();
return toReturn;

You could achieve the same thing by abandoning the linked list all together and just using a regular Item[] array private final Item[] elements = init1(); and then using an incrementing index to go through the values, either way the end result is the same.

Nerdsie
  • 128
  • 1
  • 3
  • 11
  • Mostly I would like to thank you for your help. It turns out that the warning did NOT kill the compiler - it was only a warning. On the other side, I thought that all routines in algs4.jar could be used and this too is NOT true. I chose to use readLine from inside their jar, and readLine was forbidden, and I needed to use readString. Never mind, the purpose of these theoretical programming problems is to learn, and you taught me why a simple array couldn't handle typed input - so it was worth the effort – Ilan Tal May 22 '20 at 10:13
  • Last night while sleeping I too thought of the idea to use Item[] elements, instead of Node< Item>[] elements. This morning I tried Item[] elements = new Item[N] which failed. After going through the more complicated case, it was obvious to me that "new Item[N]" would fail and I needed to cast from new Object[N]. The cast would give the same warning - but it is a warning and not an error. In short, thanks again for your help to show exactly where the problem was. I accepted your solution as another way to solve the problem. – Ilan Tal May 22 '20 at 10:21
  • Hm, do you have any idea why Item[] elements = new Item[N] failed? As far as I can tell that should work, and typically you should prefer doing something like that over casting an Object array – Nerdsie May 22 '20 at 14:16