my display() method is public and looks to be defined , however eclipse says that its not defined.
this is a code for an amortized time resizing circular array. can someone help me out here ? also , please do point out if you see something else wrong with the code.

thanks in advance.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class ResizingCircularArray<E>{

    private int head = 0; // head , tail are modulo capacity , ie modulo arr.length
    private int tail = 0;
    private int size = 0; // a measure of non-null elements in the array
    private E[] arr;
    private void resize(){

        @SuppressWarnings("unchecked")
        E[] tempArr = (E[]) new Object[2*size];
        System.arraycopy(arr, head , tempArr , 0 , size );
        arr = tempArr;
    }

    public void enqueue(E item){
        // when size = arr.length , and tail has made one loop and now pointing to head.
        if( tail == head && size!=0 ){ resize(); }
        arr[tail] = item;
        tail = ( tail + 1 ) % arr.length;
        size++;     
    }
    public E dequeue(){
        E item = arr[head];
        head = ( head + 1 ) % arr.length; // dequeing leads to size reduction , making the head move more towards tail , hence + 1.
        size--;
        if(size == arr.length/4){ resize(); }
        return item;
    }
    public int size(){ return size; }   

    public void display(){
        for(E item : arr)
            System.out.print(" " + item);
        System.out.println("\n\n");
    }

    @SuppressWarnings("unchecked")
    public ResizingCircularArray(){
        arr = (E[]) new Object[5];
    }

    public static void main(String[] args){

        ResizingArrayQueue<String> r = new ResizingArrayQueue<String>();
        String line = null;
        String[] parsed;

        try(BufferedReader is = new BufferedReader(new InputStreamReader(System.in))){
            while((line = is.readLine())!=null){
                if(line.equals("stop")) break;
                parsed = line.trim().split("[ ]+");
                switch (parsed[0]) {
                case "enq":
                    System.out.println(" adding : " + parsed[1]);
                    r.enqueue(parsed[1]);                   
                    break;
                case "deq":
                    System.out.println(" removing : " + r.dequeue());
                    break;
                case "disp":
                    r.display();
                    break;
                default:
                    break;
                }
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

Dani AI

Generated

Quick summary and practical fixes (follow-up to and ): Eclipse reporting that display() is undefined usually means the declared type of the variable holding the object doesn't declare that method — often because an auto-import picked a similarly named class from another package/JAR. Confirm the import and the declared type (use the Quick Fix on the type name or open the type to see its fully qualified name) and change the import or declaration to the class that actually defines display().

Notes on the incorrect dequeue sequence and resilient resizing logic: two frequent causes are (a) performing a single System.arraycopy when the logical contents wrap around the end of the backing array, and (b) choosing the wrong new capacity (for example, using 2 * size in growth where 2 * capacity is intended). On any resize the elements must be copied in logical order, then head must be reset to 0 and tail set to size. Also enforce a sensible minimum capacity and avoid aggressive shrinking (shrink only when size <= capacity/4) to prevent thrashing.

Robust resize examples (safe to paste into the class):

private void resize(int newCap) {
    @SuppressWarnings("unchecked")
    E[] newArr = (E[]) new Object[newCap];
    for (int i = 0; i < size; i++) {
        newArr[i] = arr[(head + i) % arr.length];
    }
    arr = newArr;
    head = 0;
    tail = size;
}

Alternative using two System.arraycopy calls:

int first = Math.min(size, arr.length - head);
System.arraycopy(arr, head, newArr, 0, first);
if (size - first > 0) System.arraycopy(arr, 0, newArr, first, size - first);

Final troubleshooting tips: keep a small set of unit tests (enqueue many -> trigger grow -> dequeue many -> trigger shrink) and print invariants (head, tail, size, capacity) while debugging. Credit to for identifying the import/typing issue and to for simplifying the buffer logic during debugging.

Recommended Answers

All 5 Replies

I don't see anything wrong there, but since we don't have the entire code, we can't actually test it, I guess.

r is an instance of ResizingArrayQueue whereas the display method is part of ResizingCircularArray?

commented: guess I missed the obvious there :) +14
commented: *facepalm* to me. +3

thanks a lot mr Baen Mido !! kudos to your evil eyes... i hope you can use that for more than thrice a day :)

i never noticed that eclipse auto code added something else from my jar package... thanks!

commented: You bet I can ;) +14

i marked it solved , but something's wrong with the code... for the record , im marking it unsolved again
(dont want a faulty code in a solved thread)

i changed the code to read from a file , being :

enq a
enq b
enq c
enq d
enq e
enq f
enq g
enq h
enq i
enq j
enq k
deq
deq
deq
deq
deq
deq
deq

and it gave this as output :
adding : a
adding : b
adding : c
adding : d
adding : e
adding : f
resizing array to size: 10
adding : g
adding : h
adding : i
adding : j
adding : k
removing : f
removing : g
removing : h
removing : i
removing : j
removing : k
removing : null

somethings not right... ill see if i can figure it out. any suggestions would be great as well.
edit : i think my resize condition at enqueue is causing the problem.

edit 2 : overthought the whole modulo thing. not needed . simplified (and working) change :

public void enqueue(E item) {
        // when size = arr.length , and tail has made one loop and now pointing
        // to head.
        arr[size++] = item;
        if (size == arr.length) {
            resize();
        }       

        System.out.println(" head : " + head + " tail : " + tail + " , size : " + size);
    }

    public E dequeue() {
        E item = arr[head++];
        size--;
        System.out.println(" head : " + head + " tail : " + tail + " , size : " + size);
        if (size == arr.length / 4) {
            resize();
            System.out.println(" head : " + head + " tail : " + tail + " , size : " + size);
        }

        return item;
    }

finer debugs needed. but essentially solved i hope. thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.