applied patch #27778, also added an indexOf(char[]..).
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137848 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7eab1e6a6c
commit
813952e3f4
|
@ -44,7 +44,7 @@ import org.apache.commons.lang.builder.ToStringStyle;
|
|||
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
|
||||
* @author Maarten Coene
|
||||
* @since 2.0
|
||||
* @version $Id: ArrayUtils.java,v 1.43 2004/03/16 01:40:57 ggregory Exp $
|
||||
* @version $Id: ArrayUtils.java,v 1.44 2004/06/06 03:53:23 bayard Exp $
|
||||
*/
|
||||
public class ArrayUtils {
|
||||
|
||||
|
@ -1626,6 +1626,109 @@ public class ArrayUtils {
|
|||
return (indexOf(array, valueToFind) != -1);
|
||||
}
|
||||
|
||||
// char IndexOf
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Find the index of the given value in the array.</p>
|
||||
*
|
||||
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
|
||||
*
|
||||
* @param array the array to search through for the object, may be <code>null</code>
|
||||
* @param valueToFind the value to find
|
||||
* @return the index of the value within the array,
|
||||
* <code>-1</code> if not found or <code>null</code> array input
|
||||
*/
|
||||
public static int indexOf(final char[] array, final char valueToFind) {
|
||||
return indexOf(array, valueToFind, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Find the index of the given value in the array starting at the given index.</p>
|
||||
*
|
||||
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
|
||||
*
|
||||
* <p>A negative startIndex is treated as zero. A startIndex larger than the array
|
||||
* length will return -1.</p>
|
||||
*
|
||||
* @param array the array to search through for the object, may be <code>null</code>
|
||||
* @param valueToFind the value to find
|
||||
* @param startIndex the index to start searching at
|
||||
* @return the index of the value within the array,
|
||||
* <code>-1</code> if not found or <code>null</code> array input
|
||||
*/
|
||||
public static int indexOf(final char[] array, final char valueToFind, int startIndex) {
|
||||
if (array == null) {
|
||||
return -1;
|
||||
}
|
||||
if (startIndex < 0) {
|
||||
startIndex = 0;
|
||||
}
|
||||
for (int i = startIndex; i < array.length; i++) {
|
||||
if (valueToFind == array[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Find the last index of the given value within the array.</p>
|
||||
*
|
||||
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
|
||||
*
|
||||
* @param array the array to travers backwords looking for the object, may be <code>null</code>
|
||||
* @param valueToFind the object to find
|
||||
* @return the last index of the value within the array,
|
||||
* <code>-1</code> if not found or <code>null</code> array input
|
||||
*/
|
||||
public static int lastIndexOf(final char[] array, final char valueToFind) {
|
||||
return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Find the last index of the given value in the array starting at the given index.</p>
|
||||
*
|
||||
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
|
||||
*
|
||||
* <p>A negative startIndex will return -1. A startIndex larger than the array
|
||||
* length will search from the end of the array.</p>
|
||||
*
|
||||
* @param array the array to traverse for looking for the object, may be <code>null</code>
|
||||
* @param valueToFind the value to find
|
||||
* @param startIndex the start index to travers backwards from
|
||||
* @return the last index of the value within the array,
|
||||
* <code>-1</code> if not found or <code>null</code> array input
|
||||
*/
|
||||
public static int lastIndexOf(final char[] array, final char valueToFind, int startIndex) {
|
||||
if (array == null) {
|
||||
return -1;
|
||||
}
|
||||
if (startIndex < 0) {
|
||||
return -1;
|
||||
} else if (startIndex >= array.length) {
|
||||
startIndex = array.length - 1;
|
||||
}
|
||||
for (int i = startIndex; i >= 0; i--) {
|
||||
if (valueToFind == array[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if the value is in the given array.</p>
|
||||
*
|
||||
* <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
|
||||
*
|
||||
* @param array the array to search through
|
||||
* @param valueToFind the value to find
|
||||
* @return <code>true</code> if the array contains the object
|
||||
*/
|
||||
public static boolean contains(final char[] array, final char valueToFind) {
|
||||
return (indexOf(array, valueToFind) != -1);
|
||||
}
|
||||
|
||||
// byte IndexOf
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
|
@ -3115,4 +3218,625 @@ public class ArrayUtils {
|
|||
return (Object[]) result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove(["a"], 0) = []
|
||||
* ArrayUtils.remove(["a", "b"], 0) = ["b"]
|
||||
* ArrayUtils.remove(["a", "b"], 1) = ["a"]
|
||||
* ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static Object[] remove(final Object[] array, final int index) {
|
||||
return (Object[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, "a") = null
|
||||
* ArrayUtils.removeElement([], "a") = []
|
||||
* ArrayUtils.removeElement(["a"], "b") = ["a"]
|
||||
* ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
|
||||
* ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static Object[] removeElement(final Object[] array, final Object element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove([true], 0) = []
|
||||
* ArrayUtils.remove([true, false], 0) = [false]
|
||||
* ArrayUtils.remove([true, false], 1) = [true]
|
||||
* ArrayUtils.remove([true, true, false], 1) = [true, false]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static boolean[] remove(final boolean[] array, final int index) {
|
||||
return (boolean[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, true) = null
|
||||
* ArrayUtils.removeElement([], true) = []
|
||||
* ArrayUtils.removeElement([true], false) = [true]
|
||||
* ArrayUtils.removeElement([true, false], false) = [true]
|
||||
* ArrayUtils.removeElement([true, false, true], true) = [false, true]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static boolean[] removeElement(final boolean[] array, final boolean element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove([1], 0) = []
|
||||
* ArrayUtils.remove([1, 0], 0) = [0]
|
||||
* ArrayUtils.remove([1, 0], 1) = [1]
|
||||
* ArrayUtils.remove([1, 0, 1], 1) = [1, 1]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static byte[] remove(final byte[] array, final int index) {
|
||||
return (byte[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, 1) = null
|
||||
* ArrayUtils.removeElement([], 1) = []
|
||||
* ArrayUtils.removeElement([1], 0) = [1]
|
||||
* ArrayUtils.removeElement([1, 0], 0) = [1]
|
||||
* ArrayUtils.removeElement([1, 0, 1], 1) = [0, 1]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static byte[] removeElement(final byte[] array, final byte element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove(['a'], 0) = []
|
||||
* ArrayUtils.remove(['a', 'b'], 0) = ['b']
|
||||
* ArrayUtils.remove(['a', 'b'], 1) = ['a']
|
||||
* ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c']
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static char[] remove(final char[] array, final int index) {
|
||||
return (char[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, 'a') = null
|
||||
* ArrayUtils.removeElement([], 'a') = []
|
||||
* ArrayUtils.removeElement(['a'], 'b') = ['a']
|
||||
* ArrayUtils.removeElement(['a', 'b'], 'a') = ['b']
|
||||
* ArrayUtils.removeElement(['a', 'b', 'a'], 'a') = ['b', 'a']
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static char[] removeElement(final char[] array, final char element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove([1.1], 0) = []
|
||||
* ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
|
||||
* ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
|
||||
* ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static double[] remove(final double[] array, final int index) {
|
||||
return (double[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, 1.1) = null
|
||||
* ArrayUtils.removeElement([], 1.1) = []
|
||||
* ArrayUtils.removeElement([1.1], 1.2) = [1.1]
|
||||
* ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
|
||||
* ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static double[] removeElement(final double[] array, final double element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove([1.1], 0) = []
|
||||
* ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
|
||||
* ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
|
||||
* ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static float[] remove(final float[] array, final int index) {
|
||||
return (float[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, 1.1) = null
|
||||
* ArrayUtils.removeElement([], 1.1) = []
|
||||
* ArrayUtils.removeElement([1.1], 1.2) = [1.1]
|
||||
* ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
|
||||
* ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static float[] removeElement(final float[] array, final float element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove([1], 0) = []
|
||||
* ArrayUtils.remove([2, 6], 0) = [6]
|
||||
* ArrayUtils.remove([2, 6], 1) = [2]
|
||||
* ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static int[] remove(final int[] array, final int index) {
|
||||
return (int[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, 1) = null
|
||||
* ArrayUtils.removeElement([], 1) = []
|
||||
* ArrayUtils.removeElement([1], 2) = [1]
|
||||
* ArrayUtils.removeElement([1, 3], 1) = [3]
|
||||
* ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static int[] removeElement(final int[] array, final int element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove([1], 0) = []
|
||||
* ArrayUtils.remove([2, 6], 0) = [6]
|
||||
* ArrayUtils.remove([2, 6], 1) = [2]
|
||||
* ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static long[] remove(final long[] array, final int index) {
|
||||
return (long[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, 1) = null
|
||||
* ArrayUtils.removeElement([], 1) = []
|
||||
* ArrayUtils.removeElement([1], 2) = [1]
|
||||
* ArrayUtils.removeElement([1, 3], 1) = [3]
|
||||
* ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static long[] removeElement(final long[] array, final long element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.remove([1], 0) = []
|
||||
* ArrayUtils.remove([2, 6], 0) = [6]
|
||||
* ArrayUtils.remove([2, 6], 1) = [2]
|
||||
* ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static short[] remove(final short[] array, final int index) {
|
||||
return (short[]) remove((Object) array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the first occurrence of the specified element from the
|
||||
* specified array. All subsequent elements are shifted to the left
|
||||
* (substracts one from their indices). If the array doesn't contains
|
||||
* such an element, no elements are removed from the array.</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the first occurrence of the specified element. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.removeElement(null, 1) = null
|
||||
* ArrayUtils.removeElement([], 1) = []
|
||||
* ArrayUtils.removeElement([1], 2) = [1]
|
||||
* ArrayUtils.removeElement([1, 3], 1) = [3]
|
||||
* ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to remove the element from, may be <code>null</code>
|
||||
* @param element the element to be removed
|
||||
* @return A new array containing the existing elements except the first
|
||||
* occurrence of the specified element.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static short[] removeElement(final short[] array, final short element) {
|
||||
int index = indexOf(array, element);
|
||||
if (index == -1) {
|
||||
return clone(array);
|
||||
}
|
||||
return remove(array, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Removes the element at the specified position from the specified array.
|
||||
* All subsequent elements are shifted to the left (substracts one from
|
||||
* their indices).</p>
|
||||
*
|
||||
* <p>This method returns a new array with the same elements of the input
|
||||
* array except the element on the specified position. The component
|
||||
* type of the returned array is always the same as that of the input
|
||||
* array.</p>
|
||||
*
|
||||
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
|
||||
* will be thrown, because in that case no valid index can be specified.</p>
|
||||
*
|
||||
* @param array the array to remove the element from, may not be <code>null</code>
|
||||
* @param index the position of the element to be removed
|
||||
* @return A new array containing the existing elements except the element
|
||||
* at the specified position.
|
||||
* @throws IndexOutOfBoundsException if the index is out of range
|
||||
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
|
||||
* @since 2.1
|
||||
*/
|
||||
private static Object remove(final Object array, final int index) {
|
||||
int length = getLength(array);
|
||||
if (index < 0 || index >= length) {
|
||||
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
|
||||
}
|
||||
|
||||
Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
|
||||
System.arraycopy(array, 0, result, 0, index);
|
||||
if (index < length - 1) {
|
||||
System.arraycopy(array, index + 1, result, index, length - index - 1);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,457 @@
|
|||
/*
|
||||
* Copyright 2002-2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.lang;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import junit.textui.TestRunner;
|
||||
|
||||
/**
|
||||
* Tests ArrayUtils remove and removeElement methods.
|
||||
*
|
||||
* @author Maarten Coene
|
||||
* @version $Id: ArrayUtilsRemoveTest.java,v 1.1 2004/06/06 03:53:24 bayard Exp $
|
||||
*/
|
||||
public class ArrayUtilsRemoveTest extends TestCase {
|
||||
public static void main(String[] args) {
|
||||
TestRunner.run(suite());
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(ArrayUtilsRemoveTest.class);
|
||||
suite.setName("ArrayUtils remove Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
public void testRemoveObjectArray() {
|
||||
Object[] array;
|
||||
array = ArrayUtils.remove(new Object[] {"a"}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_OBJECT_ARRAY, array));
|
||||
assertEquals(Object.class, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new Object[] {"a", "b"}, 0);
|
||||
assertTrue(Arrays.equals(new Object[] {"b"}, array));
|
||||
assertEquals(Object.class, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new Object[] {"a", "b"}, 1);
|
||||
assertTrue(Arrays.equals(new Object[] {"a"}, array));
|
||||
assertEquals(Object.class, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new Object[] {"a", "b", "c"}, 1);
|
||||
assertTrue(Arrays.equals(new Object[] {"a", "c"}, array));
|
||||
assertEquals(Object.class, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new Object[] {"a", "b"}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new Object[] {"a", "b"}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((Object[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveBooleanArray() {
|
||||
boolean[] array;
|
||||
array = ArrayUtils.remove(new boolean[] {true}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, array));
|
||||
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new boolean[] {true, false}, 0);
|
||||
assertTrue(Arrays.equals(new boolean[] {false}, array));
|
||||
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new boolean[] {true, false}, 1);
|
||||
assertTrue(Arrays.equals(new boolean[] {true}, array));
|
||||
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new boolean[] {true, false, true}, 1);
|
||||
assertTrue(Arrays.equals(new boolean[] {true, true}, array));
|
||||
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new boolean[] {true, false}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new boolean[] {true, false}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((boolean[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveByteArray() {
|
||||
byte[] array;
|
||||
array = ArrayUtils.remove(new byte[] {1}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY, array));
|
||||
assertEquals(Byte.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new byte[] {1, 2}, 0);
|
||||
assertTrue(Arrays.equals(new byte[] {2}, array));
|
||||
assertEquals(Byte.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new byte[] {1, 2}, 1);
|
||||
assertTrue(Arrays.equals(new byte[] {1}, array));
|
||||
assertEquals(Byte.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new byte[] {1, 2, 1}, 1);
|
||||
assertTrue(Arrays.equals(new byte[] {1, 1}, array));
|
||||
assertEquals(Byte.TYPE, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new byte[] {1, 2}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new byte[] {1, 2}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((byte[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveCharArray() {
|
||||
char[] array;
|
||||
array = ArrayUtils.remove(new char[] {'a'}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY, array));
|
||||
assertEquals(Character.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new char[] {'a', 'b'}, 0);
|
||||
assertTrue(Arrays.equals(new char[] {'b'}, array));
|
||||
assertEquals(Character.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new char[] {'a', 'b'}, 1);
|
||||
assertTrue(Arrays.equals(new char[] {'a'}, array));
|
||||
assertEquals(Character.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new char[] {'a', 'b', 'c'}, 1);
|
||||
assertTrue(Arrays.equals(new char[] {'a', 'c'}, array));
|
||||
assertEquals(Character.TYPE, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new char[] {'a', 'b'}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new char[] {'a', 'b'}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((char[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveDoubleArray() {
|
||||
double[] array;
|
||||
array = ArrayUtils.remove(new double[] {1}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY, array));
|
||||
assertEquals(Double.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new double[] {1, 2}, 0);
|
||||
assertTrue(Arrays.equals(new double[] {2}, array));
|
||||
assertEquals(Double.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new double[] {1, 2}, 1);
|
||||
assertTrue(Arrays.equals(new double[] {1}, array));
|
||||
assertEquals(Double.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new double[] {1, 2, 1}, 1);
|
||||
assertTrue(Arrays.equals(new double[] {1, 1}, array));
|
||||
assertEquals(Double.TYPE, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new double[] {1, 2}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new double[] {1, 2}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((double[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveFloatArray() {
|
||||
float[] array;
|
||||
array = ArrayUtils.remove(new float[] {1}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY, array));
|
||||
assertEquals(Float.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new float[] {1, 2}, 0);
|
||||
assertTrue(Arrays.equals(new float[] {2}, array));
|
||||
assertEquals(Float.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new float[] {1, 2}, 1);
|
||||
assertTrue(Arrays.equals(new float[] {1}, array));
|
||||
assertEquals(Float.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new float[] {1, 2, 1}, 1);
|
||||
assertTrue(Arrays.equals(new float[] {1, 1}, array));
|
||||
assertEquals(Float.TYPE, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new float[] {1, 2}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new float[] {1, 2}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((float[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveIntArray() {
|
||||
int[] array;
|
||||
array = ArrayUtils.remove(new int[] {1}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY, array));
|
||||
assertEquals(Integer.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new int[] {1, 2}, 0);
|
||||
assertTrue(Arrays.equals(new int[] {2}, array));
|
||||
assertEquals(Integer.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new int[] {1, 2}, 1);
|
||||
assertTrue(Arrays.equals(new int[] {1}, array));
|
||||
assertEquals(Integer.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new int[] {1, 2, 1}, 1);
|
||||
assertTrue(Arrays.equals(new int[] {1, 1}, array));
|
||||
assertEquals(Integer.TYPE, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new int[] {1, 2}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new int[] {1, 2}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((int[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveLongArray() {
|
||||
long[] array;
|
||||
array = ArrayUtils.remove(new long[] {1}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY, array));
|
||||
assertEquals(Long.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new long[] {1, 2}, 0);
|
||||
assertTrue(Arrays.equals(new long[] {2}, array));
|
||||
assertEquals(Long.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new long[] {1, 2}, 1);
|
||||
assertTrue(Arrays.equals(new long[] {1}, array));
|
||||
assertEquals(Long.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new long[] {1, 2, 1}, 1);
|
||||
assertTrue(Arrays.equals(new long[] {1, 1}, array));
|
||||
assertEquals(Long.TYPE, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new long[] {1, 2}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new long[] {1, 2}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((long[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveShortArray() {
|
||||
short[] array;
|
||||
array = ArrayUtils.remove(new short[] {1}, 0);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY, array));
|
||||
assertEquals(Short.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new short[] {1, 2}, 0);
|
||||
assertTrue(Arrays.equals(new short[] {2}, array));
|
||||
assertEquals(Short.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new short[] {1, 2}, 1);
|
||||
assertTrue(Arrays.equals(new short[] {1}, array));
|
||||
assertEquals(Short.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.remove(new short[] {1, 2, 1}, 1);
|
||||
assertTrue(Arrays.equals(new short[] {1, 1}, array));
|
||||
assertEquals(Short.TYPE, array.getClass().getComponentType());
|
||||
try {
|
||||
ArrayUtils.remove(new short[] {1, 2}, -1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove(new short[] {1, 2}, 2);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
try {
|
||||
ArrayUtils.remove((short[]) null, 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
public void testRemoveElementObjectArray() {
|
||||
Object[] array;
|
||||
array = ArrayUtils.removeElement((Object[]) null, "a");
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_OBJECT_ARRAY, "a");
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_OBJECT_ARRAY, array));
|
||||
assertEquals(Object.class, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new Object[] {"a"}, "a");
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_OBJECT_ARRAY, array));
|
||||
assertEquals(Object.class, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new Object[] {"a", "b"}, "a");
|
||||
assertTrue(Arrays.equals(new Object[] {"b"}, array));
|
||||
assertEquals(Object.class, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new Object[] {"a", "b", "a"}, "a");
|
||||
assertTrue(Arrays.equals(new Object[] {"b", "a"}, array));
|
||||
assertEquals(Object.class, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
public void testRemoveElementBooleanArray() {
|
||||
boolean[] array;
|
||||
array = ArrayUtils.removeElement((boolean[]) null, true);
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_BOOLEAN_ARRAY, true);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, array));
|
||||
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new boolean[] {true}, true);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, array));
|
||||
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new boolean[] {true, false}, true);
|
||||
assertTrue(Arrays.equals(new boolean[] {false}, array));
|
||||
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new boolean[] {true, false, true}, true);
|
||||
assertTrue(Arrays.equals(new boolean[] {false, true}, array));
|
||||
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
public void testRemoveElementByteArray() {
|
||||
byte[] array;
|
||||
array = ArrayUtils.removeElement((byte[]) null, (byte) 1);
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_BYTE_ARRAY, (byte) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY, array));
|
||||
assertEquals(Byte.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new byte[] {1}, (byte) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY, array));
|
||||
assertEquals(Byte.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new byte[] {1, 2}, (byte) 1);
|
||||
assertTrue(Arrays.equals(new byte[] {2}, array));
|
||||
assertEquals(Byte.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new byte[] {1, 2, 1}, (byte) 1);
|
||||
assertTrue(Arrays.equals(new byte[] {2, 1}, array));
|
||||
assertEquals(Byte.TYPE, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
public void testRemoveElementCharArray() {
|
||||
char[] array;
|
||||
array = ArrayUtils.removeElement((char[]) null, 'a');
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_CHAR_ARRAY, 'a');
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY, array));
|
||||
assertEquals(Character.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new char[] {'a'}, 'a');
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY, array));
|
||||
assertEquals(Character.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new char[] {'a', 'b'}, 'a');
|
||||
assertTrue(Arrays.equals(new char[] {'b'}, array));
|
||||
assertEquals(Character.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new char[] {'a', 'b', 'a'}, 'a');
|
||||
assertTrue(Arrays.equals(new char[] {'b', 'a'}, array));
|
||||
assertEquals(Character.TYPE, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
public void testRemoveElementDoubleArray() {
|
||||
double[] array;
|
||||
array = ArrayUtils.removeElement((double[]) null, (double) 1);
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_DOUBLE_ARRAY, (double) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY, array));
|
||||
assertEquals(Double.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new double[] {1}, (double) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY, array));
|
||||
assertEquals(Double.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new double[] {1, 2}, (double) 1);
|
||||
assertTrue(Arrays.equals(new double[] {2}, array));
|
||||
assertEquals(Double.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new double[] {1, 2, 1}, (double) 1);
|
||||
assertTrue(Arrays.equals(new double[] {2, 1}, array));
|
||||
assertEquals(Double.TYPE, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
public void testRemoveElementFloatArray() {
|
||||
float[] array;
|
||||
array = ArrayUtils.removeElement((float[]) null, (float) 1);
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_FLOAT_ARRAY, (float) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY, array));
|
||||
assertEquals(Float.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new float[] {1}, (float) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY, array));
|
||||
assertEquals(Float.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new float[] {1, 2}, (float) 1);
|
||||
assertTrue(Arrays.equals(new float[] {2}, array));
|
||||
assertEquals(Float.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new float[] {1, 2, 1}, (float) 1);
|
||||
assertTrue(Arrays.equals(new float[] {2, 1}, array));
|
||||
assertEquals(Float.TYPE, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
public void testRemoveElementIntArray() {
|
||||
int[] array;
|
||||
array = ArrayUtils.removeElement((int[]) null, 1);
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_INT_ARRAY, 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY, array));
|
||||
assertEquals(Integer.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new int[] {1}, 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY, array));
|
||||
assertEquals(Integer.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new int[] {1, 2}, 1);
|
||||
assertTrue(Arrays.equals(new int[] {2}, array));
|
||||
assertEquals(Integer.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new int[] {1, 2, 1}, 1);
|
||||
assertTrue(Arrays.equals(new int[] {2, 1}, array));
|
||||
assertEquals(Integer.TYPE, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
public void testRemoveElementLongArray() {
|
||||
long[] array;
|
||||
array = ArrayUtils.removeElement((long[]) null, (long) 1);
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_LONG_ARRAY, (long) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY, array));
|
||||
assertEquals(Long.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new long[] {1}, (long) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY, array));
|
||||
assertEquals(Long.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new long[] {1, 2}, (long) 1);
|
||||
assertTrue(Arrays.equals(new long[] {2}, array));
|
||||
assertEquals(Long.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new long[] {1, 2, 1}, (long) 1);
|
||||
assertTrue(Arrays.equals(new long[] {2, 1}, array));
|
||||
assertEquals(Long.TYPE, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
public void testRemoveElementShortArray() {
|
||||
short[] array;
|
||||
array = ArrayUtils.removeElement((short[]) null, (short) 1);
|
||||
assertNull(array);
|
||||
array = ArrayUtils.removeElement(ArrayUtils.EMPTY_SHORT_ARRAY, (short) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY, array));
|
||||
assertEquals(Short.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new short[] {1}, (short) 1);
|
||||
assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY, array));
|
||||
assertEquals(Short.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new short[] {1, 2}, (short) 1);
|
||||
assertTrue(Arrays.equals(new short[] {2}, array));
|
||||
assertEquals(Short.TYPE, array.getClass().getComponentType());
|
||||
array = ArrayUtils.removeElement(new short[] {1, 2, 1}, (short) 1);
|
||||
assertTrue(Arrays.equals(new short[] {2, 1}, array));
|
||||
assertEquals(Short.TYPE, array.getClass().getComponentType());
|
||||
}
|
||||
|
||||
}
|
|
@ -38,7 +38,7 @@ import junit.textui.TestRunner;
|
|||
* @author Fredrik Westermarck
|
||||
* @author Gary Gregory
|
||||
* @author Maarten Coene
|
||||
* @version $Id: ArrayUtilsTest.java,v 1.26 2004/02/18 23:06:19 ggregory Exp $
|
||||
* @version $Id: ArrayUtilsTest.java,v 1.27 2004/06/06 03:53:24 bayard Exp $
|
||||
*/
|
||||
public class ArrayUtilsTest extends TestCase {
|
||||
|
||||
|
@ -1542,6 +1542,66 @@ public class ArrayUtilsTest extends TestCase {
|
|||
assertEquals(false, ArrayUtils.contains(array, (short) 99));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testIndexOfChar() {
|
||||
char[] array = null;
|
||||
assertEquals(-1, ArrayUtils.indexOf(array, 'a'));
|
||||
array = new char[] { 'a', 'b', 'c', 'd', 'a' };
|
||||
assertEquals(0, ArrayUtils.indexOf(array, 'a'));
|
||||
assertEquals(1, ArrayUtils.indexOf(array, 'b'));
|
||||
assertEquals(2, ArrayUtils.indexOf(array, 'c'));
|
||||
assertEquals(3, ArrayUtils.indexOf(array, 'd'));
|
||||
assertEquals(-1, ArrayUtils.indexOf(array, 'e'));
|
||||
}
|
||||
|
||||
public void testIndexOfCharWithStartIndex() {
|
||||
char[] array = null;
|
||||
assertEquals(-1, ArrayUtils.indexOf(array, 'a', 2));
|
||||
array = new char[] { 'a', 'b', 'c', 'd', 'a' };
|
||||
assertEquals(4, ArrayUtils.indexOf(array, 'a', 2));
|
||||
assertEquals(-1, ArrayUtils.indexOf(array, 'b', 2));
|
||||
assertEquals(2, ArrayUtils.indexOf(array, 'c', 2));
|
||||
assertEquals(3, ArrayUtils.indexOf(array, 'd', 2));
|
||||
assertEquals(3, ArrayUtils.indexOf(array, 'd', -1));
|
||||
assertEquals(-1, ArrayUtils.indexOf(array, 'e', 0));
|
||||
assertEquals(-1, ArrayUtils.indexOf(array, 'a', 6));
|
||||
}
|
||||
|
||||
public void testLastIndexOfChar() {
|
||||
char[] array = null;
|
||||
assertEquals(-1, ArrayUtils.lastIndexOf(array, 'a'));
|
||||
array = new char[] { 'a', 'b', 'c', 'd', 'a' };
|
||||
assertEquals(4, ArrayUtils.lastIndexOf(array, 'a'));
|
||||
assertEquals(1, ArrayUtils.lastIndexOf(array, 'b'));
|
||||
assertEquals(2, ArrayUtils.lastIndexOf(array, 'c'));
|
||||
assertEquals(3, ArrayUtils.lastIndexOf(array, 'd'));
|
||||
assertEquals(-1, ArrayUtils.lastIndexOf(array, 'e'));
|
||||
}
|
||||
|
||||
public void testLastIndexOfCharWithStartIndex() {
|
||||
char[] array = null;
|
||||
assertEquals(-1, ArrayUtils.lastIndexOf(array, 'a', 2));
|
||||
array = new char[] { 'a', 'b', 'c', 'd', 'a' };
|
||||
assertEquals(0, ArrayUtils.lastIndexOf(array, 'a', 2));
|
||||
assertEquals(1, ArrayUtils.lastIndexOf(array, 'b', 2));
|
||||
assertEquals(2, ArrayUtils.lastIndexOf(array, 'c', 2));
|
||||
assertEquals(-1, ArrayUtils.lastIndexOf(array, 'd', 2));
|
||||
assertEquals(-1, ArrayUtils.lastIndexOf(array, 'd', -1));
|
||||
assertEquals(-1, ArrayUtils.lastIndexOf(array, 'e'));
|
||||
assertEquals(4, ArrayUtils.lastIndexOf(array, 'a', 88));
|
||||
}
|
||||
|
||||
public void testContainsChar() {
|
||||
char[] array = null;
|
||||
assertEquals(false, ArrayUtils.contains(array, 'b'));
|
||||
array = new char[] { 'a', 'b', 'c', 'd', 'a' };
|
||||
assertEquals(true, ArrayUtils.contains(array, 'a'));
|
||||
assertEquals(true, ArrayUtils.contains(array, 'b'));
|
||||
assertEquals(true, ArrayUtils.contains(array, 'c'));
|
||||
assertEquals(true, ArrayUtils.contains(array, 'd'));
|
||||
assertEquals(false, ArrayUtils.contains(array, 'e'));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testIndexOfByte() {
|
||||
byte[] array = null;
|
||||
|
|
Loading…
Reference in New Issue