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();
}
}
}