Implementation of a Resizing Array Queue
The code does not compile cleanly.  Use @SuppressWarnings("unchecked") if necessary.


$ javac -Xlint:unchecked ArrayQueue.java
ArrayQueue.java:8: warning: [unchecked] unchecked cast
    private E[] a = (E[]) new Object[1];
                          ^
  required: E[]
  found:    Object[]
  where E is a type-variable:
    E extends Object declared in class ArrayQueue
ArrayQueue.java:26: warning: [unchecked] unchecked cast
        E[] temp = (E[]) new Object[cap];
                         ^
  required: E[]
  found:    Object[]
  where E is a type-variable:
    E extends Object declared in class ArrayQueue
2 warnings





This test would crash:

public class ArrayQueueTest {
    public static void main(String[] args) {
        ArrayQueue&lt;Integer&gt; q = new ArrayQueue&lt;&gt;();
        q.enqueue(1);
        for (Integer n : q) { System.out.println(n); }
    }
}



1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
  at ArrayQueue$ArrayIterator.hasNext(ArrayQueue.java:93)
  at ArrayQueueTest.main(ArrayQueueTest.java:5)