List<Integer> integers = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) {
integers.add(i);
}
Integer[] integers2 = integers.toArray(new Integer[integers.size()]);
这样就可以把集合中的数据转为数组了!
以上方法是参考JDK 1.6 的API中的ArrayList的文档说明!
<table border="1" cellspacing="0" summary="" cellpadding="3" width="100%"><tbody><tr class="TableRowColor" bgcolor="white"><td valign="top" width="1%" align="right"><span size="-1" style=""><code></code></span><table border="0" cellspacing="0" summary="" cellpadding="0"><tbody><tr valign="top" align="right"><td nowrap="nowrap"><span size="-1" style=""><code><T> T[]</code></span></td></tr></tbody></table></td><td><code><strong><a target=_blank href="">toArray</a></strong>(T[] a)</code>
按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。</td></tr></tbody></table>
public <T> T[] toArray(T[] a)
-
按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。如果指定的数组能容纳列表,则将该列表返回此处。否则,将分配一个具有指定数组的运行时类型和此列表大小的新数组。
如果指定的数组能容纳队列,并有剩余的空间(即数组的元素比队列多),那么会将数组中紧接 collection 尾部的元素设置为 null。(仅 在调用者知道列表中不包含任何 null 元素时才能用此方法确定列表长度)。
-
-
指定者:
-
接口
Collection<E>
中的toArray
指定者:
-
接口
List<E>
中的toArray
覆盖:
-
类
AbstractCollection<E>
中的toArray
-
接口
-
-
参数:
-
a
- 要在其中存储列表元素的数组(如果它足够大);否则,为此分配一个具有相同运行时类型的新数组。
返回:
- 包含列表元素的数组 抛出:
-
ArrayStoreException
- 如果指定数组的运行时类型不是此列表每个元素的运行时类型的超类型 -
NullPointerException
- 如果指定数组为 null
-