I am tasked with creating a method that adds to the front (left) of an ArrayDeque without using the Deque library. I have come up with a method though its not adding to the que, it is coming out with an empty que. Here is my addLeft method:
public T[] addLeft(T item){
T[] copyarr = (T[]) new Object[arr.length+1];
if(isEmpty()){
copyarr[frontPos] = item;
}else{
copyarr[frontPos] = item;
frontPos--;
for(int i = 1; i<copyarr.length; i++){
copyarr[i] = arr[i];
}
}
arr = copyarr;
return arr;
}
Here is the test code iv been using:
public class DequeueTest {
public static void main(String[] args) {
Dequeue test = new Dequeue();
test.addLeft(3);
test.addLeft(4);
System.out.println(test.toString());
}
}
Any idea where i have gone wrong ?
arrcome from? However, you are calling addLeft on the Dequeue but the outcome of addLeft just alters thearrbut not the dequeue itself.frontPosare right?