Added an array equivalent of Collection.toArray(T[]) to convert arrays

to sub-types.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1967 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
andrzej
2006-11-08 03:06:36 +00:00
parent 162deee43d
commit 154be182a6
@@ -22,6 +22,7 @@ package com.samskivert.util;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Comparator;
import java.util.Random;
@@ -817,6 +818,23 @@ public class ArrayUtil
return values;
}
/**
* Similar to {@link Collection#toArray}, this method copies the contents
* of the first parameter to the second, creating a new array of the same
* type if the destination array is too small to hold the contents of the
* source.
*/
public static <S extends Object, T extends S> T[] copy (
S[] values, T[] store)
{
@SuppressWarnings("unchecked")
T[] dest = (store.length >= values.length) ? store :
(T[])Array.newInstance(store.getClass().getComponentType(),
values.length);
System.arraycopy(values, 0, dest, 0, values.length);
return dest;
}
/** The default random object used when shuffling an array. */
protected static Random _rnd = new Random();
}