Added toArray() that accepts a runtime-type token class literal and creates

an array of the correct size.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2645 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2009-11-19 22:04:06 +00:00
parent dee9bbc5f9
commit 690c58a6ff
@@ -20,6 +20,8 @@
package com.samskivert.util;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
@@ -148,6 +150,17 @@ public class CollectionUtil
return subset;
}
/**
* Returns an Array, of the type specified by the runtime-type token <code>type</code>,
* containing the elements of the collection.
*/
public static <T> T[] toArray (Collection<? extends T> col, Class<T> type)
{
@SuppressWarnings("unchecked")
T[] array = (T[]) Array.newInstance(type, col.size());
return col.toArray(array);
}
/**
* If a collection contains only <code>Integer</code> objects, it can be
* passed to this function and converted into an int array.