I am tasked with creating a Deque in java with options to add left, add right, remove left and remove right. 

I have coded the add right and remove right methods successfully however I have issues trying to get add left and remove left working. 

I think I have gone massively wrong somewhere. I have just tried swapping round the variables for add left and reversing the calculations which did not work and would just come up with the following : 

```
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space 
at java.util.Arrays.copyOf(Arrays.java:3332) 
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137) 
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121) 
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:421) 
at java.lang.StringBuffer.append(StringBuffer.java:265) 
at datastructuresass1.DendQueue.toString(DendQueue.java:107) 
at datastructuresass1.main.main(main.java:21) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

```

I am trying to create the deque using arrays. Below is my code for the add right and add left methods: 

Add Left (which is not working) 

```
public void addLeft(T o){

         left = (left + 1) % arr.length;
         arr[left] = o;

        // if the array is full copy it to a larger one
        if ((left + 1) % arr.length == right) {
            T[] newarr = (T[]) new Object[arr.length * 2];
            int i;
            for (i = 0; i < arr.length; i++)
                newarr[i] = arr[(right - i) % arr.length];
            arr = newarr;
            left = i - 1;
            right = 0;

            System.out.println("Array size increased to " + 
            arr.length);
        }

```

Add Right (Which is working) 

```
public void addRight(T o) {
        right = (right + 1) % arr.length;
        arr[right] = o;

        // if the array is full copy it to a larger one
        if ((right + 1) % arr.length == left) {
            T[] newarr = (T[]) new Object[arr.length * 2];
            int i;
            for (i = 0; i < arr.length; i++)
                newarr[i] = arr[(left + i) % arr.length];
            arr = newarr;
            left = 0;
            right = i - 1;

            System.out.println("Array size increased to " + 
            arr.length);
        }

```

Please could someone explain to me why this addLeft method is not working. It would be a great help as I have been stumped on this for a while! Thanks in advance.