Domanda

I implemented a java class but come up with two errors which I can't fix. Error1:

incompatible types
Required: RandomizedQueue<Item>
Found:    Item

Error2:

can not find symbol
Symbol: valuable last
location:valuable next of type Item
public class RandomizedQueue<Item> implements Iterable<Item> {
    private int number=0;
    private Node First=null;
    private class Node {
        Item item;
        Item next=null;
        Item last=null;
    }
    private Node Random() {
        double r = Math.random();
        int n = (int) (r*number);
        if(n==0) n=1;
        Node ob=First;
        for(int i=0;i<(n-1);i++) {
            ob = ob.next;   //Error1
        }
        return ob;
    }

    public RandomizedQueue() {
        Node empty=new Node();
    }
    public boolean isEmpty() {
        return number==0;
    }
    public int size() {
        return number;
    }
    public void enqueue(Item item) {
        if(item==null) throw new NullPointerException();
        Node oldfirst = First;
        First = new Node();
        First.item = item;
        First.next = oldfirst; //Error1
        oldfirst.last = First;
        number++;
    }
    public Item dequeue() {
        Node ob=Random();
        Item back = ob.item;
        Node temp = ob.last;
        temp.next = ob.next;
        ob.next.last = temp;   //Error2
        return back;
        }
    public Item sample() {
        return Random();
    }
    public Iterator<Item> iterator() {       //Error:cannot find symbol symbol:class Itertor
        return IndepentRandomIterator();
    }
    private class IndepentRandomIterator implements Iterator<Item> {
        private RandomizedQueue<Item> iq = new RandomizedQueue<Item>();
        Node scan = First;
        public IndepentRandomIterator() {
            while(scan != null) {
                iq.enqueue(scan.item);
                scan=scan.next;
        }
        }
        public boolean hasNext() {
            return iq.number >0;
        }
        public void remove() {
            throw new UnsupportedOperationException();
        }
        public Item next() {
            if(iq.number==0) throw new java.util.NoSuchElementException();
            return iq.dequeue();
        }
    }
    public static void main(String[] args) {
        RandomizedQueue<String> test = new RandomizedQueue<String>();
        test.enqueue("Luo");
        test.enqueue("Jiebin");
        test.enqueue("is");
        test.enqueue("genious");
        test.dequeue();
        test.dequeue();
        StdOut.println("Is it empty?"+test.isEmpty());
        StdOut.println("Size:       "+test.size());
        StdOut.println("A sample:   "+test.siample());
        Iterator<String> it = test.iterator();
        while(test.hasNext()) {
            String s = it.next();
            StdOut.println(s);
        }
    }
}
È stato utile?

Soluzione 2

For the first error, you are trying to assign ob a value of ob.next. ob is a Node, but ob.next is an Item. Obviously, an Item is not a Node, so you can't assign an Item to a Node.

The same thing applies on the line:

first.next = oldfirst;

first.next is an Item, but oldFirst is a Node. You can't assign a Node to a variable of type Item.


For the error cannot find symbol symbol:class Itertor. That is because you need to put this at the very top of your code:

import java.util.Iterator;

On the line:

return IndepentRandomIterator();

The code attempts to call the method IndepentRandomIterator(). To make it call the constructor for the IndepentRandomIterator class, you need to add the keyword new right before IndepentRandomIterator() like this:

return new IndepentRandomIterator();

For your error on this line:

ob.next.last = temp;

The code attempts to access the field last of the object ob.next. I think you want to access the object ob.last. That will still be a problem, though, because then you will get the first error. Both ob.next and ob.last are of the type Item, but temp is a Node.


Near the end of your code, you try to use StdOut.println(). StdOut does not exist; you should use System.out.println().

Altri suggerimenti

The first error can not be diagnosed as you did not provide the implementation of Node class

The second error:

You do not craate a instance of your class.

 public Iterator<Item> iterator() {       //Error:cannot find symbol symbol:class Itertor
        return IndepentRandomIterator();
    }

You must provide an intances to be able to return.

 public Iterator<Item> iterator() {       //Error:cannot find symbol symbol:class Itertor
        return new IndepentRandomIterator();
    }

I get more than 2 compilation errors.

Firstly, in the first line of the class:

public class RandomizedQueue<Item> implements Iterable<Item> {

is Item the name of an actual class, or is it a placeholder for a class?

If it is a class, then your first error is complaining that it cannot find the field 'next' on this class - maybe it's not defined or not visible.

If Item is a generic placeholder then you need to define RandomizedQueue like this:

public class RandomizedQueue<Item extends MyClass> implements Iterable<Item> {

Where MyClass does have a visible field called next

Secondly, the code:

    Node oldfirst = First;
    First = new Node();
    First.item = item;
    First.next = oldfirst; //Error1

It seems to my that you are trying to cast a Node into an Item. But exactly what is an Item, and has it got anything to do with a Node?

I can't see what you are trying to do here. It looks like you don't really understand inheritance, and you really do need to understand it before you can start using generics.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top