用于用List.toArray()返回Object[]数组并强制转换的问题。

fowlerowen 2005-11-17 09:00:13
我有一个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
...全文
944 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
fowlerowen 2005-11-18
  • 打赏
  • 举报
回复
后来我自己也通过细读Java Doc解决了问题,谢谢大家,就是用 Object[] List.toArray(Object[] obj) 这个方法轻松解决了问题。

问题是,“The correct order”是按什么排序的呢?
j_fan 2005-11-17
  • 打赏
  • 举报
回复
有个笔误,不好意思。instanceof
j_fan 2005-11-17
  • 打赏
  • 举报
回复
拷贝本身并不能解决强制转换的异常抛出。instanceof是个类型转换过程中很重要的函数。
j_fan 2005-11-17
  • 打赏
  • 举报
回复
我是新学的新手,写过一些list的语句。第一个回答的人讲的很有道理。顺着他的思路
你可以试试用 intstanceof 这个判断你的someclass 与Object是否匹配。如果不匹配,当然不能强制转换。这个判断好象很重要。if ( xxx instanceof someclass ) (someclass)*****
这样就不会出cast的异常了
myhotsun 2005-11-17
  • 打赏
  • 举报
回复
这个绝对没有问题
SomeClass[] sc = new SomeClass[list.size()];
list.toArray(sc);
www203 2005-11-17
  • 打赏
  • 举报
回复
楼主加油
fu_wie 2005-11-17
  • 打赏
  • 举报
回复
UP
treeroot 2005-11-17
  • 打赏
  • 举报
回复
数组和其他类型一样
Object[] 是Object的子类型
String[] 也是Object的子类型
他们当然是不兼容的
treeroot 2005-11-17
  • 打赏
  • 举报
回复
可以这么写
SomeClass[] sc = null;
list.toArray(sc);
根本就不需要转化了!

不错才怪!
myhotsun 2005-11-17
  • 打赏
  • 举报
回复
我没事过,不知道会不会出错,如果出错的话数组可能需要初始化一下
SomeClass[] sc = new SomeClass[list.size()];
list.toArray(sc);
myhotsun 2005-11-17
  • 打赏
  • 举报
回复
可以这么写
SomeClass[] sc = null;
list.toArray(sc);
根本就不需要转化了!
Yanbin_Q 2005-11-17
  • 打赏
  • 举报
回复
不能这样整体转换

就像下面的代码也会出现转型错误一样
Object[] o= new Object[2];
o[0]= "1";
o[1]= "2";
String[] s=(String[])o;
System.out.println(s[0]);

但是向上转型是可以的,这就涉及到一个协变的问题
你仍然需要一项一项的转换类型
didoleo 2005-11-17
  • 打赏
  • 举报
回复
/**
* 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;
}
pauliuyou 2005-11-17
  • 打赏
  • 举报
回复
list.toArray返回的是Object [] 所以不能直接造型.
除非你add的时候就是放的SomeClass [] get出来直接造型就可以

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧