[英]ClassCastException when casting Object[] array to generic type array in Java
Hi I'm very new to Java and in this code, I think I'm not creating the Bag correctly in the Main? 嗨,我是Java的新手,在这段代码中,我认为我没有在Main中正确创建Bag? Please help thanks!
请帮忙谢谢!
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; 线程“主”中的异常java.lang.ClassCastException:[Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
无法转换为[Ljava.lang.Comparable; at mid.Bag.(Bag.java:12) at mid.Bag.main(Bag.java:91)
在包包中间(Bag.java:12)在中间包包主要(Bag.java:91)
public class Bag<T extends Comparable<T>> implements Iterable<T> {
private int MAX_ITEMS = 10; // initial array size
private int size;
private T[] data;
public Bag( ) {
data = (T []) new Object[MAX_ITEMS];
size = 0;
}
public void add(T newItem) {
// check if it's full, then extend (array resizing)
if (size == data.length) {
T[ ] temp = (T [ ] ) new Object[data.length*2];
for (int i = 0; i < size; ++i)
temp[i] = data[i];
// reassign data to point to temp
data = temp;
}
// then do the assignment
data[size++] = newItem; // assign newItem in the next-available slot
}
public Iterator<T> iterator() {
return new BagIterator();
}
/***************************
* nested class BagIterator
***************************/
class BagIterator implements Iterator<T> {
// instance member
private int index;
// (0) constructor
public BagIterator() {
index = 0;
}
// (1)
public boolean hasNext() {
return (index < size); // size in the outer Bag<E>
}
// (2)
public T next() {
/*
T temp = data[index]; // save the element value
index++; // increment index
return temp;
*/
return data[index++];
}
public static void main(String[ ] args) {
Bag<String> bag1=new Bag<String>();
bag1.add("good");
bag1.add("fortune");
bag1.add("billionarie");
for (String x: bag1)
System.out.println(x);
}
Yes, you're creating an Object[]
and then trying to cast it to T[]
, which the compiler is converting to a cast to Comparable[]
(using the raw Comparable
type) due to your constraint on T. 是的,您正在创建一个
Object[]
,然后尝试将其Comparable[]
转换为T[]
,由于您对T的限制,编译器将其转换为Comparable[]
转换为Comparable[]
(使用原始Comparable
类型)。
Arrays and generics don't work terribly nicely together, basically. 基本上,数组和泛型不能很好地协同工作。
It would probably be simpler to make your data
field just an Object[]
and cast individual values where necessary. 将
data
字段设置为Object[]
并在必要时强制转换单个值可能会更简单。
Here: 这里:
data = (T []) new Object[MAX_ITEMS];
you are constructing an Object
array and trying to cast it to T[]
. 您正在构造一个
Object
数组,并尝试将其强制转换为T[]
。 But you have declared that T
inherits from Comparable
. 但是您已经声明
T
继承自Comparable
。 So use: 因此使用:
data = (T []) new Comparable[MAX_ITEMS];
You can probably rewrite your constructor as well: 您可能还可以重写您的构造函数:
public Bag(Class<T> c, int s) {
// Use Array native method to create array of a type only known at run time
@SuppressWarnings("unchecked")
final T[] dataArray = (T[]) Array.newInstance(c, s);
this.data = dataArray;
}
Then you can use it like: 然后您可以像这样使用它:
Bag<String> bag1 = new Bag<>(String.class,10);
That should also work, IMO. IMO,这也应该起作用。 The instances of T must be comparable in any case.
在任何情况下,T的实例都必须是可比较的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.