gpt4 book ai didi

java - 使用 -Xlint :unchecked for detail error in java 重新编译

转载 作者:行者123 更新时间:2023-12-02 08:56:15 24 4
gpt4 key购买 nike

我有以下错误,但我似乎不知道如何调试它:

Note: MyStack.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

public class MyStack<T> implements MyStackInterface<T>{
private T[] s;
private int size;

public MyStack() {
this.s = (T[])new Object[30];
}

public void push(T x){
if (size==s.length){
T[] b = (T[])new Object[size*2];
int i;
for (i=0;i<s.length;i++){
b[i] = s[i];
}
s=b;
}
s[size++] = x;
}
public T pop(){
if (size == 0){
throw new RuntimeException("Stack Underflow");
}
return s[--size];
}
public T peek(){
if (size==0) throw new RuntimeException("Stack Underflow");
return s[size-1];
}

public boolean isEmpty(){
return size==0;
}
public int size(){
return size;
}
}

最佳答案

编译代码时使用:

javac -Xlint:unchecked

然后你会看到:

MyStack.java:6: warning: [unchecked] unchecked cast
this.s = (T[])new Object[30];
^
required: T[]
found: Object[]
where T is a type-variable:
T extends Object declared in class MyStack\

MyStack.java:11: warning: [unchecked] unchecked cast
T[] b = (T[])new Object[size*2];
^
required: T[]
found: Object[]
where T is a type-variable:
T extends Object declared in class MyStack
2 warnings

最重要的是,问题是由于泛型类型数组的创 build 成的。要解决此问题,请阅读此链接:https://stackoverflow.com/a/530289/588532

关于java - 使用 -Xlint :unchecked for detail error in java 重新编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473079/

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