Iterables.toArrayGoogle Guava中有一种方法。
查看源代码,其定义为:
public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) { Collection<? extends T> collection = toCollection(iterable); T[] array = ObjectArrays.newArray(type, collection.size()); return collection.toArray(array); }凡
ObjectArrays.newArray最终委托给方法的样子:
@SuppressWarnings("unchecked") static <T> T[] newArray(Class<T> type, int length) { return (T[]) Array.newInstance(type, length); }因此,似乎无法
@SuppressWarnings完全避免,但是您可以并且至少应该将其限制在最小范围内。
或者,更好的是,仅使用其他人的实现!


![可重用的方法来转换Iterable 到T []? 可重用的方法来转换Iterable 到T []?](http://www.mshxw.com/aiimages/31/570116.png)
