我有一个List对象list,里面的对象是某类型的数据,如:SomeClass,以下是我的代码:SomeClass[] sc = null;
sc = (SomeClass[])list.toArray();结果sc并没有成功获得数据,而是在转换时抛出ClassCastException?重写代码:SomeClass[] sc = null;
sc = (SomeClass[])list.toArray(new SomeClass[list.size()]);结果没有问题,很奇怪,想请教List.toArray() 和 List.toArray(Object[] obj)有什么不同呢?补充一句,我的环境是 jdk1.4.2
sc = (SomeClass[])list.toArray();结果sc并没有成功获得数据,而是在转换时抛出ClassCastException?重写代码:SomeClass[] sc = null;
sc = (SomeClass[])list.toArray(new SomeClass[list.size()]);结果没有问题,很奇怪,想请教List.toArray() 和 List.toArray(Object[] obj)有什么不同呢?补充一句,我的环境是 jdk1.4.2
解决方案 »
- 如何打乱数组
- Java中一个关于数据删除的很怪的问题
- 这个应该怎么写?
- 什么内存出错,高手来帮忙看看啦
- 帮忙看看下面这点小代码?是不是因为jdk的版本造成的!(关于java.lang.Integer)
- 小弟,遇到了java.lang.NullPointerException,帮忙看看问题出在哪里,送分100,急,在线等.....
- 急问:在本机器上jsp里怎么连接别人机器上的数据库?有代码实例最好!!
- 几个Java 开发过程中的问题
- 谁有thinking in java 3的 电子版 ????
- 从map 中取出来的键值对 值是对象,如何转化成具体的对象,key不确定
- Runtime.getRuntime.exe() 为什么只能启动windows 自带的进程?
- 再求一个学习tomcat的好书:
除非你add的时候就是放的SomeClass [] get出来直接造型就可以
* Returns an array containing all of the elements in this list
* in the correct order.
*
* @return an array containing all of the elements in this list
* in the correct order.
*/
public Object[] toArray() {
Object[] result = new Object[size];
System.arraycopy(elementData, 0, result, 0, size);
return result;
} /**
* Returns an array containing all of the elements in this list in the
* correct order; the runtime type of the returned array is that of the
* specified array. If the list fits in the specified array, it is
* returned therein. Otherwise, a new array is allocated with the runtime
* type of the specified array and the size of this list.<p>
*
* If the list fits in the specified array with room to spare (i.e., the
* array has more elements than the list), the element in the array
* immediately following the end of the collection is set to
* <tt>null</tt>. This is useful in determining the length of the list
* <i>only</i> if the caller knows that the list does not contain any
* <tt>null</tt> elements.
*
* @param a the array into which the elements of the list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of the list.
* @throws ArrayStoreException if the runtime type of a is not a supertype
* of the runtime type of every element in this list.
*/
public Object[] toArray(Object a[]) {
if (a.length < size)
a = (Object[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size)
a[size] = null; return a;
}
Object[] o= new Object[2];
o[0]= "1";
o[1]= "2";
String[] s=(String[])o;
System.out.println(s[0]);但是向上转型是可以的,这就涉及到一个协变的问题
你仍然需要一项一项的转换类型
SomeClass[] sc = null;
list.toArray(sc);
根本就不需要转化了!
SomeClass[] sc = new SomeClass[list.size()];
list.toArray(sc);
SomeClass[] sc = null;
list.toArray(sc);
根本就不需要转化了!不错才怪!
Object[] 是Object的子类型
String[] 也是Object的子类型
他们当然是不兼容的
SomeClass[] sc = new SomeClass[list.size()];
list.toArray(sc);
你可以试试用 intstanceof 这个判断你的someclass 与Object是否匹配。如果不匹配,当然不能强制转换。这个判断好象很重要。if ( xxx instanceof someclass ) (someclass)*****
这样就不会出cast的异常了