From b9e2c2c0e5265923e63f505886faa4d5d3e743b0 Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 6 Feb 2003 18:51:50 +0000 Subject: [PATCH] git-svn-id: https://samskivert.googlecode.com/svn/trunk@1050 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ArrayUtil.java | 54 ++++++++++++++++++- .../java/com/samskivert/util/IntListUtil.java | 14 +---- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java b/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java index 71b8796b..25814749 100644 --- a/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java @@ -1,5 +1,5 @@ // -// $Id: ArrayUtil.java,v 1.24 2003/02/04 03:12:30 mdb Exp $ +// $Id: ArrayUtil.java,v 1.25 2003/02/06 18:51:50 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Walter Korman @@ -20,6 +20,8 @@ package com.samskivert.util; +import java.lang.reflect.Array; + import java.util.ArrayList; import java.util.Comparator; import java.util.Random; @@ -623,6 +625,56 @@ public class ArrayUtil return nvalues; } + /** + * Creates a new array one larger than the supplied array and with the + * specified value inserted into the last slot. + */ + public static byte[] append (byte[] values, byte value) + { + byte[] nvalues = new byte[values.length+1]; + System.arraycopy(values, 0, nvalues, 0, values.length); + nvalues[values.length] = value; + return nvalues; + } + + /** + * Creates a new array one larger than the supplied array and with the + * specified value inserted into the last slot. + */ + public static short[] append (short[] values, short value) + { + short[] nvalues = new short[values.length+1]; + System.arraycopy(values, 0, nvalues, 0, values.length); + nvalues[values.length] = value; + return nvalues; + } + + /** + * Creates a new array one larger than the supplied array and with the + * specified value inserted into the last slot. + */ + public static int[] append (int[] values, int value) + { + int[] nvalues = new int[values.length+1]; + System.arraycopy(values, 0, nvalues, 0, values.length); + nvalues[values.length] = value; + return nvalues; + } + + /** + * Creates a new array one larger than the supplied array and with the + * specified value inserted into the last slot. The type of the values + * array will be preserved. + */ + public static Object[] append (Object[] values, Object value) + { + Object[] nvalues = (Object[])Array.newInstance( + values.getClass().getComponentType(), values.length+1); + System.arraycopy(values, 0, nvalues, 0, values.length); + nvalues[values.length] = value; + return nvalues; + } + /** The default random object used when shuffling an array. */ protected static Random _rnd = new Random(); } diff --git a/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java b/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java index 8d365264..a5d78415 100644 --- a/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java @@ -1,5 +1,5 @@ // -// $Id: IntListUtil.java,v 1.4 2003/02/04 03:12:30 mdb Exp $ +// $Id: IntListUtil.java,v 1.5 2003/02/06 18:51:50 mdb Exp $ package com.samskivert.util; @@ -355,18 +355,6 @@ public class IntListUtil return idx; } - /** - * Creates a new array one larger than the supplied array and with the - * specified value inserted into the last slot. - */ - public static int[] append (int[] values, int value) - { - int[] nvalues = new int[values.length+1]; - System.arraycopy(values, 0, nvalues, 0, values.length); - nvalues[values.length] = value; - return nvalues; - } - /** * Returns an array of the indexes in the given array of values that * have the maximum value in the array, or a zero-length array if the