java - 我的java接口(interface)队列的thread.main问题,如何解决?

标签 java oop interface implementation

我正在做作业,构建代码以支持接口(interface)的队列,我编写了代码,但输出有 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;

    }
}

最佳答案

像下面一样初始化你的数组

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

看看它是否有效

关于java - 我的java接口(interface)队列的thread.main问题,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59923849/

相关文章:

java - 如何制作包含 x-z 数字的 n 个数字的列表

java - 使用 Kotlin DSL 配置 Java 规范时出现 Gradle 编译错误

c++ - 构造函数中没有匹配的调用函数 - C++ 11

C# - 复杂变量赋值

iphone - Xcode 3 无法打开界面生成器

java - 如何在不强制转换的情况下在具有相同数据类型参数的另一个泛型类(或接口(interface))中使用泛型类(或接口(interface))

java - 在非常慢的 2G 互联网上下载 xml 数据

java - 使用标签为 EBS 卷创建快照

javascript - 我应该为 OOP Javascript Web 应用程序使用什么类型的数据管理系统?

c++ - 我的复制构造函数导致使用我的类的方法失败。有人可以看一下并告诉我我做错了什么吗?