In our midterm exam teacher asked us to do this question below: Write a generic Queue class which uses an array(not a link list) as a memory storage. It should support generic type parameter and array resizing (both growing and shrinking), but it doesn't have to support iterators.Hints:Queue is a FIFO data structure ; the API for a queue contains enqueue, dequeue,size and isEmpty methods in addition to the constructor; you don't have to implement toString.
I tried to write this code and here's what I wrote. But I don't know how should I check this code. Is it correct or how can I check it?
public class Queue<T> {
public T[] array;
public int N;
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
public void resize(int max) {
T[] temp = (T[]) new Object[max];
for (int i = 0; i < N; i++) {
temp[i] = array[i];
}
array = temp;
}
public void enqueue(T item) {
if (isEmpty()) {
array[0] = item;
N++;
} else if (N == array.length) {
resize(array.length * 2);
array[N] = item;
N++;
} else {
array[N] = item;
N++;
}
}
public T dequeue() {
T value = array[0];
for (int i = 1; i < N; i++) {
array[i - 1] = array[i];
}
array[--N] = null;
return value;
}
}
vogella.com. --- A remark I would just use anObject[]instead of aT[]. This is howArrayList(github.com) does things. Mixing arrays and generics is always a pain since arrays are covariant and retained, while generics are invariant and erasedSystem.arrayCopyis your friend... docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/…