ArrayList.toArray如何返回指定类型的数组

Welkin-Hu Welkin-Hu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如果不带参数使用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.
...全文
评论 9
收藏 已收藏
分享
返回顶部
Welkin-Hu
Java SE 62,621 成员 307,251 内容