From 154be182a6772f6106926cf7b828411ce1d8fcd2 Mon Sep 17 00:00:00 2001 From: andrzej Date: Wed, 8 Nov 2006 03:06:36 +0000 Subject: [PATCH] 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 --- src/java/com/samskivert/util/ArrayUtil.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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(); }