Search code examples
javaobjectsymbolsdeque

when I compile, it said cannot find symbol in deque java and I also dont know why dose pushleft contain this line left = (left+1) % queue.length;


    public class DequeCyclic implements Deque{
        private int left, right, size, capacity;
        private Object[] queue;
        //Constructing an empty Queue
        //@param s is the final size of the array
 
    public DequeCyclic(final int s) {
        left= right = size = 0;
        queue = new Object[s];
        capacity = s;
        
    }

    public boolean isEmpty() {
        return (size == 0);
    }
    public void pushLeft(Object c) throws Overflow{
        if (isFull()) {
            throw new Overflow ("the queue is full");
        }
        else if (left == 0 && right == 0 && size ==0) {
            queue[0] = c;
        }
        
        else {
            left = (left+1) % queue.length;
            queue[left] = c;
        }
        size++;
        }
    public static void main(String[] args){
        DequeCyclic[]  ab = new DequeCyclic[10];
        ab.isEmpty();//this is the problem
        }
    }

When I run this, I do not why it gave me that DequeCyclic.java:115: error: cannot find symbol ab.isEmpty(); ^ symbol: method isEmpty() location: variable ab of type DequeCyclic[] 1 error And I also wanna know why does the pushLeft has this line left = (left+1) % queue.length;


Solution

  • Your problem is here:

    DequeCyclic[] ab = new DequeCyclic[10];

    What does this line do? It initializes an array (size=10) of DequeCyclics with the name ab.

    What you probably want is:

    DequeCyclic ab = new DequeCyclic(10);

    You call the constructor of DequeCyclic with the parameter 10, assigning it to the variable ab.

    Next, what does left = (left+1) % queue.length; do?

    It left+1 < queue.length, it is equivalent to left++. Else, as soon, as left+1 == queue.length, thanks to the modulo-operator, left will be zero.