如果不带参数使用ArrayList.toArray,只能返回Object[]。
我想要的是整形数组,如何实现呢。JDK 1.5的帮助文档如下,我看了还是不知道如何用。
<T> T[] toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the Collection.toArray(Object[]) method. Specified by:
toArray in interface Collection<E>
Parameters:
a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of this list.
Throws:
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
NullPointerException - if the specified array is null.
我想要的是整形数组,如何实现呢。JDK 1.5的帮助文档如下,我看了还是不知道如何用。
<T> T[] toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the Collection.toArray(Object[]) method. Specified by:
toArray in interface Collection<E>
Parameters:
a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of this list.
Throws:
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
NullPointerException - if the specified array is null.
解决方案 »
- 新人的一个小题求大牛给解释一下,谢谢了
- 怎样显示按钮的快捷键?
- 初学者简单问题
- JDeveloper10 中使用的问题
- 谁知道如何在j2ee环境下使用crystalreport10的java reportting component报表组件开发报表
- 如何用java在sql做查询,删除 ,修改等操作?
- 为什么有些语句一定要用try,否则就回报编译错误?
- 在JAVA中如何实现在一个长字符串查找某个字符串??
- 一个CMP的EntityBean查询多条记录时,要在Bean体中手写方法的代码,那不是和用BMP一样吗,那位大虾详细解释一下!
- 求助,编程挑战代码运行效率问题
- ftp下载中进度条的问题
- java怎么实现richtextbox 用.net做不出来这东西
先转成Object,然后再转成int.
String[] strArr= new String[10];
dataList.toArray(strArr);
这样的话就可以了。不用使用泛型
dataList.toArray(strArr);
我同意interhanchi(闭关修练中!) 的说法,因为在一个ArrayList中加入元素时,这个元素必须是一个object型的,不能是基本数据类型,如果要加入基础数据类型的元素,必须用到对应的类来转换,如int型必须用Integer类来转,Arraylist.add(new Integer(i))。相反,如果要返回list中的数据组,也要用Integer来转回。
<T> T[] toArray(T[] a)
(int[])xxx.toArray();