gpt4 book ai didi

java - 数组队列 - 使用 -xlint : unchecked/unsafe operations 重新编译

转载 作者:行者123 更新时间:2023-12-01 04:20:28 26 4
gpt4 key购买 nike

可能有很多错误,抱歉。我是java新手,我对所有语法之类的东西都感到相当困难。我无法克服这个错误,但我看不到它在哪里。我什至无法编译,因为我收到消息:“...使用未经检查或不安全的操作。注意:使用 -Xlint 重新编译:未检查详细信息”请帮忙。

这应该实现 Queue 接口(interface),这几乎就是

public boolean enqueue(Item item);
public Item dequeue();
public int size;
public boolean isEmpty();
public boolean isFull();

我确实尝试过将其转为循环,但我不知道是否成功。我认为是那些泛型带来了问题,我不知道。

    public class ArrayQueue<Item> implements Queue<Item> {
private Item[] q;
public int N = 0;
private int tail = 0;
private int head = 0;

public ArrayQueue(int capacity) {
q = (Item[]) new Object [capacity];
}

public boolean enqueue(Item item) {
if (isFull())
return false;
q[tail++] = item;

if(tail == q.length )
tail = 0;
N++;
return true;
}

public Item dequeue( ) {
if (isEmpty( ) )
return null;
N--;
Item headItem = q[head];
q[head++] = null;
if(head == q.length )
head = 0;

return headItem;
}



public int size( ) {
return N;
}

public boolean isFull() {
return N == q.length;
}


public boolean isEmpty() {
return N == 0;
}
}

最佳答案

有关“未经检查或不安全操作”的警告不应阻止代码的编译。您必须修复其他错误。请注意,如果没有收到此类警告(或抑制它们),就不可能实现基于数组的通用集合。这就是为什么最好使用现有的 JFC Collection 实现,因为您可以期望它们的开发人员足够小心且经验丰富,能够区分不可避免的警告和实际问题。

关于java - 数组队列 - 使用 -xlint : unchecked/unsafe operations 重新编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18972166/

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