gpt4 book ai didi

java - Ljava.lang.Object;不能转换为 [Ljava.lang.Integer

转载 作者:搜寻专家 更新时间:2023-11-01 04:05:49 25 4
gpt4 key购买 nike

我写了一个泛型类,下面是类的构造函数。我想按照行写的那样做这样的事情

elements = (E[])new Object[size] 

因为我在运行时不知道泛型类型所以它会抛出异常。

public class Stack<E> implements IStack<E> {
protected E[] elements = null;
protected int top = -1;
protected int size= 0;
private static final int DEFAULT_CAPACITY = 10;

public Stack(){
this(DEFAULT_CAPACITY);
}

public Stack(int size){
if(size <0){
throw new IllegalArgumentException("Initial capacity cannot be negative or zero");
}
ArrayList<Integer> ar = new ArrayList<Integer>();
elements = (E[])new Object[size];
}
}

有什么办法可以解决这样的问题吗? E的声明是

protected E[] elements = null;    

这就是我想要调用的方式

Random ran = new Random();
Stack<Integer> st = new Stack<Integer>();
st.push(ran.nextInt(100));

更新伙计们,谢谢你的帮助。我在搞乱泛型,所以问题就产生了。这是造成问题的所有代码 -

public class StackMain {
public static void main(String[] args) {
MinMaxStack minMaxStack = new MinMaxStack();
Random ran = new Random();
for (int k = 0; k < 10; k++) {
minMaxStack.push(ran.nextInt(100));
}
System.out.println(minMaxStack);
}
}

public class MinMaxStack extends Stack<Integer> implements IMinMaxStack<Integer>{

private int min;
private int max;
/*
* Approach 1:
* For push method we can push and update the minimum/maximum value
* For pop method we will be traversing whole stack to find out the new minimum/maximum
*
*/
@Override
public void push(Integer element){
if(isEmpty()){
this.min = element;
this.max = element;
elements[top+1] = element;
size++;
}else{
if(element < min){
min = element;
}
if(element > max){
max = element;
}
elements[top+1] = element;
size++;
}
}
}

public class Stack<E> implements IStack<E> {
protected E[] elements = null;
protected int top = -1;
protected int size= 0;
private static final int DEFAULT_CAPACITY = 10;

public Stack(){
this(DEFAULT_CAPACITY);
}

public Stack(int size){
if(size <0){
throw new IllegalArgumentException("Initial capacity cannot be negative or zero");
}
elements = (E[])new Object[size];
}

public void push(E element) {
ensureCapacity();
elements[top+1] = element;
size++;
}
}

public interface IStack<E> {
public void push(E element );
}


public interface IMinMaxStack<E> extends IStack<E> {
public int min();
public int max();
}

更新 2:似乎,除了下面的答案中提到的传递类类型之外,我们无法做到这一点。

最佳答案

这是重现异常所需的最少代码。

class Stack<E> {
protected E[] elements = (E[])new Object[1];
}

class IntStack extends Stack<Integer> {
void push(Integer i) {
// subtly accessing elements as Integer[] which it's not
elements[0] = i;
}
}

Java 泛型是用 type erasure 实现的所以在编译之后,这段代码会翻译成这样:

class Stack {
protected Object[] elements = new Object[1];
}

class IntStack extends Stack {
void push(Integer i) {
// throws ClassCastException
((Integer[])elements)[0] = i;
}
}

显然 new Object[] 不是 Integer[]。请注意类型转换是如何移动到您没有明确放置的地方的。这就是为什么 (E[])new Object[size]未经检查的转换并显示警告的原因。

相反,您应该使用 Object[] 并仅在需要向外界返回元素时才执行未经检查的转换。

class Stack<E> {
private Object[] elements;
private int size;

Stack(int len) {
elements = new Object[len];
}

void push(E e) {
elements[size] = e;
size++;
}

E pop() {
@SuppressWarnings("unchecked");
E e = (E)elements[size - 1];
size--;
return e;
}
}

关于java - Ljava.lang.Object;不能转换为 [Ljava.lang.Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27591061/

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