我的Java接口队列中的thread.main问题,如何解决?

问题描述 投票:0回答:1

我正在做作业,在接口的支持下构建代码以排队,我编写了代码,但是输出了hade main.thread问题,老实说我找不到该问题,但是我确实认为该问题是由插入引起的,主要是尺寸增加,我很欣赏一些建议

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;

    }
}
java oop interface implementation
1个回答
0
投票

像下面一样初始化数组

private int[] heltal = new int[100];

并查看是否有效

© www.soinside.com 2019 - 2024. All rights reserved.