diff --git a/src/java/com/samskivert/util/ArrayUtil.java b/src/java/com/samskivert/util/ArrayUtil.java index 02f7711a..ff7028e6 100644 --- a/src/java/com/samskivert/util/ArrayUtil.java +++ b/src/java/com/samskivert/util/ArrayUtil.java @@ -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 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(); }