Ljava.lang.Object;不能投射到[Ljava.lang.Integer [英] Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer
问题描述
我写了一个泛型类,下面是该类的构造函数。我想按照这样写:
elements =(E [])new Object [size]
因为我不知道运行时的泛型类型,所以它会抛出一个异常。
public class Stack< E>实现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);如果(size <0){
抛出new IllegalArgumentException(初始容量不能为负数或零),则返回
。 ;
}
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(); (int k = 0; k <10; k ++){
minMaxStack.push(ran.nextInt(100))的
;
}
System.out.println(minMaxStack);
}
}
public class MinMaxStack extends Stack< Integer>实现IMinMaxStack< Integer> {
private int min;
private int max;
/ *
*方法1:
*对于push方法,我们可以推送并更新最小/最大值
*对于pop方法,我们将遍历整个堆栈以找出新的最小值/最大值
*
* /
@Override
public void push(Integer element){
if(isEmpty()){
this。 min = element;
this.max = element;
元素[top + 1] =元素;
size ++;
} else {
if(element min = element;
}
if(element> max){
max = element;
}
elements [top + 1] = element;
size ++;
}
}
}
公共类Stack< E>实现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);如果(size <0){
抛出new IllegalArgumentException(初始容量不能为负数或零),则返回
。 ;
}
elements =(E [])new Object [size];
}
public void push(E element){
ensureCapacity();
元素[top + 1] =元素;
size ++;
}
}
public interface IStack< E> {
public void push(E element);
}
public interface IMinMaxStack< E>扩展IStack< E> {
public int min();
public int max();
更新2 :
似乎,除了传递下面答案中提到的类类型之外,我们无法做到这一点。
这是
class Stack< E> {
protected E [] elements =(E [])new Object [1];
}
类IntStack扩展堆栈<整数> {
void push(Integer i){
//巧妙地将元素作为Integer []进行访问,它不是
elements [0] = i;
$ b 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;
$ b明显是一个新对象[ ]
不是整数[]
。注意演员是如何移动到你没有明确表达的地方的。这就是为什么(E [])new Object [size]
是未经检查的投射并显示警告。
相反,您应该使用 Object []
并仅在需要将元素返回到外部世界时执行未选中的转换。
class Stack< E> {
private Object []元素;
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)元素[size - 1];
size--;
return e;
}
}
I have written a generic class and below is the constructor of the class. I want to do something like this as written in line
elements = (E[])new Object[size]
Because I do not know the generic type on the run time therefore it will throw an exception.
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];
}
}
Is there any way to solve such problems? The declaration of E is
protected E[] elements = null;
This is how I am trying to call
Random ran = new Random();
Stack<Integer> st = new Stack<Integer>();
st.push(ran.nextInt(100));
Update
Guys, Thanks for the help. I was messing around with generics so the problem was being created. Here is all the code which created the problem -
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();
}
Update 2:
Seems, other than passing the class type as mentioned in an answer below, there is no way we can do this.
解决方案 Here is the most-minimal code necessary to reproduce your exception.
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 generics are implemented with type erasure so after compilation, this code translates to something like this:
class Stack {
protected Object[] elements = new Object[1];
}
class IntStack extends Stack {
void push(Integer i) {
// throws ClassCastException
((Integer[])elements)[0] = i;
}
}
Clearly a new Object[]
is not an Integer[]
. Notice how the cast gets moved to somewhere you did not explicitly put it. This is why (E[])new Object[size]
was an unchecked cast and displayed a warning.
Instead, you should use Object[]
and perform the unchecked cast only when you need to return an element to the outside world.
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;
}
}
这篇关于Ljava.lang.Object;不能投射到[Ljava.lang.Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!