我的java接口队列的thread.main问题,如何解决?
我正在做我的家庭作业,在接口的支持下构建代码来排队,我写了我的代码,但是输出hade main.thread的问题,老实说,我找不到问题,但是我确实相信问题来自插入,主要是大小增加,我感谢一些建议我的java接口队列的thread.main问题,如何解决?,java,oop,interface,implementation,Java,Oop,Interface,Implementation,我正在做我的家庭作业,在接口的支持下构建代码来排队,我写了我的代码,但是输出hade main.thread的问题,老实说,我找不到问题,但是我确实相信问题来自插入,主要是大小增加,我感谢一些建议 public class MyQueue implements IntQueue { private int[] heltal; private int size; public void enqueue(int tal) { // Inserts the specifie
public class MyQueue implements IntQueue {
private int[] heltal;
private int size;
public void enqueue(int tal) {
// Inserts the specified element into the end of this queue.
// increases the size after every insertion
if (size == 0) {
size++;
heltal[0] = tal;
int[] newArr = new int[heltal.length * 2];
for (int i = 0; i < heltal.length; i++) {
newArr[i] = heltal[i];
}
heltal = newArr;
}
return;
}
public int dequeue() throws NoSuchElementException {
// Returns the head of this queue and removes it.
// Throws an exception if this queue is empty.
if (empty())
throw new NoSuchElementException("The queue is empty");
int NewValue = heltal[0];
for (int i = 1; i < size; i++) {
heltal[i - 1] = heltal[i];
}
heltal[size - 1] = 0;
size--;
return NewValue;
}
@Override
public int peek() throws NoSuchElementException {
// Retrieves, but does not remove, the head of this queue.
// Throws an exception if this queue is empty.
if (empty())
throw new NoSuchElementException("The queue is empty");
return heltal[0];
}
@Override
public boolean empty() {
// Checks if this queue is empty.
return size == 0;
}
}
公共类MyQueue实现IntQueue{
私人国际[]赫尔塔尔;
私有整数大小;
公共无效排队(整数){
//将指定的元素插入此队列的末尾。
//增加每次插入后的大小
如果(大小==0){
大小++;
heltal[0]=tal;
int[]newArr=newint[heltal.length*2];
对于(int i=0;i
如下所示初始化阵列
private int[] heltal = new int[100];
看看它是否有效这里有这么多错误。只有当队列为空时,才插入一个值,总是将队列的大小增加一倍,将原始/数组用于无界队列,并且您的问题将此作为与线程相关的问题提出,而所有代码都不涉及线程或并发原语。然而,迪帕克是正确的。如果执行了enqueue方法,则数组最初为null,导致
hental[0]=…
失败。