gpt4 book ai didi

java - 循环数组队列错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:27:05 26 4
gpt4 key购买 nike

<分区>

代码中有一个错误,说不能创建 E 的泛型数组。谁能帮我解决这个问题

    **Q = new E[N];**

这是完整的代码;

package org.circular;

public class CircularArrayQueue<E> implements QueueADT<E> {

private static final int capacity = 5;
private E[] Q;
private final int N; // capacity
private int f = 0;
private int r = 0;

public CircularArrayQueue() {
this(capacity);
}

public CircularArrayQueue(int capacity) {
N = capacity;
Q = new E[N];
}

public int size() {
if (r > f)
return r - f;
return N - f + r;
}

public boolean isEmpty() {
return (r == f) ? true : false;
}

public boolean isFull() {
int diff = r - f;
if (diff == -1 || diff == (N - 1))
return true;
return false;
}

public void enqueue(E element) throws FullQueueException {
if (isFull()) {
throw new FullQueueException("Full");
} else {
Q[r] = element;
r = (r + 1) % N;
}
}

public E dequeue() throws EmptyQueueException {
E item;
if (isEmpty()) {
throw new EmptyQueueException("Empty");
} else {
item = Q[f];
Q[f] = null;
f = (f + 1) % N;
}
return item;
}

@Override
public E front() throws EmptyQueueException {
if (isEmpty()) {
throw new EmptyQueueException("Empty");

}
return null;
}

}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com