Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot find problems with array based deque, but getting outOfBounds exception
    text
    copied!<p>I have been trying to figure this out for an age, but to no avail. I think I must have several problems with my code that I just can't see. I have implemented this using a slightly more complex way before, and was writing it in a simpler form to help a friend in the year below me who is struggling, but I've just ended up getting myself in a muddle!</p> <p>The code is as follows :</p> <pre><code> public class ArrayBasedDeque&lt;EltType&gt; implements Deque&lt;EltType&gt; { private final int CAPACITY = 10; private int capacity; private int end; private EltType deque[]; public ArrayBasedDeque() { this.capacity = CAPACITY; deque = (EltType[]) (new Object[capacity]); } public EltType first() { return deque[0]; } public EltType last() { return deque[end]; } public boolean isEmpty() { return end == 0; } public int size() { return deque.length; } public boolean isFull() { int curSize = size(); return curSize &gt;= capacity; } public void insertFirst(EltType first) { if(!isEmpty()) { EltType[] tempArray; tempArray = (EltType[]) new Object[capacity+1]; for (int i=0;i&lt;deque.length;i++) { tempArray[i+1] = deque[i]; } deque = tempArray; } deque[0] = first; end++; } public void insertLast(EltType last) { if (isFull()){ EltType[] tempArray; tempArray = (EltType[]) new Object[CAPACITY+1]; for (int i=0;i&lt;deque.length;i++) { tempArray[i] = deque[i]; } } deque[end] = last; end++; } public EltType removeFirst() { EltType[] tempArray; EltType returned = deque[0]; tempArray = (EltType[]) new Object[capacity]; for (int i=1;i&lt;capacity;i++) { tempArray[i-1] = deque[i]; } deque = tempArray; end--; return returned; } public EltType removeLast() { EltType[] tempArray; System.out.println(end); EltType returned = deque[end]; tempArray = (EltType[]) new Object[capacity]; for (int i=0;i&lt;deque.length;i++) { tempArray[i] = deque[i]; } deque = tempArray; return returned; } } </code></pre> <p>The problem is that when I call</p> <pre><code>abd.insertFirst( 3 ); abd.insertFirst( 3 ); abd.insertFirst( 3 ); </code></pre> <p>this, it returns an error.</p> <pre><code>java.lang.ArrayIndexOutOfBoundsException: 11 at ArrayBasedDeque.insertFirst(ArrayBasedDeque.java:37) at TestABD.main(TestABD.java:7) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271) </code></pre> <p>the same is true for the insertLast method. I can't figure it out, and was hoping the scrutinizing gaze of stackOverflow could help me. Thanks a lot !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload