diff --git a/src/java/org/apache/commons/lang/ArrayUtils.java b/src/java/org/apache/commons/lang/ArrayUtils.java index d0554a794..55f344042 100644 --- a/src/java/org/apache/commons/lang/ArrayUtils.java +++ b/src/java/org/apache/commons/lang/ArrayUtils.java @@ -44,7 +44,7 @@ import org.apache.commons.lang.builder.ToStringStyle; * @author Ashwin S * @author Maarten Coene * @since 2.0 - * @version $Id: ArrayUtils.java,v 1.47 2004/10/08 00:11:00 scolebourne Exp $ + * @version $Id: ArrayUtils.java,v 1.48 2004/10/09 11:55:51 scolebourne Exp $ */ public class ArrayUtils { @@ -2943,9 +2943,10 @@ public class ArrayUtils { * @since 2.1 */ public static Object[] add(Object[] array, Object element) { - Object newArray = copyArrayGrow1(array, element != null ? element.getClass() : Object.class); - Array.set(newArray, lastIndex(newArray), element); - return (Object[]) newArray; + Class type = (array != null ? array.getClass() : (element != null ? element.getClass() : Object.class)); + Object[] newArray = (Object[]) copyArrayGrow1(array, type); + newArray[newArray.length - 1] = element; + return newArray; } /** @@ -2971,7 +2972,7 @@ public class ArrayUtils { */ public static boolean[] add(boolean[] array, boolean element) { boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE); - newArray[lastIndex(newArray)] = element; + newArray[newArray.length - 1] = element; return newArray; } @@ -2998,7 +2999,7 @@ public class ArrayUtils { */ public static byte[] add(byte[] array, byte element) { byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE); - newArray[lastIndex(newArray)] = element; + newArray[newArray.length - 1] = element; return newArray; } @@ -3025,7 +3026,7 @@ public class ArrayUtils { */ public static char[] add(char[] array, char element) { char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE); - newArray[lastIndex(newArray)] = element; + newArray[newArray.length - 1] = element; return newArray; } @@ -3052,7 +3053,7 @@ public class ArrayUtils { */ public static double[] add(double[] array, double element) { double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE); - newArray[lastIndex(newArray)] = element; + newArray[newArray.length - 1] = element; return newArray; } @@ -3079,7 +3080,7 @@ public class ArrayUtils { */ public static float[] add(float[] array, float element) { float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE); - newArray[lastIndex(newArray)] = element; + newArray[newArray.length - 1] = element; return newArray; } @@ -3106,7 +3107,7 @@ public class ArrayUtils { */ public static int[] add(int[] array, int element) { int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE); - newArray[lastIndex(newArray)] = element; + newArray[newArray.length - 1] = element; return newArray; } @@ -3133,7 +3134,7 @@ public class ArrayUtils { */ public static long[] add(long[] array, long element) { long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE); - newArray[lastIndex(newArray)] = element; + newArray[newArray.length - 1] = element; return newArray; } @@ -3160,7 +3161,7 @@ public class ArrayUtils { */ public static short[] add(short[] array, short element) { short[] newArray = (short[])copyArrayGrow1(array, Short.TYPE); - newArray[lastIndex(newArray)] = element; + newArray[newArray.length - 1] = element; return newArray; } diff --git a/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java b/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java index 9d1ca8a56..8785c6a68 100644 --- a/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java +++ b/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java @@ -27,7 +27,7 @@ import junit.textui.TestRunner; * Tests ArrayUtils add methods. * * @author Gary D. Gregory - * @version $Id: ArrayUtilsAddTest.java,v 1.3 2004/03/16 01:40:57 ggregory Exp $ + * @version $Id: ArrayUtilsAddTest.java,v 1.4 2004/10/09 11:55:51 scolebourne Exp $ */ public class ArrayUtilsAddTest extends TestCase { public static void main(String[] args) { @@ -178,17 +178,35 @@ public class ArrayUtilsAddTest extends TestCase { newArray = ArrayUtils.add((Object[])null, null); assertTrue(Arrays.equals((new Object[]{null}), newArray)); assertEquals(Object.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((Object[])null, "a"); assertTrue(Arrays.equals((new String[]{"a"}), newArray)); assertTrue(Arrays.equals((new Object[]{"a"}), newArray)); assertEquals(String.class, newArray.getClass().getComponentType()); + String[] stringArray1 = new String[]{"a", "b", "c"}; newArray = ArrayUtils.add(stringArray1, null); assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray)); assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(stringArray1, "d"); assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray)); assertEquals(String.class, newArray.getClass().getComponentType()); + + Number[] numberArray1 = new Number[]{new Integer(1), new Double(2)}; + newArray = ArrayUtils.add(numberArray1, new Float(3)); + assertTrue(Arrays.equals((new Number[]{new Integer(1), new Double(2), new Float(3)}), newArray)); + assertEquals(Number.class, newArray.getClass().getComponentType()); + + numberArray1 = null; + newArray = ArrayUtils.add(numberArray1, new Float(3)); + assertTrue(Arrays.equals((new Float[]{new Float(3)}), newArray)); + assertEquals(Float.class, newArray.getClass().getComponentType()); + + numberArray1 = null; + newArray = ArrayUtils.add(numberArray1, null); + assertTrue(Arrays.equals((new Object[]{null}), newArray)); + assertEquals(Object.class, newArray.getClass().getComponentType()); } public void testAddObjectArrayToObjectArray() {