phishman3579/java-algorithms-implementation

Possible bug in ArryaQueue

haoluoluo opened this issue · 1 comments

function next in ArrayQueueIterator maybe happen ArrayIndexOutOfBoundsException;
because "queue.firstIndex+index" maybe not the real index
public T next() {
if (queue.firstIndex+index < queue.lastIndex) {
last = queue.firstIndex+index;
return queue.array[queue.firstIndex+index++];
//this is my change
// return queue.array[(queue.firstIndex+index++) % queue.array.length];
}
return null;
}

this is a simple test
IQueue arrayQueue = new Queue.ArrayQueue();
for(int i=0;i<1024;i++){

        arrayQueue.offer(i);
    }
    System.out.println(arrayQueue.poll());
    arrayQueue.offer(1024);
    Iterator it = arrayQueue.toQueue().iterator();
    while (it.hasNext()){
        System.out.println(it.next());
    }

That is absolutely a bug and your fix is the correct solution. Thanks.