You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/10/29 14:38:37 UTC
[commons-lang] branch master updated: Sort members.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 89d2edb Sort members.
89d2edb is described below
commit 89d2edbf1726b8cd512076ded14b6d05f0840d23
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Oct 29 10:38:34 2019 -0400
Sort members.
---
.../java/org/apache/commons/lang3/ArrayUtils.java | 13590 +++++++++----------
1 file changed, 6795 insertions(+), 6795 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
index 7569bf7..a35dde9 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -46,99 +46,99 @@ import org.apache.commons.lang3.mutable.MutableInt;
public class ArrayUtils {
/**
- * An empty immutable {@code Object} array.
+ * An empty immutable {@code boolean} array.
*/
- public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
+ public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
/**
- * An empty immutable {@code Class} array.
+ * An empty immutable {@code Boolean} array.
*/
- public static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0];
+ public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
/**
- * An empty immutable {@code String} array.
+ * An empty immutable {@code byte} array.
*/
- public static final String[] EMPTY_STRING_ARRAY = new String[0];
+ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
/**
- * An empty immutable {@code long} array.
+ * An empty immutable {@code Byte} array.
*/
- public static final long[] EMPTY_LONG_ARRAY = new long[0];
+ public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
/**
- * An empty immutable {@code Long} array.
+ * An empty immutable {@code char} array.
*/
- public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
+ public static final char[] EMPTY_CHAR_ARRAY = new char[0];
/**
- * An empty immutable {@code int} array.
+ * An empty immutable {@code Character} array.
*/
- public static final int[] EMPTY_INT_ARRAY = new int[0];
+ public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
/**
- * An empty immutable {@code Integer} array.
+ * An empty immutable {@code Class} array.
*/
- public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
+ public static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0];
/**
- * An empty immutable {@code short} array.
+ * An empty immutable {@code double} array.
*/
- public static final short[] EMPTY_SHORT_ARRAY = new short[0];
+ public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
/**
- * An empty immutable {@code Short} array.
+ * An empty immutable {@code Double} array.
*/
- public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
+ public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
/**
- * An empty immutable {@code byte} array.
+ * An empty immutable {@code float} array.
*/
- public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+ public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
/**
- * An empty immutable {@code Byte} array.
+ * An empty immutable {@code Float} array.
*/
- public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
+ public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
/**
- * An empty immutable {@code double} array.
+ * An empty immutable {@code int} array.
*/
- public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
+ public static final int[] EMPTY_INT_ARRAY = new int[0];
/**
- * An empty immutable {@code Double} array.
+ * An empty immutable {@code Integer} array.
*/
- public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
+ public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
/**
- * An empty immutable {@code float} array.
+ * An empty immutable {@code long} array.
*/
- public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
+ public static final long[] EMPTY_LONG_ARRAY = new long[0];
/**
- * An empty immutable {@code Float} array.
+ * An empty immutable {@code Long} array.
*/
- public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
+ public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
/**
- * An empty immutable {@code boolean} array.
+ * An empty immutable {@code Object} array.
*/
- public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
+ public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
/**
- * An empty immutable {@code Boolean} array.
+ * An empty immutable {@code short} array.
*/
- public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
+ public static final short[] EMPTY_SHORT_ARRAY = new short[0];
/**
- * An empty immutable {@code char} array.
+ * An empty immutable {@code Short} array.
*/
- public static final char[] EMPTY_CHAR_ARRAY = new char[0];
+ public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
/**
- * An empty immutable {@code Character} array.
+ * An empty immutable {@code String} array.
*/
- public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
+ public static final String[] EMPTY_STRING_ARRAY = new String[0];
/**
* The index value when an element is not found in a list or array: {@code -1}.
@@ -148,3959 +148,3540 @@ public class ArrayUtils {
public static final int INDEX_NOT_FOUND = -1;
/**
- * <p>ArrayUtils instances should NOT be constructed in standard programming.
- * Instead, the class should be used as <code>ArrayUtils.clone(new int[] {2})</code>.
+ * <p>Copies the given array and adds the given element at the end of the new array.
*
- * <p>This constructor is public to permit tools that require a JavaBean instance
- * to operate.
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
+ *
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
+ *
+ * <pre>
+ * ArrayUtils.add(null, true) = [true]
+ * ArrayUtils.add([true], false) = [true, false]
+ * ArrayUtils.add([true, false], true) = [true, false, true]
+ * </pre>
+ *
+ * @param array the array to copy and add the element to, may be {@code null}
+ * @param element the object to add at the last index of the new array
+ * @return A new array containing the existing elements plus the new element
+ * @since 2.1
*/
- public ArrayUtils() {
- super();
+ public static boolean[] add(final boolean[] array, final boolean element) {
+ final boolean[] newArray = (boolean[]) copyArrayGrow1(array, Boolean.TYPE);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
// NOTE: Cannot use {@code} to enclose text which includes {}, but <code></code> is OK
- // Basic methods handling multi-dimensional arrays
- //-----------------------------------------------------------------------
/**
- * <p>Outputs an array as a String, treating {@code null} as an empty array.
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <p>Multi-dimensional arrays are handled correctly, including
- * multi-dimensional primitive arrays.
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * <p>The format is that of Java source code, for example <code>{a,b}</code>.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * @param array the array to get a toString for, may be {@code null}
- * @return a String representation of the array, '{}' if null array input
+ * <pre>
+ * ArrayUtils.add(null, 0, true) = [true]
+ * ArrayUtils.add([true], 0, false) = [false, true]
+ * ArrayUtils.add([false], 1, true) = [false, true]
+ * ArrayUtils.add([true, false], 1, true) = [true, true, false]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index > array.length).
+ * @deprecated this method has been superseded by {@link #insert(int, boolean[], boolean...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
- public static String toString(final Object array) {
- return toString(array, "{}");
+ @Deprecated
+ public static boolean[] add(final boolean[] array, final int index, final boolean element) {
+ return (boolean[]) add(array, index, Boolean.valueOf(element), Boolean.TYPE);
}
/**
- * <p>Outputs an array as a String handling {@code null}s.
+ * <p>Copies the given array and adds the given element at the end of the new array.
*
- * <p>Multi-dimensional arrays are handled correctly, including
- * multi-dimensional primitive arrays.
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
*
- * <p>The format is that of Java source code, for example <code>{a,b}</code>.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * @param array the array to get a toString for, may be {@code null}
- * @param stringIfNull the String to return if the array is {@code null}
- * @return a String representation of the array
+ * <pre>
+ * ArrayUtils.add(null, 0) = [0]
+ * ArrayUtils.add([1], 0) = [1, 0]
+ * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * </pre>
+ *
+ * @param array the array to copy and add the element to, may be {@code null}
+ * @param element the object to add at the last index of the new array
+ * @return A new array containing the existing elements plus the new element
+ * @since 2.1
*/
- public static String toString(final Object array, final String stringIfNull) {
- if (array == null) {
- return stringIfNull;
- }
- return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
+ public static byte[] add(final byte[] array, final byte element) {
+ final byte[] newArray = (byte[]) copyArrayGrow1(array, Byte.TYPE);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
/**
- * <p>Get a hash code for an array handling multi-dimensional arrays correctly.
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <p>Multi-dimensional primitive arrays are also handled correctly by this method.
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array the array to get a hash code for, {@code null} returns zero
- * @return a hash code for the array
- */
- public static int hashCode(final Object array) {
- return new HashCodeBuilder().append(array).toHashCode();
- }
-
- /**
- * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
- * correctly.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * <p>Multi-dimensional primitive arrays are also handled correctly by this method.
+ * <pre>
+ * ArrayUtils.add([1], 0, 2) = [2, 1]
+ * ArrayUtils.add([2, 6], 2, 3) = [2, 6, 3]
+ * ArrayUtils.add([2, 6], 0, 1) = [1, 2, 6]
+ * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
+ * </pre>
*
- * @param array1 the left hand array to compare, may be {@code null}
- * @param array2 the right hand array to compare, may be {@code null}
- * @return {@code true} if the arrays are equal
- * @deprecated this method has been replaced by {@code java.util.Objects.deepEquals(Object, Object)} and will be
- * removed from future releases.
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ * @deprecated this method has been superseded by {@link #insert(int, byte[], byte...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
@Deprecated
- public static boolean isEquals(final Object array1, final Object array2) {
- return new EqualsBuilder().append(array1, array2).isEquals();
+ public static byte[] add(final byte[] array, final int index, final byte element) {
+ return (byte[]) add(array, index, Byte.valueOf(element), Byte.TYPE);
}
- // To map
- //-----------------------------------------------------------------------
/**
- * <p>Converts the given array into a {@link java.util.Map}. Each element of the array
- * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
- * elements, where the first element is used as key and the second as
- * value.
+ * <p>Copies the given array and adds the given element at the end of the new array.
+ *
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
+ *
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * <p>This method can be used to initialize:
* <pre>
- * // Create a Map mapping colors.
- * Map colorMap = ArrayUtils.toMap(new String[][] {
- * {"RED", "#FF0000"},
- * {"GREEN", "#00FF00"},
- * {"BLUE", "#0000FF"}});
+ * ArrayUtils.add(null, '0') = ['0']
+ * ArrayUtils.add(['1'], '0') = ['1', '0']
+ * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
* </pre>
*
- * <p>This method returns {@code null} for a {@code null} input array.
- *
- * @param array an array whose elements are either a {@link java.util.Map.Entry} or
- * an Array containing at least two elements, may be {@code null}
- * @return a {@code Map} that was created from the array
- * @throws IllegalArgumentException if one element of this Array is
- * itself an Array containing less then two elements
- * @throws IllegalArgumentException if the array contains elements other
- * than {@link java.util.Map.Entry} and an Array
+ * @param array the array to copy and add the element to, may be {@code null}
+ * @param element the object to add at the last index of the new array
+ * @return A new array containing the existing elements plus the new element
+ * @since 2.1
*/
- public static Map<Object, Object> toMap(final Object[] array) {
- if (array == null) {
- return null;
- }
- final Map<Object, Object> map = new HashMap<>((int) (array.length * 1.5));
- for (int i = 0; i < array.length; i++) {
- final Object object = array[i];
- if (object instanceof Map.Entry<?, ?>) {
- final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object;
- map.put(entry.getKey(), entry.getValue());
- } else if (object instanceof Object[]) {
- final Object[] entry = (Object[]) object;
- if (entry.length < 2) {
- throw new IllegalArgumentException("Array element " + i + ", '"
- + object
- + "', has a length less than 2");
- }
- map.put(entry[0], entry[1]);
- } else {
- throw new IllegalArgumentException("Array element " + i + ", '"
- + object
- + "', is neither of type Map.Entry nor an Array");
- }
- }
- return map;
+ public static char[] add(final char[] array, final char element) {
+ final char[] newArray = (char[]) copyArrayGrow1(array, Character.TYPE);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
- // Generic array
- //-----------------------------------------------------------------------
/**
- * <p>Create a type-safe generic array.
- *
- * <p>The Java language does not allow an array to be created from a generic type:
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <pre>
- public static <T> T[] createAnArray(int size) {
- return new T[size]; // compiler error here
- }
- public static <T> T[] createAnArray(int size) {
- return (T[]) new Object[size]; // ClassCastException at runtime
- }
- * </pre>
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * <p>Therefore new arrays of generic types can be created with this method.
- * For example, an array of Strings can be created:
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
* <pre>
- String[] array = ArrayUtils.toArray("1", "2");
- String[] emptyArray = ArrayUtils.<String>toArray();
+ * ArrayUtils.add(null, 0, 'a') = ['a']
+ * ArrayUtils.add(['a'], 0, 'b') = ['b', 'a']
+ * ArrayUtils.add(['a', 'b'], 0, 'c') = ['c', 'a', 'b']
+ * ArrayUtils.add(['a', 'b'], 1, 'k') = ['a', 'k', 'b']
+ * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c']
* </pre>
*
- * <p>The method is typically used in scenarios, where the caller itself uses generic types
- * that have to be combined into an array.
- *
- * <p>Note, this method makes only sense to provide arguments of the same type so that the
- * compiler can deduce the type of the array itself. While it is possible to select the
- * type explicitly like in
- * <code>Number[] array = ArrayUtils.<Number>toArray(Integer.valueOf(42), Double.valueOf(Math.PI))</code>,
- * there is no real advantage when compared to
- * <code>new Number[] {Integer.valueOf(42), Double.valueOf(Math.PI)}</code>.
- *
- * @param <T> the array's element type
- * @param items the varargs array items, null allowed
- * @return the array, not null unless a null array is passed in
- * @since 3.0
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ * @deprecated this method has been superseded by {@link #insert(int, char[], char...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
- public static <T> T[] toArray(@SuppressWarnings("unchecked") final T... items) {
- return items;
+ @Deprecated
+ public static char[] add(final char[] array, final int index, final char element) {
+ return (char[]) add(array, index, Character.valueOf(element), Character.TYPE);
}
- // Clone
- //-----------------------------------------------------------------------
/**
- * <p>Shallow clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>Copies the given array and adds the given element at the end of the new array.
*
- * <p>The objects in the array are not cloned, thus there is no special
- * handling for multi-dimensional arrays.
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * @param <T> the component type of the array
- * @param array the array to shallow clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
+ * <pre>
+ * ArrayUtils.add(null, 0) = [0]
+ * ArrayUtils.add([1], 0) = [1, 0]
+ * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * </pre>
+ *
+ * @param array the array to copy and add the element to, may be {@code null}
+ * @param element the object to add at the last index of the new array
+ * @return A new array containing the existing elements plus the new element
+ * @since 2.1
*/
- public static <T> T[] clone(final T[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
+ public static double[] add(final double[] array, final double element) {
+ final double[] newArray = (double[]) copyArrayGrow1(array, Double.TYPE);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
/**
- * <p>Clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array the array to clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
- */
- public static long[] clone(final long[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
- }
-
- /**
- * <p>Clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <pre>
+ * ArrayUtils.add([1.1], 0, 2.2) = [2.2, 1.1]
+ * ArrayUtils.add([2.3, 6.4], 2, 10.5) = [2.3, 6.4, 10.5]
+ * ArrayUtils.add([2.6, 6.7], 0, -4.8) = [-4.8, 2.6, 6.7]
+ * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0) = [2.9, 6.0, 1.0, 0.3]
+ * </pre>
*
- * @param array the array to clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ * @deprecated this method has been superseded by {@link #insert(int, double[], double...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
- public static int[] clone(final int[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
+ @Deprecated
+ public static double[] add(final double[] array, final int index, final double element) {
+ return (double[]) add(array, index, Double.valueOf(element), Double.TYPE);
}
/**
- * <p>Clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>Copies the given array and adds the given element at the end of the new array.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
*
- * @param array the array to clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
- */
- public static short[] clone(final short[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
- }
-
- /**
- * <p>Clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <pre>
+ * ArrayUtils.add(null, 0) = [0]
+ * ArrayUtils.add([1], 0) = [1, 0]
+ * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * </pre>
*
- * @param array the array to clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
+ * @param array the array to copy and add the element to, may be {@code null}
+ * @param element the object to add at the last index of the new array
+ * @return A new array containing the existing elements plus the new element
+ * @since 2.1
*/
- public static char[] clone(final char[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
+ public static float[] add(final float[] array, final float element) {
+ final float[] newArray = (float[]) copyArrayGrow1(array, Float.TYPE);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
/**
- * <p>Clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array the array to clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
+ *
+ * <pre>
+ * ArrayUtils.add([1.1f], 0, 2.2f) = [2.2f, 1.1f]
+ * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f) = [2.3f, 6.4f, 10.5f]
+ * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f) = [-4.8f, 2.6f, 6.7f]
+ * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f) = [2.9f, 6.0f, 1.0f, 0.3f]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ * @deprecated this method has been superseded by {@link #insert(int, float[], float...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
- public static byte[] clone(final byte[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
+ @Deprecated
+ public static float[] add(final float[] array, final int index, final float element) {
+ return (float[]) add(array, index, Float.valueOf(element), Float.TYPE);
}
/**
- * <p>Clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>Copies the given array and adds the given element at the end of the new array.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
*
- * @param array the array to clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
- */
- public static double[] clone(final double[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
- }
-
- /**
- * <p>Clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <pre>
+ * ArrayUtils.add(null, 0) = [0]
+ * ArrayUtils.add([1], 0) = [1, 0]
+ * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * </pre>
*
- * @param array the array to clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
+ * @param array the array to copy and add the element to, may be {@code null}
+ * @param element the object to add at the last index of the new array
+ * @return A new array containing the existing elements plus the new element
+ * @since 2.1
*/
- public static float[] clone(final float[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
+ public static int[] add(final int[] array, final int element) {
+ final int[] newArray = (int[]) copyArrayGrow1(array, Integer.TYPE);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
/**
- * <p>Clones an array returning a typecast result and handling
- * {@code null}.
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array the array to clone, may be {@code null}
- * @return the cloned array, {@code null} if {@code null} input
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
+ *
+ * <pre>
+ * ArrayUtils.add([1], 0, 2) = [2, 1]
+ * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
+ * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
+ * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ * @deprecated this method has been superseded by {@link #insert(int, int[], int...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
- public static boolean[] clone(final boolean[] array) {
- if (array == null) {
- return null;
- }
- return array.clone();
+ @Deprecated
+ public static int[] add(final int[] array, final int index, final int element) {
+ return (int[]) add(array, index, Integer.valueOf(element), Integer.TYPE);
}
- // nullToEmpty
- //-----------------------------------------------------------------------
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array the array to check for {@code null} or empty
- * @param type the class representation of the desired array
- * @param <T> the class type
- * @return the same array, {@code public static} empty array if {@code null}
- * @throws IllegalArgumentException if the type argument is null
- * @since 3.5
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
+ *
+ * <pre>
+ * ArrayUtils.add([1L], 0, 2L) = [2L, 1L]
+ * ArrayUtils.add([2L, 6L], 2, 10L) = [2L, 6L, 10L]
+ * ArrayUtils.add([2L, 6L], 0, -4L) = [-4L, 2L, 6L]
+ * ArrayUtils.add([2L, 6L, 3L], 2, 1L) = [2L, 6L, 1L, 3L]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ * @deprecated this method has been superseded by {@link #insert(int, long[], long...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
- public static <T> T[] nullToEmpty(final T[] array, final Class<T[]> type) {
- if (type == null) {
- throw new IllegalArgumentException("The type must not be null");
- }
-
- if (array == null) {
- return type.cast(Array.newInstance(type.getComponentType(), 0));
- }
- return array;
+ @Deprecated
+ public static long[] add(final long[] array, final int index, final long element) {
+ return (long[]) add(array, index, Long.valueOf(element), Long.TYPE);
}
-
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
+ * <p>Copies the given array and adds the given element at the end of the new array.
*
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * <pre>
+ * ArrayUtils.add(null, 0) = [0]
+ * ArrayUtils.add([1], 0) = [1, 0]
+ * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * </pre>
+ *
+ * @param array the array to copy and add the element to, may be {@code null}
+ * @param element the object to add at the last index of the new array
+ * @return A new array containing the existing elements plus the new element
+ * @since 2.1
*/
- public static Object[] nullToEmpty(final Object[] array) {
- if (isEmpty(array)) {
- return EMPTY_OBJECT_ARRAY;
- }
- return array;
+ public static long[] add(final long[] array, final long element) {
+ final long[] newArray = (long[]) copyArrayGrow1(array, Long.TYPE);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
- *
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * Underlying implementation of add(array, index, element) methods.
+ * The last parameter is the class, which may not equal element.getClass
+ * for primitives.
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 3.2
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @param clss the type of the element being added
+ * @return A new array containing the existing elements and the new element
*/
- public static Class<?>[] nullToEmpty(final Class<?>[] array) {
- if (isEmpty(array)) {
- return EMPTY_CLASS_ARRAY;
+ private static Object add(final Object array, final int index, final Object element, final Class<?> clss) {
+ if (array == null) {
+ if (index != 0) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0");
+ }
+ final Object joinedArray = Array.newInstance(clss, 1);
+ Array.set(joinedArray, 0, element);
+ return joinedArray;
}
- return array;
+ final int length = Array.getLength(array);
+ if (index > length || index < 0) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
+ }
+ final Object result = Array.newInstance(clss, length + 1);
+ System.arraycopy(array, 0, result, 0, index);
+ Array.set(result, index, element);
+ if (index < length) {
+ System.arraycopy(array, index, result, index + 1, length - index);
+ }
+ return result;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * <pre>
+ * ArrayUtils.add([1], 0, 2) = [2, 1]
+ * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
+ * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
+ * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ * @deprecated this method has been superseded by {@link #insert(int, short[], short...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
- public static String[] nullToEmpty(final String[] array) {
- if (isEmpty(array)) {
- return EMPTY_STRING_ARRAY;
- }
- return array;
+ @Deprecated
+ public static short[] add(final short[] array, final int index, final short element) {
+ return (short[]) add(array, index, Short.valueOf(element), Short.TYPE);
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Copies the given array and adds the given element at the end of the new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
- */
- public static long[] nullToEmpty(final long[] array) {
- if (isEmpty(array)) {
- return EMPTY_LONG_ARRAY;
- }
- return array;
- }
-
- /**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.add(null, 0) = [0]
+ * ArrayUtils.add([1], 0) = [1, 0]
+ * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array the array to copy and add the element to, may be {@code null}
+ * @param element the object to add at the last index of the new array
+ * @return A new array containing the existing elements plus the new element
+ * @since 2.1
*/
- public static int[] nullToEmpty(final int[] array) {
- if (isEmpty(array)) {
- return EMPTY_INT_ARRAY;
- }
- return array;
+ public static short[] add(final short[] array, final short element) {
+ final short[] newArray = (short[]) copyArrayGrow1(array, Short.TYPE);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
+
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
+ * <p>Inserts the specified element at the specified position in the array.
+ * Shifts the element currently at that position (if any) and any subsequent
+ * elements to the right (adds one to their indices).
*
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element.
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * <pre>
+ * ArrayUtils.add(null, 0, null) = IllegalArgumentException
+ * ArrayUtils.add(null, 0, "a") = ["a"]
+ * ArrayUtils.add(["a"], 1, null) = ["a", null]
+ * ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
+ * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
+ * </pre>
+ *
+ * @param <T> the component type of the array
+ * @param array the array to add the element to, may be {@code null}
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new element
+ * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index > array.length).
+ * @throws IllegalArgumentException if both array and element are null
+ * @deprecated this method has been superseded by {@link #insert(int, Object[], Object...) insert(int, T[], T...)} and
+ * may be removed in a future release. Please note the handling of {@code null} input arrays differs
+ * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
*/
- public static short[] nullToEmpty(final short[] array) {
- if (isEmpty(array)) {
- return EMPTY_SHORT_ARRAY;
+ @Deprecated
+ public static <T> T[] add(final T[] array, final int index, final T element) {
+ Class<?> clss = null;
+ if (array != null) {
+ clss = array.getClass().getComponentType();
+ } else if (element != null) {
+ clss = element.getClass();
+ } else {
+ throw new IllegalArgumentException("Array and element cannot both be null");
}
- return array;
+ @SuppressWarnings("unchecked") // the add method creates an array of type clss, which is type T
+ final T[] newArray = (T[]) add(array, index, element, clss);
+ return newArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
+ * <p>Copies the given array and adds the given element at the end of the new array.
*
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>The new array contains the same elements of the input
+ * array plus the given element in the last position. The component type of
+ * the new array is the same as that of the input array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <p>If the input array is {@code null}, a new one element array is returned
+ * whose component type is the same as the element, unless the element itself is null,
+ * in which case the return type is Object[]
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * <pre>
+ * ArrayUtils.add(null, null) = IllegalArgumentException
+ * ArrayUtils.add(null, "a") = ["a"]
+ * ArrayUtils.add(["a"], null) = ["a", null]
+ * ArrayUtils.add(["a"], "b") = ["a", "b"]
+ * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
+ * </pre>
+ *
+ * @param <T> the component type of the array
+ * @param array the array to "add" the element to, may be {@code null}
+ * @param element the object to add, may be {@code null}
+ * @return A new array containing the existing elements plus the new element
+ * The returned array type will be that of the input array (unless null),
+ * in which case it will have the same type as the element.
+ * If both are null, an IllegalArgumentException is thrown
+ * @since 2.1
+ * @throws IllegalArgumentException if both arguments are null
*/
- public static char[] nullToEmpty(final char[] array) {
- if (isEmpty(array)) {
- return EMPTY_CHAR_ARRAY;
+ public static <T> T[] add(final T[] array, final T element) {
+ Class<?> type;
+ if (array != null) {
+ type = array.getClass().getComponentType();
+ } else if (element != null) {
+ type = element.getClass();
+ } else {
+ throw new IllegalArgumentException("Arguments cannot both be null");
}
- return array;
+ @SuppressWarnings("unchecked") // type must be T
+ final
+ T[] newArray = (T[]) copyArrayGrow1(array, type);
+ newArray[newArray.length - 1] = element;
+ return newArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array1 the first array whose elements are added to the new array.
+ * @param array2 the second array whose elements are added to the new array.
+ * @return The new boolean[] array.
+ * @since 2.1
*/
- public static byte[] nullToEmpty(final byte[] array) {
- if (isEmpty(array)) {
- return EMPTY_BYTE_ARRAY;
+ public static boolean[] addAll(final boolean[] array1, final boolean... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final boolean[] joinedArray = new boolean[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array1 the first array whose elements are added to the new array.
+ * @param array2 the second array whose elements are added to the new array.
+ * @return The new byte[] array.
+ * @since 2.1
*/
- public static double[] nullToEmpty(final double[] array) {
- if (isEmpty(array)) {
- return EMPTY_DOUBLE_ARRAY;
+ public static byte[] addAll(final byte[] array1, final byte... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final byte[] joinedArray = new byte[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array1 the first array whose elements are added to the new array.
+ * @param array2 the second array whose elements are added to the new array.
+ * @return The new char[] array.
+ * @since 2.1
*/
- public static float[] nullToEmpty(final float[] array) {
- if (isEmpty(array)) {
- return EMPTY_FLOAT_ARRAY;
+ public static char[] addAll(final char[] array1, final char... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final char[] joinedArray = new char[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array1 the first array whose elements are added to the new array.
+ * @param array2 the second array whose elements are added to the new array.
+ * @return The new double[] array.
+ * @since 2.1
*/
- public static boolean[] nullToEmpty(final boolean[] array) {
- if (isEmpty(array)) {
- return EMPTY_BOOLEAN_ARRAY;
+ public static double[] addAll(final double[] array1, final double... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final double[] joinedArray = new double[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array1 the first array whose elements are added to the new array.
+ * @param array2 the second array whose elements are added to the new array.
+ * @return The new float[] array.
+ * @since 2.1
*/
- public static Long[] nullToEmpty(final Long[] array) {
- if (isEmpty(array)) {
- return EMPTY_LONG_OBJECT_ARRAY;
+ public static float[] addAll(final float[] array1, final float... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final float[] joinedArray = new float[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array1 the first array whose elements are added to the new array.
+ * @param array2 the second array whose elements are added to the new array.
+ * @return The new int[] array.
+ * @since 2.1
*/
- public static Integer[] nullToEmpty(final Integer[] array) {
- if (isEmpty(array)) {
- return EMPTY_INTEGER_OBJECT_ARRAY;
+ public static int[] addAll(final int[] array1, final int... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final int[] joinedArray = new int[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array1 the first array whose elements are added to the new array.
+ * @param array2 the second array whose elements are added to the new array.
+ * @return The new long[] array.
+ * @since 2.1
*/
- public static Short[] nullToEmpty(final Short[] array) {
- if (isEmpty(array)) {
- return EMPTY_SHORT_OBJECT_ARRAY;
+ public static long[] addAll(final long[] array1, final long... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final long[] joinedArray = new long[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param array1 the first array whose elements are added to the new array.
+ * @param array2 the second array whose elements are added to the new array.
+ * @return The new short[] array.
+ * @since 2.1
*/
- public static Character[] nullToEmpty(final Character[] array) {
- if (isEmpty(array)) {
- return EMPTY_CHARACTER_OBJECT_ARRAY;
+ public static short[] addAll(final short[] array1, final short... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final short[] joinedArray = new short[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
- *
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>Adds all the elements of the given arrays into a new array.
+ * <p>The new array contains all of the element of {@code array1} followed
+ * by all of the elements {@code array2}. When an array is returned, it is always
+ * a new array.
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <pre>
+ * ArrayUtils.addAll(null, null) = null
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * ArrayUtils.addAll([null], [null]) = [null, null]
+ * ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
+ * </pre>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * @param <T> the component type of the array
+ * @param array1 the first array whose elements are added to the new array, may be {@code null}
+ * @param array2 the second array whose elements are added to the new array, may be {@code null}
+ * @return The new array, {@code null} if both arrays are {@code null}.
+ * The type of the new array is the type of the first array,
+ * unless the first array is null, in which case the type is the same as the second array.
+ * @since 2.1
+ * @throws IllegalArgumentException if the array types are incompatible
*/
- public static Byte[] nullToEmpty(final Byte[] array) {
- if (isEmpty(array)) {
- return EMPTY_BYTE_OBJECT_ARRAY;
+ public static <T> T[] addAll(final T[] array1, @SuppressWarnings("unchecked") final T... array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
}
- return array;
+ final Class<?> type1 = array1.getClass().getComponentType();
+ @SuppressWarnings("unchecked") // OK, because array is of type T
+ final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ try {
+ System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+ } catch (final ArrayStoreException ase) {
+ // Check if problem was due to incompatible types
+ /*
+ * We do this here, rather than before the copy because:
+ * - it would be a wasted check most of the time
+ * - safer, in case check turns out to be too strict
+ */
+ final Class<?> type2 = array2.getClass().getComponentType();
+ if (!type1.isAssignableFrom(type2)) {
+ throw new IllegalArgumentException("Cannot store " + type2.getName() + " in an array of "
+ + type1.getName(), ase);
+ }
+ throw ase; // No, so rethrow original
+ }
+ return joinedArray;
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
+ * Copies the given array and adds the given element at the beginning of the new array.
*
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first position. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element.
+ * </p>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * <pre>
+ * ArrayUtils.add(null, true) = [true]
+ * ArrayUtils.add([true], false) = [false, true]
+ * ArrayUtils.add([true, false], true) = [true, true, false]
+ * </pre>
+ *
+ * @param array the array to "add" the element to, may be {@code null}.
+ * @param element the object to add.
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element.
+ * @since 3.10
*/
- public static Double[] nullToEmpty(final Double[] array) {
- if (isEmpty(array)) {
- return EMPTY_DOUBLE_OBJECT_ARRAY;
- }
- return array;
+ public static boolean[] addFirst(final boolean[] array, final boolean element) {
+ return array == null ? add(array, element) : insert(0, array, element);
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
+ * Copies the given array and adds the given element at the beginning of the new array.
*
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first position. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element.
+ * </p>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * <pre>
+ * ArrayUtils.add(null, 1) = [1]
+ * ArrayUtils.add([1], 0) = [0, 1]
+ * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
+ * </pre>
+ *
+ * @param array the array to "add" the element to, may be {@code null}.
+ * @param element the object to add.
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element.
+ * @since 3.10
*/
- public static Float[] nullToEmpty(final Float[] array) {
- if (isEmpty(array)) {
- return EMPTY_FLOAT_OBJECT_ARRAY;
- }
- return array;
+ public static byte[] addFirst(final byte[] array, final byte element) {
+ return array == null ? add(array, element) : insert(0, array, element);
}
/**
- * <p>Defensive programming technique to change a {@code null}
- * reference to an empty one.
+ * Copies the given array and adds the given element at the beginning of the new array.
*
- * <p>This method returns an empty array for a {@code null} input array.
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first position. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
*
- * <p>As a memory optimizing technique an empty array passed in will be overridden with
- * the empty {@code public static} references in this class.
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element.
+ * </p>
*
- * @param array the array to check for {@code null} or empty
- * @return the same array, {@code public static} empty array if {@code null} or empty input
- * @since 2.5
+ * <pre>
+ * ArrayUtils.add(null, '1') = ['1']
+ * ArrayUtils.add(['1'], '0') = ['0', '1']
+ * ArrayUtils.add(['1', '0'], '1') = ['1', '1', '0']
+ * </pre>
+ *
+ * @param array the array to "add" the element to, may be {@code null}.
+ * @param element the object to add.
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element.
+ * @since 3.10
*/
- public static Boolean[] nullToEmpty(final Boolean[] array) {
- if (isEmpty(array)) {
- return EMPTY_BOOLEAN_OBJECT_ARRAY;
- }
- return array;
+ public static char[] addFirst(final char[] array, final char element) {
+ return array == null ? add(array, element) : insert(0, array, element);
}
- // Subarrays
- //-----------------------------------------------------------------------
/**
- * <p>Produces a new array containing the elements between
- * the start and end indices.
+ * Copies the given array and adds the given element at the beginning of the new array.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first position. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
*
- * <p>The component type of the subarray is always the same as
- * that of the input array. Thus, if the input is an array of type
- * {@code Date}, the following usage is envisaged:
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element.
+ * </p>
*
* <pre>
- * Date[] someDates = (Date[]) ArrayUtils.subarray(allDates, 2, 5);
+ * ArrayUtils.add(null, 1) = [1]
+ * ArrayUtils.add([1], 0) = [0, 1]
+ * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
* </pre>
*
- * @param <T> the component type of the array
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(Object[], int, int)
+ * @param array the array to "add" the element to, may be {@code null}.
+ * @param element the object to add.
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element.
+ * @since 3.10
*/
- public static <T> T[] subarray(final T[] array, int startIndexInclusive, int endIndexExclusive) {
- if (array == null) {
- return null;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- final Class<?> type = array.getClass().getComponentType();
- if (newSize <= 0) {
- @SuppressWarnings("unchecked") // OK, because array is of type T
- final T[] emptyArray = (T[]) Array.newInstance(type, 0);
- return emptyArray;
- }
- @SuppressWarnings("unchecked") // OK, because array is of type T
- final
- T[] subarray = (T[]) Array.newInstance(type, newSize);
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ public static double[] addFirst(final double[] array, final double element) {
+ return array == null ? add(array, element) : insert(0, array, element);
}
/**
- * <p>Produces a new {@code long} array containing the elements
- * between the start and end indices.
+ * Copies the given array and adds the given element at the beginning of the new array.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first position. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
*
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(long[], int, int)
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element.
+ * </p>
+ *
+ * <pre>
+ * ArrayUtils.add(null, 1) = [1]
+ * ArrayUtils.add([1], 0) = [0, 1]
+ * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
+ * </pre>
+ *
+ * @param array the array to "add" the element to, may be {@code null}.
+ * @param element the object to add.
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element.
+ * @since 3.10
*/
- public static long[] subarray(final long[] array, int startIndexInclusive, int endIndexExclusive) {
- if (array == null) {
- return null;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- if (newSize <= 0) {
- return EMPTY_LONG_ARRAY;
- }
-
- final long[] subarray = new long[newSize];
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ public static float[] addFirst(final float[] array, final float element) {
+ return array == null ? add(array, element) : insert(0, array, element);
}
/**
- * <p>Produces a new {@code int} array containing the elements
- * between the start and end indices.
+ * Copies the given array and adds the given element at the beginning of the new array.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first position. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
*
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(int[], int, int)
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element.
+ * </p>
+ *
+ * <pre>
+ * ArrayUtils.add(null, 1) = [1]
+ * ArrayUtils.add([1], 0) = [0, 1]
+ * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
+ * </pre>
+ *
+ * @param array the array to "add" the element to, may be {@code null}.
+ * @param element the object to add.
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element.
+ * @since 3.10
*/
- public static int[] subarray(final int[] array, int startIndexInclusive, int endIndexExclusive) {
- if (array == null) {
- return null;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- if (newSize <= 0) {
- return EMPTY_INT_ARRAY;
- }
-
- final int[] subarray = new int[newSize];
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ public static int[] addFirst(final int[] array, final int element) {
+ return array == null ? add(array, element) : insert(0, array, element);
}
/**
- * <p>Produces a new {@code short} array containing the elements
- * between the start and end indices.
+ * Copies the given array and adds the given element at the beginning of the new array.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first position. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
*
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(short[], int, int)
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element.
+ * </p>
+ *
+ * <pre>
+ * ArrayUtils.add(null, 1) = [1]
+ * ArrayUtils.add([1], 0) = [0, 1]
+ * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
+ * </pre>
+ *
+ * @param array the array to "add" the element to, may be {@code null}.
+ * @param element the object to add.
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element.
+ * @since 3.10
*/
- public static short[] subarray(final short[] array, int startIndexInclusive, int endIndexExclusive) {
- if (array == null) {
- return null;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- if (newSize <= 0) {
- return EMPTY_SHORT_ARRAY;
- }
-
- final short[] subarray = new short[newSize];
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ public static long[] addFirst(final long[] array, final long element) {
+ return array == null ? add(array, element) : insert(0, array, element);
}
/**
- * <p>Produces a new {@code char} array containing the elements
- * between the start and end indices.
+ * Copies the given array and adds the given element at the beginning of the new array.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first position. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
*
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(char[], int, int)
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element.
+ * </p>
+ *
+ * <pre>
+ * ArrayUtils.add(null, 1) = [1]
+ * ArrayUtils.add([1], 0) = [0, 1]
+ * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
+ * </pre>
+ *
+ * @param array the array to "add" the element to, may be {@code null}.
+ * @param element the object to add.
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element.
+ * @since 3.10
*/
- public static char[] subarray(final char[] array, int startIndexInclusive, int endIndexExclusive) {
- if (array == null) {
- return null;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- if (newSize <= 0) {
- return EMPTY_CHAR_ARRAY;
- }
+ public static short[] addFirst(final short[] array, final short element) {
+ return array == null ? add(array, element) : insert(0, array, element);
+ }
- final char[] subarray = new char[newSize];
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ /**
+ * Copies the given array and adds the given element at the beginning of the new array.
+ *
+ * <p>
+ * The new array contains the same elements of the input array plus the given element in the first positioaddFirstaddFirstaddFirstn. The
+ * component type of the new array is the same as that of the input array.
+ * </p>
+ *
+ * <p>
+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
+ * element, unless the element itself is null, in which case the return type is Object[]
+ * </p>
+ *
+ * <pre>
+ * ArrayUtils.add(null, null) = IllegalArgumentException
+ * ArrayUtils.add(null, "a") = ["a"]
+ * ArrayUtils.add(["a"], null) = [null, "a"]
+ * ArrayUtils.add(["a"], "b") = ["b", "a"]
+ * ArrayUtils.add(["a", "b"], "c") = ["c", "a", "b"]
+ * </pre>
+ *
+ * @param <T> the component type of the array
+ * @param array the array to "add" the element to, may be {@code null}
+ * @param element the object to add, may be {@code null}
+ * @return A new array containing the existing elements plus the new element The returned array type will be that of
+ * the input array (unless null), in which case it will have the same type as the element. If both are null,
+ * an IllegalArgumentException is thrown
+ * @since 3.10
+ * @throws IllegalArgumentException if both arguments are null
+ */
+ public static <T> T[] addFirst(final T[] array, final T element) {
+ return array == null ? add(array, element) : insert(0, array, element);
}
/**
- * <p>Produces a new {@code byte} array containing the elements
- * between the start and end indices.
+ * <p>Clones an array returning a typecast result and handling
+ * {@code null}.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>This method returns {@code null} for a {@code null} input array.
*
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(byte[], int, int)
+ * @param array the array to clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static byte[] subarray(final byte[] array, int startIndexInclusive, int endIndexExclusive) {
+ public static boolean[] clone(final boolean[] array) {
if (array == null) {
return null;
}
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- if (newSize <= 0) {
- return EMPTY_BYTE_ARRAY;
- }
-
- final byte[] subarray = new byte[newSize];
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ return array.clone();
}
/**
- * <p>Produces a new {@code double} array containing the elements
- * between the start and end indices.
+ * <p>Clones an array returning a typecast result and handling
+ * {@code null}.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>This method returns {@code null} for a {@code null} input array.
*
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(double[], int, int)
+ * @param array the array to clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static double[] subarray(final double[] array, int startIndexInclusive, int endIndexExclusive) {
+ public static byte[] clone(final byte[] array) {
if (array == null) {
return null;
}
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- if (newSize <= 0) {
- return EMPTY_DOUBLE_ARRAY;
- }
-
- final double[] subarray = new double[newSize];
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ return array.clone();
}
/**
- * <p>Produces a new {@code float} array containing the elements
- * between the start and end indices.
+ * <p>Clones an array returning a typecast result and handling
+ * {@code null}.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>This method returns {@code null} for a {@code null} input array.
*
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(float[], int, int)
+ * @param array the array to clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static float[] subarray(final float[] array, int startIndexInclusive, int endIndexExclusive) {
+ public static char[] clone(final char[] array) {
if (array == null) {
return null;
}
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- if (newSize <= 0) {
- return EMPTY_FLOAT_ARRAY;
- }
-
- final float[] subarray = new float[newSize];
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ return array.clone();
}
/**
- * <p>Produces a new {@code boolean} array containing the elements
- * between the start and end indices.
+ * <p>Clones an array returning a typecast result and handling
+ * {@code null}.
*
- * <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
+ * <p>This method returns {@code null} for a {@code null} input array.
*
- * @param array the array
- * @param startIndexInclusive the starting index. Undervalue (<0)
- * is promoted to 0, overvalue (>array.length) results
- * in an empty array.
- * @param endIndexExclusive elements up to endIndex-1 are present in the
- * returned subarray. Undervalue (< startIndex) produces
- * empty array, overvalue (>array.length) is demoted to
- * array length.
- * @return a new array containing the elements between
- * the start and end indices.
- * @since 2.1
- * @see Arrays#copyOfRange(boolean[], int, int)
+ * @param array the array to clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static boolean[] subarray(final boolean[] array, int startIndexInclusive, int endIndexExclusive) {
+ public static double[] clone(final double[] array) {
if (array == null) {
return null;
}
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive > array.length) {
- endIndexExclusive = array.length;
- }
- final int newSize = endIndexExclusive - startIndexInclusive;
- if (newSize <= 0) {
- return EMPTY_BOOLEAN_ARRAY;
- }
-
- final boolean[] subarray = new boolean[newSize];
- System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
- return subarray;
+ return array.clone();
}
- // Is same length
- //-----------------------------------------------------------------------
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Clones an array returning a typecast result and handling
+ * {@code null}.
*
- * <p>Any multi-dimensional aspects of the arrays are ignored.
+ * <p>This method returns {@code null} for a {@code null} input array.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * @param array the array to clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static boolean isSameLength(final Object[] array1, final Object[] array2) {
- return getLength(array1) == getLength(array2);
+ public static float[] clone(final float[] array) {
+ if (array == null) {
+ return null;
+ }
+ return array.clone();
}
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Clones an array returning a typecast result and handling
+ * {@code null}.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * <p>This method returns {@code null} for a {@code null} input array.
+ *
+ * @param array the array to clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static boolean isSameLength(final long[] array1, final long[] array2) {
- return getLength(array1) == getLength(array2);
+ public static int[] clone(final int[] array) {
+ if (array == null) {
+ return null;
+ }
+ return array.clone();
}
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Clones an array returning a typecast result and handling
+ * {@code null}.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * <p>This method returns {@code null} for a {@code null} input array.
+ *
+ * @param array the array to clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static boolean isSameLength(final int[] array1, final int[] array2) {
- return getLength(array1) == getLength(array2);
+ public static long[] clone(final long[] array) {
+ if (array == null) {
+ return null;
+ }
+ return array.clone();
}
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Clones an array returning a typecast result and handling
+ * {@code null}.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * <p>This method returns {@code null} for a {@code null} input array.
+ *
+ * @param array the array to clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static boolean isSameLength(final short[] array1, final short[] array2) {
- return getLength(array1) == getLength(array2);
+ public static short[] clone(final short[] array) {
+ if (array == null) {
+ return null;
+ }
+ return array.clone();
}
+ // Clone
+ //-----------------------------------------------------------------------
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Shallow clones an array returning a typecast result and handling
+ * {@code null}.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * <p>The objects in the array are not cloned, thus there is no special
+ * handling for multi-dimensional arrays.
+ *
+ * <p>This method returns {@code null} for a {@code null} input array.
+ *
+ * @param <T> the component type of the array
+ * @param array the array to shallow clone, may be {@code null}
+ * @return the cloned array, {@code null} if {@code null} input
*/
- public static boolean isSameLength(final char[] array1, final char[] array2) {
- return getLength(array1) == getLength(array2);
+ public static <T> T[] clone(final T[] array) {
+ if (array == null) {
+ return null;
+ }
+ return array.clone();
}
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Checks if the value is in the given array.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
*/
- public static boolean isSameLength(final byte[] array1, final byte[] array2) {
- return getLength(array1) == getLength(array2);
+ public static boolean contains(final boolean[] array, final boolean valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Checks if the value is in the given array.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
*/
- public static boolean isSameLength(final double[] array1, final double[] array2) {
- return getLength(array1) == getLength(array2);
+ public static boolean contains(final byte[] array, final byte valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Checks if the value is in the given array.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ * @since 2.1
*/
- public static boolean isSameLength(final float[] array1, final float[] array2) {
- return getLength(array1) == getLength(array2);
+ public static boolean contains(final char[] array, final char valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
/**
- * <p>Checks whether two arrays are the same length, treating
- * {@code null} arrays as length {@code 0}.
+ * <p>Checks if the value is in the given array.
*
- * @param array1 the first array, may be {@code null}
- * @param array2 the second array, may be {@code null}
- * @return {@code true} if length of arrays matches, treating
- * {@code null} as an empty array
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
*/
- public static boolean isSameLength(final boolean[] array1, final boolean[] array2) {
- return getLength(array1) == getLength(array2);
+ public static boolean contains(final double[] array, final double valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
- //-----------------------------------------------------------------------
/**
- * <p>Returns the length of the specified array.
- * This method can deal with {@code Object} arrays and with primitive arrays.
- *
- * <p>If the input array is {@code null}, {@code 0} is returned.
+ * <p>Checks if a value falling within the given tolerance is in the
+ * given array. If the array contains a value within the inclusive range
+ * defined by (value - tolerance) to (value + tolerance).
*
- * <pre>
- * ArrayUtils.getLength(null) = 0
- * ArrayUtils.getLength([]) = 0
- * ArrayUtils.getLength([null]) = 1
- * ArrayUtils.getLength([true, false]) = 2
- * ArrayUtils.getLength([1, 2, 3]) = 3
- * ArrayUtils.getLength(["a", "b", "c"]) = 3
- * </pre>
+ * <p>The method returns {@code false} if a {@code null} array
+ * is passed in.
*
- * @param array the array to retrieve the length from, may be null
- * @return The length of the array, or {@code 0} if the array is {@code null}
- * @throws IllegalArgumentException if the object argument is not an array.
- * @since 2.1
+ * @param array the array to search
+ * @param valueToFind the value to find
+ * @param tolerance the array contains the tolerance of the search
+ * @return true if value falling within tolerance is in array
*/
- public static int getLength(final Object array) {
- if (array == null) {
- return 0;
- }
- return Array.getLength(array);
+ public static boolean contains(final double[] array, final double valueToFind, final double tolerance) {
+ return indexOf(array, valueToFind, 0, tolerance) != INDEX_NOT_FOUND;
}
/**
- * <p>Checks whether two arrays are the same type taking into account
- * multi-dimensional arrays.
+ * <p>Checks if the value is in the given array.
*
- * @param array1 the first array, must not be {@code null}
- * @param array2 the second array, must not be {@code null}
- * @return {@code true} if type of arrays matches
- * @throws IllegalArgumentException if either array is {@code null}
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
*/
- public static boolean isSameType(final Object array1, final Object array2) {
- if (array1 == null || array2 == null) {
- throw new IllegalArgumentException("The Array must not be null");
- }
- return array1.getClass().getName().equals(array2.getClass().getName());
+ public static boolean contains(final float[] array, final float valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
- // Reverse
- //-----------------------------------------------------------------------
/**
- * <p>Reverses the order of the given array.
- *
- * <p>There is no special handling for multi-dimensional arrays.
+ * <p>Checks if the value is in the given array.
*
- * <p>This method does nothing for a {@code null} input array.
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
*
- * @param array the array to reverse, may be {@code null}
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
*/
- public static void reverse(final Object[] array) {
- if (array == null) {
- return;
- }
- reverse(array, 0, array.length);
+ public static boolean contains(final int[] array, final int valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
/**
- * <p>Reverses the order of the given array.
+ * <p>Checks if the value is in the given array.
*
- * <p>This method does nothing for a {@code null} input array.
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
*
- * @param array the array to reverse, may be {@code null}
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
*/
- public static void reverse(final long[] array) {
- if (array == null) {
- return;
- }
- reverse(array, 0, array.length);
+ public static boolean contains(final long[] array, final long valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
/**
- * <p>Reverses the order of the given array.
+ * <p>Checks if the object is in the given array.
*
- * <p>This method does nothing for a {@code null} input array.
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
*
- * @param array the array to reverse, may be {@code null}
+ * @param array the array to search through
+ * @param objectToFind the object to find
+ * @return {@code true} if the array contains the object
*/
- public static void reverse(final int[] array) {
- if (array == null) {
- return;
- }
- reverse(array, 0, array.length);
+ public static boolean contains(final Object[] array, final Object objectToFind) {
+ return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
}
/**
- * <p>Reverses the order of the given array.
+ * <p>Checks if the value is in the given array.
*
- * <p>This method does nothing for a {@code null} input array.
+ * <p>The method returns {@code false} if a {@code null} array is passed in.
*
- * @param array the array to reverse, may be {@code null}
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
*/
- public static void reverse(final short[] array) {
- if (array == null) {
- return;
- }
- reverse(array, 0, array.length);
+ public static boolean contains(final short[] array, final short valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
/**
- * <p>Reverses the order of the given array.
- *
- * <p>This method does nothing for a {@code null} input array.
+ * Returns a copy of the given array of size 1 greater than the argument.
+ * The last value of the array is left to the default value.
*
- * @param array the array to reverse, may be {@code null}
+ * @param array The array to copy, must not be {@code null}.
+ * @param newArrayComponentType If {@code array} is {@code null}, create a
+ * size 1 array of this type.
+ * @return A new copy of the array of size 1 greater than the input.
*/
- public static void reverse(final char[] array) {
- if (array == null) {
- return;
+ private static Object copyArrayGrow1(final Object array, final Class<?> newArrayComponentType) {
+ if (array != null) {
+ final int arrayLength = Array.getLength(array);
+ final Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);
+ System.arraycopy(array, 0, newArray, 0, arrayLength);
+ return newArray;
}
- reverse(array, 0, array.length);
+ return Array.newInstance(newArrayComponentType, 1);
}
+ //-----------------------------------------------------------------------
/**
- * <p>Reverses the order of the given array.
+ * <p>Returns the length of the specified array.
+ * This method can deal with {@code Object} arrays and with primitive arrays.
*
- * <p>This method does nothing for a {@code null} input array.
+ * <p>If the input array is {@code null}, {@code 0} is returned.
*
- * @param array the array to reverse, may be {@code null}
+ * <pre>
+ * ArrayUtils.getLength(null) = 0
+ * ArrayUtils.getLength([]) = 0
+ * ArrayUtils.getLength([null]) = 1
+ * ArrayUtils.getLength([true, false]) = 2
+ * ArrayUtils.getLength([1, 2, 3]) = 3
+ * ArrayUtils.getLength(["a", "b", "c"]) = 3
+ * </pre>
+ *
+ * @param array the array to retrieve the length from, may be null
+ * @return The length of the array, or {@code 0} if the array is {@code null}
+ * @throws IllegalArgumentException if the object argument is not an array.
+ * @since 2.1
*/
- public static void reverse(final byte[] array) {
+ public static int getLength(final Object array) {
if (array == null) {
- return;
+ return 0;
}
- reverse(array, 0, array.length);
+ return Array.getLength(array);
}
/**
- * <p>Reverses the order of the given array.
+ * <p>Get a hash code for an array handling multi-dimensional arrays correctly.
*
- * <p>This method does nothing for a {@code null} input array.
+ * <p>Multi-dimensional primitive arrays are also handled correctly by this method.
*
- * @param array the array to reverse, may be {@code null}
+ * @param array the array to get a hash code for, {@code null} returns zero
+ * @return a hash code for the array
*/
- public static void reverse(final double[] array) {
- if (array == null) {
- return;
- }
- reverse(array, 0, array.length);
+ public static int hashCode(final Object array) {
+ return new HashCodeBuilder().append(array).toHashCode();
}
/**
- * <p>Reverses the order of the given array.
+ * Finds the indices of the given value in the array.
*
- * <p>This method does nothing for a {@code null} input array.
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array the array to reverse, may be {@code null}
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return a BitSet of all the the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final float[] array) {
- if (array == null) {
- return;
- }
- reverse(array, 0, array.length);
+ public static BitSet indexesOf(final boolean[] array, final boolean valueToFind) {
+ return indexesOf(array, valueToFind, 0);
}
/**
- * <p>Reverses the order of the given array.
+ * Finds the indices of the given value in the array starting at the given index.
*
- * <p>This method does nothing for a {@code null} input array.
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array the array to reverse, may be {@code null}
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet ({@code -1}).</p>
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null}
+ * array input
+ * @since 3.10
*/
- public static void reverse(final boolean[] array) {
+ public static BitSet indexesOf(final boolean[] array, final boolean valueToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
if (array == null) {
- return;
+ return bitSet;
}
- reverse(array, 0, array.length);
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
+ }
+
+ return bitSet;
}
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value in the array.
*
- * <p>
- * This method does nothing for a {@code null} input array.
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @since 3.2
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final boolean[] array, final int startIndexInclusive, final int endIndexExclusive) {
- if (array == null) {
- return;
- }
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- boolean tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
- }
+ public static BitSet indexesOf(final byte[] array, final byte valueToFind) {
+ return indexesOf(array, valueToFind, 0);
}
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value in the array starting at the given index.
*
- * <p>
- * This method does nothing for a {@code null} input array.
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @since 3.2
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.</p>
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final byte[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ public static BitSet indexesOf(final byte[] array, final byte valueToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
if (array == null) {
- return;
- }
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- byte tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
+ return bitSet;
}
- }
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
+ }
+
+ return bitSet;
+ }
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value in the array.
*
- * <p>
- * This method does nothing for a {@code null} input array.
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @since 3.2
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final char[] array, final int startIndexInclusive, final int endIndexExclusive) {
- if (array == null) {
- return;
- }
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- char tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
- }
+ public static BitSet indexesOf(final char[] array, final char valueToFind) {
+ return indexesOf(array, valueToFind, 0);
}
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value in the array starting at the given index.
*
- * <p>
- * This method does nothing for a {@code null} input array.
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @since 3.2
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.</p>
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final double[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ public static BitSet indexesOf(final char[] array, final char valueToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
if (array == null) {
- return;
+ return bitSet;
}
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- double tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
}
+
+ return bitSet;
}
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value in the array.
*
- * <p>
- * This method does nothing for a {@code null} input array.
+ * <p>This method returns empty BitSet for a {@code null} input array.</p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @since 3.2
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final float[] array, final int startIndexInclusive, final int endIndexExclusive) {
- if (array == null) {
- return;
- }
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- float tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
- }
+ public static BitSet indexesOf(final double[] array, final double valueToFind) {
+ return indexesOf(array, valueToFind, 0);
}
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value within a given tolerance in the array.
*
* <p>
- * This method does nothing for a {@code null} input array.
+ * This method will return all the indices of the value which fall between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance, each time between the nearest integers.
+ * </p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @since 3.2
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param tolerance tolerance of the search
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final int[] array, final int startIndexInclusive, final int endIndexExclusive) {
- if (array == null) {
- return;
- }
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- int tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
- }
+ public static BitSet indexesOf(final double[] array, final double valueToFind, final double tolerance) {
+ return indexesOf(array, valueToFind, 0, tolerance);
}
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value in the array starting at the given index.
*
- * <p>
- * This method does nothing for a {@code null} input array.
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @since 3.2
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.</p>
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return a BitSet of the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final long[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ public static BitSet indexesOf(final double[] array, final double valueToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
if (array == null) {
- return;
+ return bitSet;
}
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- long tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
}
+
+ return bitSet;
}
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value in the array starting at the given index.
*
* <p>
- * This method does nothing for a {@code null} input array.
+ * This method will return the indices of the values which fall between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance, between the nearest integers.
+ * </p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Under value (<0) is promoted to 0, over value (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Under value (< start index) results in no
- * change. Over value (>array.length) is demoted to array length.
- * @since 3.2
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ *
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.</p>
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @param tolerance tolerance of the search
+ * @return a BitSet of the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final Object[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ public static BitSet indexesOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) {
+ BitSet bitSet = new BitSet();
+
if (array == null) {
- return;
+ return bitSet;
}
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- Object tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex, tolerance);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
}
+
+ return bitSet;
}
/**
- * <p>
- * Reverses the order of the given array in the given range.
+ * Finds the indices of the given value in the array.
*
- * <p>
- * This method does nothing for a {@code null} input array.
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array
- * the array to reverse, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @since 3.2
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void reverse(final short[] array, final int startIndexInclusive, final int endIndexExclusive) {
- if (array == null) {
- return;
- }
- int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
- int j = Math.min(array.length, endIndexExclusive) - 1;
- short tmp;
- while (j > i) {
- tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- j--;
- i++;
- }
+ public static BitSet indexesOf(final float[] array, final float valueToFind) {
+ return indexesOf(array, valueToFind, 0);
}
- // Swap
- //-----------------------------------------------------------------------
/**
- * Swaps two elements in the given array.
+ * Finds the indices of the given value in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap(["1", "2", "3"], 0, 2) -> ["3", "2", "1"]</li>
- * <li>ArrayUtils.swap(["1", "2", "3"], 0, 0) -> ["1", "2", "3"]</li>
- * <li>ArrayUtils.swap(["1", "2", "3"], 1, 0) -> ["2", "1", "3"]</li>
- * <li>ArrayUtils.swap(["1", "2", "3"], 0, 5) -> ["1", "2", "3"]</li>
- * <li>ArrayUtils.swap(["1", "2", "3"], -1, 1) -> ["2", "1", "3"]</li>
- * </ul>
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return empty BitSet.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final Object[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
+ public static BitSet indexesOf(final float[] array, final float valueToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
+ if (array == null) {
+ return bitSet;
}
- swap(array, offset1, offset2, 1);
- }
- /**
- * Swaps two elements in the given long array.
- *
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
- *
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([true, false, true], 0, 2) -> [true, false, true]</li>
- * <li>ArrayUtils.swap([true, false, true], 0, 0) -> [true, false, true]</li>
- * <li>ArrayUtils.swap([true, false, true], 1, 0) -> [false, true, true]</li>
- * <li>ArrayUtils.swap([true, false, true], 0, 5) -> [true, false, true]</li>
- * <li>ArrayUtils.swap([true, false, true], -1, 1) -> [false, true, true]</li>
- * </ul>
- *
- *
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
- */
- public static void swap(final long[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
}
- swap(array, offset1, offset2, 1);
+
+ return bitSet;
}
/**
- * Swaps two elements in the given int array.
- *
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
+ * Finds the indices of the given value in the array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
- * </ul>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final int[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
- }
- swap(array, offset1, offset2, 1);
+ public static BitSet indexesOf(final int[] array, final int valueToFind) {
+ return indexesOf(array, valueToFind, 0);
}
/**
- * Swaps two elements in the given short array.
+ * Finds the indices of the given value in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
- * </ul>
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final short[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
+ public static BitSet indexesOf(final int[] array, final int valueToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
+ if (array == null) {
+ return bitSet;
}
- swap(array, offset1, offset2, 1);
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
+ }
+
+ return bitSet;
}
/**
- * Swaps two elements in the given char array.
- *
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
+ * Finds the indices of the given value in the array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
- * </ul>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final char[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
- }
- swap(array, offset1, offset2, 1);
+ public static BitSet indexesOf(final long[] array, final long valueToFind) {
+ return indexesOf(array, valueToFind, 0);
}
/**
- * Swaps two elements in the given byte array.
+ * Finds the indices of the given value in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
- * </ul>
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final byte[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
+ public static BitSet indexesOf(final long[] array, final long valueToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
+ if (array == null) {
+ return bitSet;
}
- swap(array, offset1, offset2, 1);
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
+ }
+
+ return bitSet;
}
/**
- * Swaps two elements in the given double array.
- *
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
+ * Finds the indices of the given object in the array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
- * </ul>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @return a BitSet of all the indices of the object within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final double[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
- }
- swap(array, offset1, offset2, 1);
+ public static BitSet indexesOf(final Object[] array, final Object objectToFind) {
+ return indexesOf(array, objectToFind, 0);
}
/**
- * Swaps two elements in the given float array.
+ * Finds the indices of the given object in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
- * </ul>
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @param startIndex the index to start searching at
+ * @return a BitSet of all the indices of the object within the array starting at the index,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final float[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
+ public static BitSet indexesOf(final Object[] array, final Object objectToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
+ if (array == null) {
+ return bitSet;
}
- swap(array, offset1, offset2, 1);
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, objectToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
+ }
+
+ return bitSet;
}
/**
- * Swaps two elements in the given boolean array.
- *
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for a {@code null} or empty input array or for overflow indices.
- * Negative indices are promoted to 0(zero).</p>
+ * Finds the indices of the given value in the array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
- * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
- * </ul>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element to swap
- * @param offset2 the index of the second element to swap
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final boolean[] array, final int offset1, final int offset2) {
- if (isEmpty(array)) {
- return;
- }
- swap(array, offset1, offset2, 1);
+ public static BitSet indexesOf(final short[] array, final short valueToFind) {
+ return indexesOf(array, valueToFind, 0);
}
/**
- * Swaps a series of elements in the given boolean array.
+ * Finds the indices of the given value in the array starting at the given index.
*
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
+ * <p>This method returns an empty BitSet for a {@code null} input array.</p>
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([true, false, true, false], 0, 2, 1) -> [true, false, true, false]</li>
- * <li>ArrayUtils.swap([true, false, true, false], 0, 0, 1) -> [true, false, true, false]</li>
- * <li>ArrayUtils.swap([true, false, true, false], 0, 2, 2) -> [true, false, true, false]</li>
- * <li>ArrayUtils.swap([true, false, true, false], -3, 2, 2) -> [true, false, true, false]</li>
- * <li>ArrayUtils.swap([true, false, true, false], 0, 3, 3) -> [false, false, true, true]</li>
- * </ul>
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.</p>
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return a BitSet of all the indices of the value within the array,
+ * an empty BitSet if not found or {@code null} array input
+ * @since 3.10
*/
- public static void swap(final boolean[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
- }
- if (offset2 < 0) {
- offset2 = 0;
+ public static BitSet indexesOf(final short[] array, final short valueToFind, int startIndex) {
+ BitSet bitSet = new BitSet();
+
+ if (array == null) {
+ return bitSet;
}
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final boolean aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
+
+ while (startIndex < array.length) {
+ startIndex = indexOf(array, valueToFind, startIndex);
+
+ if (startIndex == INDEX_NOT_FOUND) {
+ break;
+ }
+
+ bitSet.set(startIndex);
+ ++startIndex;
}
+
+ return bitSet;
}
+ // boolean IndexOf
+ //-----------------------------------------------------------------------
/**
- * Swaps a series of elements in the given byte array.
- *
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
+ * <p>Finds the index of the given value in the array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
- * </ul>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void swap(final byte[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
- }
- if (offset2 < 0) {
- offset2 = 0;
- }
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final byte aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
- }
+ public static int indexOf(final boolean[] array, final boolean valueToFind) {
+ return indexOf(array, valueToFind, 0);
}
/**
- * Swaps a series of elements in the given char array.
+ * <p>Finds the index of the given value in the array starting at the given index.
*
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
- * </ul>
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null}
+ * array input
*/
- public static void swap(final char[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
+ public static int indexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
}
- if (offset2 < 0) {
- offset2 = 0;
+ if (startIndex < 0) {
+ startIndex = 0;
}
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final char aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
}
+ return INDEX_NOT_FOUND;
}
+ // byte IndexOf
+ //-----------------------------------------------------------------------
/**
- * Swaps a series of elements in the given double array.
- *
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
+ * <p>Finds the index of the given value in the array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
- * </ul>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void swap(final double[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
- }
- if (offset2 < 0) {
- offset2 = 0;
- }
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final double aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
- }
+ public static int indexOf(final byte[] array, final byte valueToFind) {
+ return indexOf(array, valueToFind, 0);
}
/**
- * Swaps a series of elements in the given float array.
+ * <p>Finds the index of the given value in the array starting at the given index.
*
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
- * </ul>
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void swap(final float[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
+ public static int indexOf(final byte[] array, final byte valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
}
- if (offset2 < 0) {
- offset2 = 0;
+ if (startIndex < 0) {
+ startIndex = 0;
}
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final float aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
}
-
+ return INDEX_NOT_FOUND;
}
+ // char IndexOf
+ //-----------------------------------------------------------------------
/**
- * Swaps a series of elements in the given int array.
- *
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
+ * <p>Finds the index of the given value in the array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
- * </ul>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @since 2.1
*/
- public static void swap(final int[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
- }
- if (offset2 < 0) {
- offset2 = 0;
- }
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final int aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
- }
+ public static int indexOf(final char[] array, final char valueToFind) {
+ return indexOf(array, valueToFind, 0);
}
/**
- * Swaps a series of elements in the given long array.
+ * <p>Finds the index of the given value in the array starting at the given index.
*
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
- * </ul>
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @since 2.1
*/
- public static void swap(final long[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
+ public static int indexOf(final char[] array, final char valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
}
- if (offset2 < 0) {
- offset2 = 0;
+ if (startIndex < 0) {
+ startIndex = 0;
}
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final long aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
}
+ return INDEX_NOT_FOUND;
}
+ // double IndexOf
+ //-----------------------------------------------------------------------
/**
- * Swaps a series of elements in the given array.
+ * <p>Finds the index of the given value in the array.
*
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
- *
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 2, 1) -> ["3", "2", "1", "4"]</li>
- * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 0, 1) -> ["1", "2", "3", "4"]</li>
- * <li>ArrayUtils.swap(["1", "2", "3", "4"], 2, 0, 2) -> ["3", "4", "1", "2"]</li>
- * <li>ArrayUtils.swap(["1", "2", "3", "4"], -3, 2, 2) -> ["3", "4", "1", "2"]</li>
- * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 3, 3) -> ["4", "2", "3", "1"]</li>
- * </ul>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void swap(final Object[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
- }
- if (offset2 < 0) {
- offset2 = 0;
- }
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final Object aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
- }
- }
-
- /**
- * Swaps a series of elements in the given short array.
- *
- * <p>This method does nothing for a {@code null} or empty input array or
- * for overflow indices. Negative indices are promoted to 0(zero). If any
- * of the sub-arrays to swap falls outside of the given array, then the
- * swap is stopped at the end of the array and as many as possible elements
- * are swapped.</p>
- *
- * Examples:
- * <ul>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
- * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
- * </ul>
- *
- * @param array the array to swap, may be {@code null}
- * @param offset1 the index of the first element in the series to swap
- * @param offset2 the index of the second element in the series to swap
- * @param len the number of elements to swap starting with the given indices
- * @since 3.5
- */
- public static void swap(final short[] array, int offset1, int offset2, int len) {
- if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
- return;
- }
- if (offset1 < 0) {
- offset1 = 0;
- }
- if (offset2 < 0) {
- offset2 = 0;
- }
- if (offset1 == offset2) {
- return;
- }
- len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
- for (int i = 0; i < len; i++, offset1++, offset2++) {
- final short aux = array[offset1];
- array[offset1] = array[offset2];
- array[offset2] = aux;
- }
+ public static int indexOf(final double[] array, final double valueToFind) {
+ return indexOf(array, valueToFind, 0);
}
- // Shift
- //-----------------------------------------------------------------------
/**
- * Shifts the order of the given array.
+ * <p>Finds the index of the given value within a given tolerance in the array.
+ * This method will return the index of the first value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param tolerance tolerance of the search
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final Object[] array, final int offset) {
- if (array == null) {
- return;
- }
- shift(array, 0, array.length, offset);
+ public static int indexOf(final double[] array, final double valueToFind, final double tolerance) {
+ return indexOf(array, valueToFind, 0, tolerance);
}
/**
- * Shifts the order of the given long array.
+ * <p>Finds the index of the given value in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final long[] array, final int offset) {
- if (array == null) {
- return;
+ public static int indexOf(final double[] array, final double valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
}
- shift(array, 0, array.length, offset);
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
}
/**
- * Shifts the order of the given int array.
+ * <p>Finds the index of the given value in the array starting at the given index.
+ * This method will return the index of the first value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @param tolerance tolerance of the search
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final int[] array, final int offset) {
- if (array == null) {
- return;
+ public static int indexOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
}
- shift(array, 0, array.length, offset);
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ final double min = valueToFind - tolerance;
+ final double max = valueToFind + tolerance;
+ for (int i = startIndex; i < array.length; i++) {
+ if (array[i] >= min && array[i] <= max) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
}
+ // float IndexOf
+ //-----------------------------------------------------------------------
/**
- * Shifts the order of the given short array.
+ * <p>Finds the index of the given value in the array.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final short[] array, final int offset) {
- if (array == null) {
- return;
- }
- shift(array, 0, array.length, offset);
+ public static int indexOf(final float[] array, final float valueToFind) {
+ return indexOf(array, valueToFind, 0);
}
/**
- * Shifts the order of the given char array.
+ * <p>Finds the index of the given value in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final char[] array, final int offset) {
- if (array == null) {
- return;
+ public static int indexOf(final float[] array, final float valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
}
- shift(array, 0, array.length, offset);
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
}
+ // int IndexOf
+//-----------------------------------------------------------------------
+/**
+ * <p>Finds the index of the given value in the array.
+ *
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+public static int indexOf(final int[] array, final int valueToFind) {
+ return indexOf(array, valueToFind, 0);
+}
+
/**
- * Shifts the order of the given byte array.
+ * <p>Finds the index of the given value in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final byte[] array, final int offset) {
+ public static int indexOf(final int[] array, final int valueToFind, int startIndex) {
if (array == null) {
- return;
+ return INDEX_NOT_FOUND;
}
- shift(array, 0, array.length, offset);
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
}
+ // long IndexOf
+ //-----------------------------------------------------------------------
/**
- * Shifts the order of the given double array.
+ * <p>Finds the index of the given value in the array.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final double[] array, final int offset) {
- if (array == null) {
- return;
- }
- shift(array, 0, array.length, offset);
+ public static int indexOf(final long[] array, final long valueToFind) {
+ return indexOf(array, valueToFind, 0);
}
/**
- * Shifts the order of the given float array.
+ * <p>Finds the index of the given value in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final float[] array, final int offset) {
+ public static int indexOf(final long[] array, final long valueToFind, int startIndex) {
if (array == null) {
- return;
+ return INDEX_NOT_FOUND;
}
- shift(array, 0, array.length, offset);
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
}
+ // Object IndexOf
+ //-----------------------------------------------------------------------
/**
- * Shifts the order of the given boolean array.
+ * <p>Finds the index of the given object in the array.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to shift, may be {@code null}
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @return the index of the object within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final boolean[] array, final int offset) {
- if (array == null) {
- return;
- }
- shift(array, 0, array.length, offset);
+ public static int indexOf(final Object[] array, final Object objectToFind) {
+ return indexOf(array, objectToFind, 0);
}
/**
- * Shifts the order of a series of elements in the given boolean array.
+ * <p>Finds the index of the given object in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @param startIndex the index to start searching at
+ * @return the index of the object within the array starting at the index,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final boolean[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
+ public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
- }
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
+ return INDEX_NOT_FOUND;
}
- offset %= n;
- if (offset < 0) {
- offset += n;
+ if (startIndex < 0) {
+ startIndex = 0;
}
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
-
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
+ if (objectToFind == null) {
+ for (int i = startIndex; i < array.length; i++) {
+ if (array[i] == null) {
+ return i;
+ }
+ }
+ } else {
+ for (int i = startIndex; i < array.length; i++) {
+ if (objectToFind.equals(array[i])) {
+ return i;
+ }
}
}
+ return INDEX_NOT_FOUND;
}
+ // short IndexOf
+ //-----------------------------------------------------------------------
/**
- * Shifts the order of a series of elements in the given byte array.
+ * <p>Finds the index of the given value in the array.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final byte[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
- if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
- }
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
- }
- offset %= n;
- if (offset < 0) {
- offset += n;
- }
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
-
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
- }
- }
+ public static int indexOf(final short[] array, final short valueToFind) {
+ return indexOf(array, valueToFind, 0);
}
/**
- * Shifts the order of a series of elements in the given char array.
+ * <p>Finds the index of the given value in the array starting at the given index.
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static void shift(final char[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
+ public static int indexOf(final short[] array, final short valueToFind, int startIndex) {
if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
+ return INDEX_NOT_FOUND;
}
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
+ if (startIndex < 0) {
+ startIndex = 0;
}
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
- }
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
- }
- offset %= n;
- if (offset < 0) {
- offset += n;
- }
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
-
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
- }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
}
+ return INDEX_NOT_FOUND;
}
/**
- * Shifts the order of a series of elements in the given double array.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>When an array is returned, it is always a new array.</p>
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static void shift(final double[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
+ public static boolean[] insert(final int index, final boolean[] array, final boolean... values) {
if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
+ return null;
}
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
}
- offset %= n;
- if (offset < 0) {
- offset += n;
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
}
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
- }
+ final boolean[] result = new boolean[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
}
+ return result;
}
/**
- * Shifts the order of a series of elements in the given float array.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>When an array is returned, it is always a new array.</p>
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static void shift(final float[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
+ public static byte[] insert(final int index, final byte[] array, final byte... values) {
if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
+ return null;
}
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
}
- offset %= n;
- if (offset < 0) {
- offset += n;
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
}
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
- }
+ final byte[] result = new byte[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
}
+ return result;
}
/**
- * Shifts the order of a series of elements in the given int array.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>When an array is returned, it is always a new array.</p>
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static void shift(final int[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
+ public static char[] insert(final int index, final char[] array, final char... values) {
if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
+ return null;
}
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
}
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
}
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
+
+ final char[] result = new char[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
}
- offset %= n;
- if (offset < 0) {
- offset += n;
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
}
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
-
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
- }
- }
- }
+ return result;
+ }
/**
- * Shifts the order of a series of elements in the given long array.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>When an array is returned, it is always a new array.</p>
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static void shift(final long[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
+ public static double[] insert(final int index, final double[] array, final double... values) {
if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
+ return null;
}
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
}
- offset %= n;
- if (offset < 0) {
- offset += n;
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
}
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
- }
+ final double[] result = new double[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
}
+ return result;
}
/**
- * Shifts the order of a series of elements in the given array.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>When an array is returned, it is always a new array.</p>
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static void shift(final Object[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
+ public static float[] insert(final int index, final float[] array, final float... values) {
if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
+ return null;
}
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
}
- offset %= n;
- if (offset < 0) {
- offset += n;
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
}
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
- }
+ final float[] result = new float[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
}
+ return result;
}
/**
- * Shifts the order of a series of elements in the given short array.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>There is no special handling for multi-dimensional arrays. This method
- * does nothing for {@code null} or empty input arrays.</p>
+ * <p>When an array is returned, it is always a new array.</p>
*
- * @param array
- * the array to shift, may be {@code null}
- * @param startIndexInclusive
- * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
- * change.
- * @param endIndexExclusive
- * elements up to endIndex-1 are shifted in the array. Undervalue (< start index) results in no
- * change. Overvalue (>array.length) is demoted to array length.
- * @param offset
- * The number of positions to rotate the elements. If the offset is larger than the number of elements to
- * rotate, than the effective offset is modulo the number of elements to rotate.
- * @since 3.5
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static void shift(final short[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
+ public static int[] insert(final int index, final int[] array, final int... values) {
if (array == null) {
- return;
- }
- if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
- return;
- }
- if (startIndexInclusive < 0) {
- startIndexInclusive = 0;
- }
- if (endIndexExclusive >= array.length) {
- endIndexExclusive = array.length;
+ return null;
}
- int n = endIndexExclusive - startIndexInclusive;
- if (n <= 1) {
- return;
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
}
- offset %= n;
- if (offset < 0) {
- offset += n;
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
}
- // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
- // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
- while (n > 1 && offset > 0) {
- final int n_offset = n - offset;
- if (offset > n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
- n = offset;
- offset -= n_offset;
- } else if (offset < n_offset) {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- startIndexInclusive += offset;
- n = n_offset;
- } else {
- swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
- break;
- }
+ final int[] result = new int[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
}
+ return result;
}
- // IndexOf search
- // ----------------------------------------------------------------------
-
- // Object IndexOf
- //-----------------------------------------------------------------------
/**
- * <p>Finds the index of the given object in the array.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>When an array is returned, it is always a new array.</p>
*
- * @param array the array to search through for the object, may be {@code null}
- * @param objectToFind the object to find, may be {@code null}
- * @return the index of the object within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
- */
- public static int indexOf(final Object[] array, final Object objectToFind) {
- return indexOf(array, objectToFind, 0);
- }
-
- /**
- * <p>Finds the index of the given object in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
*
- * @param array the array to search through for the object, may be {@code null}
- * @param objectToFind the object to find, may be {@code null}
- * @param startIndex the index to start searching at
- * @return the index of the object within the array starting at the index,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
+ public static long[] insert(final int index, final long[] array, final long... values) {
if (array == null) {
- return INDEX_NOT_FOUND;
+ return null;
}
- if (startIndex < 0) {
- startIndex = 0;
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
}
- if (objectToFind == null) {
- for (int i = startIndex; i < array.length; i++) {
- if (array[i] == null) {
- return i;
- }
- }
- } else {
- for (int i = startIndex; i < array.length; i++) {
- if (objectToFind.equals(array[i])) {
- return i;
- }
- }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
}
- return INDEX_NOT_FOUND;
+
+ final long[] result = new long[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
}
/**
- * Finds the indices of the given object in the array.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>When an array is returned, it is always a new array.</p>
*
- * @param array the array to search through for the object, may be {@code null}
- * @param objectToFind the object to find, may be {@code null}
- * @return a BitSet of all the indices of the object within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static BitSet indexesOf(final Object[] array, final Object objectToFind) {
- return indexesOf(array, objectToFind, 0);
+ public static short[] insert(final int index, final short[] array, final short... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final short[] result = new short[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
}
/**
- * Finds the indices of the given object in the array starting at the given index.
+ * <p>Inserts elements into an array at the given index (starting from zero).</p>
*
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>When an array is returned, it is always a new array.</p>
*
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet.</p>
+ * <pre>
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ * </pre>
*
- * @param array the array to search through for the object, may be {@code null}
- * @param objectToFind the object to find, may be {@code null}
- * @param startIndex the index to start searching at
- * @return a BitSet of all the indices of the object within the array starting at the index,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param <T> The type of elements in {@code array} and {@code values}
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
*/
- public static BitSet indexesOf(final Object[] array, final Object objectToFind, int startIndex) {
- BitSet bitSet = new BitSet();
+ @SafeVarargs
+ public static <T> T[] insert(final int index, final T[] array, final T... values) {
+ /*
+ * Note on use of @SafeVarargs:
+ *
+ * By returning null when 'array' is null, we avoid returning the vararg
+ * array to the caller. We also avoid relying on the type of the vararg
+ * array, by inspecting the component type of 'array'.
+ */
if (array == null) {
- return bitSet;
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
}
- while (startIndex < array.length) {
- startIndex = indexOf(array, objectToFind, startIndex);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
- }
+ final Class<?> type = array.getClass().getComponentType();
+ @SuppressWarnings("unchecked") // OK, because array and values are of type T
+ final
+ T[] result = (T[]) Array.newInstance(type, array.length + values.length);
- bitSet.set(startIndex);
- ++startIndex;
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
}
-
- return bitSet;
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
}
+ /**
+ * Returns whether a given array can safely be accessed at the given index.
+ * @param <T> the component type of the array
+ * @param array the array to inspect, may be null
+ * @param index the index of the array to be inspected
+ * @return Whether the given index is safely-accessible in the given array
+ * @since 3.8
+ */
+ public static <T> boolean isArrayIndexValid(T[] array, int index) {
+ if (getLength(array) == 0 || array.length <= index) {
+ return false;
+ }
+ return index >= 0;
+ }
/**
- * <p>Finds the last index of the given object within the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks if an array of primitive booleans is empty or {@code null}.
*
- * @param array the array to traverse backwards looking for the object, may be {@code null}
- * @param objectToFind the object to find, may be {@code null}
- * @return the last index of the object within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static int lastIndexOf(final Object[] array, final Object objectToFind) {
- return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
+ public static boolean isEmpty(final boolean[] array) {
+ return getLength(array) == 0;
}
+ // IndexOf search
+ // ----------------------------------------------------------------------
+
/**
- * <p>Finds the last index of the given object in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
- * the array length will search from the end of the array.
+ * <p>Checks if an array of primitive bytes is empty or {@code null}.
*
- * @param array the array to traverse for looking for the object, may be {@code null}
- * @param objectToFind the object to find, may be {@code null}
- * @param startIndex the start index to traverse backwards from
- * @return the last index of the object within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- return INDEX_NOT_FOUND;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
- if (objectToFind == null) {
- for (int i = startIndex; i >= 0; i--) {
- if (array[i] == null) {
- return i;
- }
- }
- } else if (array.getClass().getComponentType().isInstance(objectToFind)) {
- for (int i = startIndex; i >= 0; i--) {
- if (objectToFind.equals(array[i])) {
- return i;
- }
- }
- }
- return INDEX_NOT_FOUND;
+ public static boolean isEmpty(final byte[] array) {
+ return getLength(array) == 0;
}
/**
- * <p>Checks if the object is in the given array.
- *
- * <p>The method returns {@code false} if a {@code null} array is passed in.
+ * <p>Checks if an array of primitive chars is empty or {@code null}.
*
- * @param array the array to search through
- * @param objectToFind the object to find
- * @return {@code true} if the array contains the object
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static boolean contains(final Object[] array, final Object objectToFind) {
- return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
+ public static boolean isEmpty(final char[] array) {
+ return getLength(array) == 0;
}
- // long IndexOf
- //-----------------------------------------------------------------------
/**
- * <p>Finds the index of the given value in the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks if an array of primitive doubles is empty or {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static int indexOf(final long[] array, final long valueToFind) {
- return indexOf(array, valueToFind, 0);
+ public static boolean isEmpty(final double[] array) {
+ return getLength(array) == 0;
}
/**
- * <p>Finds the index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <p>Checks if an array of primitive floats is empty or {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static int indexOf(final long[] array, final long valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- startIndex = 0;
- }
- for (int i = startIndex; i < array.length; i++) {
- if (valueToFind == array[i]) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
+ public static boolean isEmpty(final float[] array) {
+ return getLength(array) == 0;
}
+
+
/**
- * Finds the indices of the given value in the array.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>Checks if an array of primitive ints is empty or {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static BitSet indexesOf(final long[] array, final long valueToFind) {
- return indexesOf(array, valueToFind, 0);
+ public static boolean isEmpty(final int[] array) {
+ return getLength(array) == 0;
}
/**
- * Finds the indices of the given value in the array starting at the given index.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet.</p>
+ * <p>Checks if an array of primitive longs is empty or {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static BitSet indexesOf(final long[] array, final long valueToFind, int startIndex) {
- BitSet bitSet = new BitSet();
-
- if (array == null) {
- return bitSet;
- }
-
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
- }
-
- bitSet.set(startIndex);
- ++startIndex;
- }
-
- return bitSet;
+ public static boolean isEmpty(final long[] array) {
+ return getLength(array) == 0;
}
+ // ----------------------------------------------------------------------
/**
- * <p>Finds the last index of the given value within the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks if an array of Objects is empty or {@code null}.
*
- * @param array the array to traverse backwards looking for the object, may be {@code null}
- * @param valueToFind the object to find
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static int lastIndexOf(final long[] array, final long valueToFind) {
- return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ public static boolean isEmpty(final Object[] array) {
+ return getLength(array) == 0;
}
/**
- * <p>Finds the last index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
- * array length will search from the end of the array.
+ * <p>Checks if an array of primitive shorts is empty or {@code null}.
*
- * @param array the array to traverse for looking for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the start index to traverse backwards from
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
*/
- public static int lastIndexOf(final long[] array, final long valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- return INDEX_NOT_FOUND;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
- for (int i = startIndex; i >= 0; i--) {
- if (valueToFind == array[i]) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
+ public static boolean isEmpty(final short[] array) {
+ return getLength(array) == 0;
}
/**
- * <p>Checks if the value is in the given array.
+ * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
+ * correctly.
*
- * <p>The method returns {@code false} if a {@code null} array is passed in.
+ * <p>Multi-dimensional primitive arrays are also handled correctly by this method.
*
- * @param array the array to search through
- * @param valueToFind the value to find
- * @return {@code true} if the array contains the object
+ * @param array1 the left hand array to compare, may be {@code null}
+ * @param array2 the right hand array to compare, may be {@code null}
+ * @return {@code true} if the arrays are equal
+ * @deprecated this method has been replaced by {@code java.util.Objects.deepEquals(Object, Object)} and will be
+ * removed from future releases.
*/
- public static boolean contains(final long[] array, final long valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ @Deprecated
+ public static boolean isEquals(final Object array1, final Object array2) {
+ return new EqualsBuilder().append(array1, array2).isEquals();
}
- // int IndexOf
- //-----------------------------------------------------------------------
/**
- * <p>Finds the index of the given value in the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks if an array of primitive booleans is not empty and not {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static int indexOf(final int[] array, final int valueToFind) {
- return indexOf(array, valueToFind, 0);
+ public static boolean isNotEmpty(final boolean[] array) {
+ return !isEmpty(array);
}
/**
- * <p>Finds the index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <p>Checks if an array of primitive bytes is not empty and not {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static int indexOf(final int[] array, final int valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- startIndex = 0;
- }
- for (int i = startIndex; i < array.length; i++) {
- if (valueToFind == array[i]) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
+ public static boolean isNotEmpty(final byte[] array) {
+ return !isEmpty(array);
}
/**
- * Finds the indices of the given value in the array.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>Checks if an array of primitive chars is not empty and not {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static BitSet indexesOf(final int[] array, final int valueToFind) {
- return indexesOf(array, valueToFind, 0);
+ public static boolean isNotEmpty(final char[] array) {
+ return !isEmpty(array);
}
/**
- * Finds the indices of the given value in the array starting at the given index.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet.</p>
+ * <p>Checks if an array of primitive doubles is not empty and not {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static BitSet indexesOf(final int[] array, final int valueToFind, int startIndex) {
- BitSet bitSet = new BitSet();
-
- if (array == null) {
- return bitSet;
- }
-
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
- }
-
- bitSet.set(startIndex);
- ++startIndex;
- }
-
- return bitSet;
- }
+ public static boolean isNotEmpty(final double[] array) {
+ return !isEmpty(array);
+ }
/**
- * <p>Finds the last index of the given value within the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks if an array of primitive floats is not empty and not {@code null}.
*
- * @param array the array to traverse backwards looking for the object, may be {@code null}
- * @param valueToFind the object to find
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static int lastIndexOf(final int[] array, final int valueToFind) {
- return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ public static boolean isNotEmpty(final float[] array) {
+ return !isEmpty(array);
}
/**
- * <p>Finds the last index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
- * array length will search from the end of the array.
+ * <p>Checks if an array of primitive ints is not empty and not {@code null}.
*
- * @param array the array to traverse for looking for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the start index to traverse backwards from
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static int lastIndexOf(final int[] array, final int valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- return INDEX_NOT_FOUND;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
- for (int i = startIndex; i >= 0; i--) {
- if (valueToFind == array[i]) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
+ public static boolean isNotEmpty(final int[] array) {
+ return !isEmpty(array);
}
/**
- * <p>Checks if the value is in the given array.
- *
- * <p>The method returns {@code false} if a {@code null} array is passed in.
+ * <p>Checks if an array of primitive longs is not empty and not {@code null}.
*
- * @param array the array to search through
- * @param valueToFind the value to find
- * @return {@code true} if the array contains the object
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static boolean contains(final int[] array, final int valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static boolean isNotEmpty(final long[] array) {
+ return !isEmpty(array);
}
- // short IndexOf
- //-----------------------------------------------------------------------
/**
- * <p>Finds the index of the given value in the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks if an array of primitive shorts is not empty and not {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static int indexOf(final short[] array, final short valueToFind) {
- return indexOf(array, valueToFind, 0);
+ public static boolean isNotEmpty(final short[] array) {
+ return !isEmpty(array);
}
+ // ----------------------------------------------------------------------
/**
- * <p>Finds the index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <p>Checks if an array of Objects is not empty and not {@code null}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param <T> the component type of the array
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
*/
- public static int indexOf(final short[] array, final short valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- startIndex = 0;
- }
- for (int i = startIndex; i < array.length; i++) {
- if (valueToFind == array[i]) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
- }
+ public static <T> boolean isNotEmpty(final T[] array) {
+ return !isEmpty(array);
+ }
/**
- * Finds the indices of the given value in the array.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static BitSet indexesOf(final short[] array, final short valueToFind) {
- return indexesOf(array, valueToFind, 0);
+ public static boolean isSameLength(final boolean[] array1, final boolean[] array2) {
+ return getLength(array1) == getLength(array2);
}
/**
- * Finds the indices of the given value in the array starting at the given index.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet.</p>
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static BitSet indexesOf(final short[] array, final short valueToFind, int startIndex) {
- BitSet bitSet = new BitSet();
-
- if (array == null) {
- return bitSet;
- }
-
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
- }
-
- bitSet.set(startIndex);
- ++startIndex;
- }
-
- return bitSet;
+ public static boolean isSameLength(final byte[] array1, final byte[] array2) {
+ return getLength(array1) == getLength(array2);
}
/**
- * <p>Finds the last index of the given value within the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * @param array the array to traverse backwards looking for the object, may be {@code null}
- * @param valueToFind the object to find
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static int lastIndexOf(final short[] array, final short valueToFind) {
- return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ public static boolean isSameLength(final char[] array1, final char[] array2) {
+ return getLength(array1) == getLength(array2);
}
/**
- * <p>Finds the last index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
- * array length will search from the end of the array.
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * @param array the array to traverse for looking for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the start index to traverse backwards from
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static int lastIndexOf(final short[] array, final short valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- return INDEX_NOT_FOUND;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
- }
- for (int i = startIndex; i >= 0; i--) {
- if (valueToFind == array[i]) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
+ public static boolean isSameLength(final double[] array1, final double[] array2) {
+ return getLength(array1) == getLength(array2);
}
/**
- * <p>Checks if the value is in the given array.
- *
- * <p>The method returns {@code false} if a {@code null} array is passed in.
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * @param array the array to search through
- * @param valueToFind the value to find
- * @return {@code true} if the array contains the object
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static boolean contains(final short[] array, final short valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static boolean isSameLength(final float[] array1, final float[] array2) {
+ return getLength(array1) == getLength(array2);
}
- // char IndexOf
- //-----------------------------------------------------------------------
/**
- * <p>Finds the index of the given value in the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
- * @since 2.1
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static int indexOf(final char[] array, final char valueToFind) {
- return indexOf(array, valueToFind, 0);
+ public static boolean isSameLength(final int[] array1, final int[] array2) {
+ return getLength(array1) == getLength(array2);
}
/**
- * <p>Finds the index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
- * @since 2.1
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static int indexOf(final char[] array, final char valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- startIndex = 0;
- }
- for (int i = startIndex; i < array.length; i++) {
- if (valueToFind == array[i]) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
+ public static boolean isSameLength(final long[] array1, final long[] array2) {
+ return getLength(array1) == getLength(array2);
}
+ // Is same length
+ //-----------------------------------------------------------------------
/**
- * Finds the indices of the given value in the array.
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>Any multi-dimensional aspects of the arrays are ignored.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static BitSet indexesOf(final char[] array, final char valueToFind) {
- return indexesOf(array, valueToFind, 0);
+ public static boolean isSameLength(final Object[] array1, final Object[] array2) {
+ return getLength(array1) == getLength(array2);
}
/**
- * Finds the indices of the given value in the array starting at the given index.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet.</p>
+ * <p>Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
*/
- public static BitSet indexesOf(final char[] array, final char valueToFind, int startIndex) {
- BitSet bitSet = new BitSet();
-
- if (array == null) {
- return bitSet;
- }
-
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
- }
-
- bitSet.set(startIndex);
- ++startIndex;
- }
-
- return bitSet;
+ public static boolean isSameLength(final short[] array1, final short[] array2) {
+ return getLength(array1) == getLength(array2);
}
/**
- * <p>Finds the last index of the given value within the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>Checks whether two arrays are the same type taking into account
+ * multi-dimensional arrays.
*
- * @param array the array to traverse backwards looking for the object, may be {@code null}
- * @param valueToFind the object to find
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
- * @since 2.1
+ * @param array1 the first array, must not be {@code null}
+ * @param array2 the second array, must not be {@code null}
+ * @return {@code true} if type of arrays matches
+ * @throws IllegalArgumentException if either array is {@code null}
*/
- public static int lastIndexOf(final char[] array, final char valueToFind) {
- return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ public static boolean isSameType(final Object array1, final Object array2) {
+ if (array1 == null || array2 == null) {
+ throw new IllegalArgumentException("The Array must not be null");
+ }
+ return array1.getClass().getName().equals(array2.getClass().getName());
}
/**
- * <p>Finds the last index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
- * array length will search from the end of the array.
+ * <p>This method checks whether the provided array is sorted according to natural ordering
+ * ({@code false} before {@code true}).
*
- * @param array the array to traverse for looking for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the start index to traverse backwards from
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
- * @since 2.1
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
*/
- public static int lastIndexOf(final char[] array, final char valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- return INDEX_NOT_FOUND;
- } else if (startIndex >= array.length) {
- startIndex = array.length - 1;
+ public static boolean isSorted(final boolean[] array) {
+ if (array == null || array.length < 2) {
+ return true;
}
- for (int i = startIndex; i >= 0; i--) {
- if (valueToFind == array[i]) {
- return i;
+
+ boolean previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final boolean current = array[i];
+ if (BooleanUtils.compare(previous, current) > 0) {
+ return false;
}
+
+ previous = current;
}
- return INDEX_NOT_FOUND;
+ return true;
}
/**
- * <p>Checks if the value is in the given array.
- *
- * <p>The method returns {@code false} if a {@code null} array is passed in.
+ * <p>This method checks whether the provided array is sorted according to natural ordering.
*
- * @param array the array to search through
- * @param valueToFind the value to find
- * @return {@code true} if the array contains the object
- * @since 2.1
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
*/
- public static boolean contains(final char[] array, final char valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
- }
+ public static boolean isSorted(final byte[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
- // byte IndexOf
- //-----------------------------------------------------------------------
- /**
- * <p>Finds the index of the given value in the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
- */
- public static int indexOf(final byte[] array, final byte valueToFind) {
- return indexOf(array, valueToFind, 0);
+ byte previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final byte current = array[i];
+ if (NumberUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
}
/**
- * <p>Finds the index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <p>This method checks whether the provided array is sorted according to natural ordering.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
*/
- public static int indexOf(final byte[] array, final byte valueToFind, int startIndex) {
- if (array == null) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- startIndex = 0;
+ public static boolean isSorted(final char[] array) {
+ if (array == null || array.length < 2) {
+ return true;
}
- for (int i = startIndex; i < array.length; i++) {
- if (valueToFind == array[i]) {
- return i;
+
+ char previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final char current = array[i];
+ if (CharUtils.compare(previous, current) > 0) {
+ return false;
}
+
+ previous = current;
}
- return INDEX_NOT_FOUND;
+ return true;
}
/**
- * Finds the indices of the given value in the array.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>This method checks whether the provided array is sorted according to natural ordering.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
*/
- public static BitSet indexesOf(final byte[] array, final byte valueToFind) {
- return indexesOf(array, valueToFind, 0);
+ public static boolean isSorted(final double[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ double previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final double current = array[i];
+ if (Double.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
}
/**
- * Finds the indices of the given value in the array starting at the given index.
+ * <p>This method checks whether the provided array is sorted according to natural ordering.
*
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final float[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ float previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final float current = array[i];
+ if (Float.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * <p>This method checks whether the provided array is sorted according to natural ordering.
*
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet.</p>
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final int[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ int previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final int current = array[i];
+ if (NumberUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * <p>This method checks whether the provided array is sorted according to natural ordering.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
*/
- public static BitSet indexesOf(final byte[] array, final byte valueToFind, int startIndex) {
- BitSet bitSet = new BitSet();
+ public static boolean isSorted(final long[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
- if (array == null) {
- return bitSet;
+ long previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final long current = array[i];
+ if (NumberUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
}
+ return true;
+ }
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex);
+ /**
+ * <p>This method checks whether the provided array is sorted according to natural ordering.
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final short[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
- if (startIndex == INDEX_NOT_FOUND) {
- break;
+ short previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final short current = array[i];
+ if (NumberUtils.compare(previous, current) > 0) {
+ return false;
}
- bitSet.set(startIndex);
- ++startIndex;
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * <p>This method checks whether the provided array is sorted according to the class's
+ * {@code compareTo} method.
+ *
+ * @param array the array to check
+ * @param <T> the datatype of the array to check, it must implement {@code Comparable}
+ * @return whether the array is sorted
+ * @since 3.4
+ */
+ public static <T extends Comparable<? super T>> boolean isSorted(final T[] array) {
+ return isSorted(array, (o1, o2) -> o1.compareTo(o2));
+ }
+
+ /**
+ * <p>This method checks whether the provided array is sorted according to the provided {@code Comparator}.
+ *
+ * @param array the array to check
+ * @param comparator the {@code Comparator} to compare over
+ * @param <T> the datatype of the array
+ * @return whether the array is sorted
+ * @since 3.4
+ */
+ public static <T> boolean isSorted(final T[] array, final Comparator<T> comparator) {
+ if (comparator == null) {
+ throw new IllegalArgumentException("Comparator should not be null.");
}
- return bitSet;
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ T previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final T current = array[i];
+ if (comparator.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
}
/**
* <p>Finds the last index of the given value within the array.
*
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) if
+ * {@code null} array input.
*
* @param array the array to traverse backwards looking for the object, may be {@code null}
* @param valueToFind the object to find
* @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int lastIndexOf(final byte[] array, final byte valueToFind) {
+ public static int lastIndexOf(final boolean[] array, final boolean valueToFind) {
return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
@@ -4109,8 +3690,8 @@ public class ArrayUtils {
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
- * array length will search from the end of the array.
+ * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
+ * the array length will search from the end of the array.
*
* @param array the array to traverse for looking for the object, may be {@code null}
* @param valueToFind the value to find
@@ -4118,8 +3699,8 @@ public class ArrayUtils {
* @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int lastIndexOf(final byte[] array, final byte valueToFind, int startIndex) {
- if (array == null) {
+ public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
+ if (isEmpty(array)) {
return INDEX_NOT_FOUND;
}
if (startIndex < 0) {
@@ -4136,73 +3717,43 @@ public class ArrayUtils {
}
/**
- * <p>Checks if the value is in the given array.
- *
- * <p>The method returns {@code false} if a {@code null} array is passed in.
- *
- * @param array the array to search through
- * @param valueToFind the value to find
- * @return {@code true} if the array contains the object
- */
- public static boolean contains(final byte[] array, final byte valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
- }
-
- // double IndexOf
- //-----------------------------------------------------------------------
- /**
- * <p>Finds the index of the given value in the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
- */
- public static int indexOf(final double[] array, final double valueToFind) {
- return indexOf(array, valueToFind, 0);
- }
-
- /**
- * <p>Finds the index of the given value within a given tolerance in the array.
- * This method will return the index of the first value which falls between the region
- * defined by valueToFind - tolerance and valueToFind + tolerance.
+ * <p>Finds the last index of the given value within the array.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param tolerance tolerance of the search
- * @return the index of the value within the array,
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int indexOf(final double[] array, final double valueToFind, final double tolerance) {
- return indexOf(array, valueToFind, 0, tolerance);
+ public static int lastIndexOf(final byte[] array, final byte valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
/**
- * <p>Finds the index of the given value in the array starting at the given index.
+ * <p>Finds the last index of the given value in the array starting at the given index.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
*
- * @param array the array to search through for the object, may be {@code null}
+ * @param array the array to traverse for looking for the object, may be {@code null}
* @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return the index of the value within the array,
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int indexOf(final double[] array, final double valueToFind, int startIndex) {
- if (isEmpty(array)) {
+ public static int lastIndexOf(final byte[] array, final byte valueToFind, int startIndex) {
+ if (array == null) {
return INDEX_NOT_FOUND;
}
if (startIndex < 0) {
- startIndex = 0;
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
}
- for (int i = startIndex; i < array.length; i++) {
+ for (int i = startIndex; i >= 0; i--) {
if (valueToFind == array[i]) {
return i;
}
@@ -4211,152 +3762,50 @@ public class ArrayUtils {
}
/**
- * <p>Finds the index of the given value in the array starting at the given index.
- * This method will return the index of the first value which falls between the region
- * defined by valueToFind - tolerance and valueToFind + tolerance.
+ * <p>Finds the last index of the given value within the array.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @param tolerance tolerance of the search
- * @return the index of the value within the array,
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @since 2.1
*/
- public static int indexOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) {
- if (isEmpty(array)) {
- return INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- startIndex = 0;
- }
- final double min = valueToFind - tolerance;
- final double max = valueToFind + tolerance;
- for (int i = startIndex; i < array.length; i++) {
- if (array[i] >= min && array[i] <= max) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
- }
-
- /**
- * Finds the indices of the given value in the array.
- *
- * <p>This method returns empty BitSet for a {@code null} input array.</p>
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
- */
- public static BitSet indexesOf(final double[] array, final double valueToFind) {
- return indexesOf(array, valueToFind, 0);
- }
-
- /**
- * Finds the indices of the given value within a given tolerance in the array.
- *
- * <p>
- * This method will return all the indices of the value which fall between the region
- * defined by valueToFind - tolerance and valueToFind + tolerance, each time between the nearest integers.
- * </p>
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param tolerance tolerance of the search
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
- */
- public static BitSet indexesOf(final double[] array, final double valueToFind, final double tolerance) {
- return indexesOf(array, valueToFind, 0, tolerance);
+ public static int lastIndexOf(final char[] array, final char valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
-
/**
- * Finds the indices of the given value in the array starting at the given index.
+ * <p>Finds the last index of the given value in the array starting at the given index.
*
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet.</p>
+ * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
*
- * @param array the array to search through for the object, may be {@code null}
+ * @param array the array to traverse for looking for the object, may be {@code null}
* @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return a BitSet of the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @since 2.1
*/
- public static BitSet indexesOf(final double[] array, final double valueToFind, int startIndex) {
- BitSet bitSet = new BitSet();
-
+ public static int lastIndexOf(final char[] array, final char valueToFind, int startIndex) {
if (array == null) {
- return bitSet;
- }
-
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
- }
-
- bitSet.set(startIndex);
- ++startIndex;
+ return INDEX_NOT_FOUND;
}
-
- return bitSet;
- }
-
- /**
- * Finds the indices of the given value in the array starting at the given index.
- *
- * <p>
- * This method will return the indices of the values which fall between the region
- * defined by valueToFind - tolerance and valueToFind + tolerance, between the nearest integers.
- * </p>
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet.</p>
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @param tolerance tolerance of the search
- * @return a BitSet of the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
- */
- public static BitSet indexesOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) {
- BitSet bitSet = new BitSet();
-
- if (array == null) {
- return bitSet;
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
}
-
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex, tolerance);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
}
-
- bitSet.set(startIndex);
- ++startIndex;
}
-
- return bitSet;
+ return INDEX_NOT_FOUND;
}
/**
@@ -4457,74 +3906,90 @@ public class ArrayUtils {
return INDEX_NOT_FOUND;
}
+
/**
- * <p>Checks if the value is in the given array.
+ * <p>Finds the last index of the given value within the array.
*
- * <p>The method returns {@code false} if a {@code null} array is passed in.
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to search through
- * @param valueToFind the value to find
- * @return {@code true} if the array contains the object
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static boolean contains(final double[] array, final double valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static int lastIndexOf(final float[] array, final float valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
/**
- * <p>Checks if a value falling within the given tolerance is in the
- * given array. If the array contains a value within the inclusive range
- * defined by (value - tolerance) to (value + tolerance).
+ * <p>Finds the last index of the given value in the array starting at the given index.
*
- * <p>The method returns {@code false} if a {@code null} array
- * is passed in.
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to search
+ * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
* @param valueToFind the value to find
- * @param tolerance the array contains the tolerance of the search
- * @return true if value falling within tolerance is in array
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static boolean contains(final double[] array, final double valueToFind, final double tolerance) {
- return indexOf(array, valueToFind, 0, tolerance) != INDEX_NOT_FOUND;
+ public static int lastIndexOf(final float[] array, final float valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
}
- // float IndexOf
- //-----------------------------------------------------------------------
/**
- * <p>Finds the index of the given value in the array.
+ * <p>Finds the last index of the given value within the array.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return the index of the value within the array,
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int indexOf(final float[] array, final float valueToFind) {
- return indexOf(array, valueToFind, 0);
+ public static int lastIndexOf(final int[] array, final int valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
/**
- * <p>Finds the index of the given value in the array starting at the given index.
+ * <p>Finds the last index of the given value in the array starting at the given index.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
*
- * @param array the array to search through for the object, may be {@code null}
+ * @param array the array to traverse for looking for the object, may be {@code null}
* @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return the index of the value within the array,
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int indexOf(final float[] array, final float valueToFind, int startIndex) {
- if (isEmpty(array)) {
+ public static int lastIndexOf(final int[] array, final int valueToFind, int startIndex) {
+ if (array == null) {
return INDEX_NOT_FOUND;
}
if (startIndex < 0) {
- startIndex = 0;
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
}
- for (int i = startIndex; i < array.length; i++) {
+ for (int i = startIndex; i >= 0; i--) {
if (valueToFind == array[i]) {
return i;
}
@@ -4533,74 +3998,23 @@ public class ArrayUtils {
}
/**
- * Finds the indices of the given value in the array.
+ * <p>Finds the last index of the given value within the array.
*
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static BitSet indexesOf(final float[] array, final float valueToFind) {
- return indexesOf(array, valueToFind, 0);
+ public static int lastIndexOf(final long[] array, final long valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
/**
- * Finds the indices of the given value in the array starting at the given index.
+ * <p>Finds the last index of the given value in the array starting at the given index.
*
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return empty BitSet.</p>
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
- */
- public static BitSet indexesOf(final float[] array, final float valueToFind, int startIndex) {
- BitSet bitSet = new BitSet();
-
- if (array == null) {
- return bitSet;
- }
-
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
- }
-
- bitSet.set(startIndex);
- ++startIndex;
- }
-
- return bitSet;
- }
-
- /**
- * <p>Finds the last index of the given value within the array.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
- *
- * @param array the array to traverse backwards looking for the object, may be {@code null}
- * @param valueToFind the object to find
- * @return the last index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
- */
- public static int lastIndexOf(final float[] array, final float valueToFind) {
- return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
- }
-
- /**
- * <p>Finds the last index of the given value in the array starting at the given index.
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
* <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
* array length will search from the end of the array.
@@ -4611,8 +4025,8 @@ public class ArrayUtils {
* @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int lastIndexOf(final float[] array, final float valueToFind, int startIndex) {
- if (isEmpty(array)) {
+ public static int lastIndexOf(final long[] array, final long valueToFind, int startIndex) {
+ if (array == null) {
return INDEX_NOT_FOUND;
}
if (startIndex < 0) {
@@ -4629,128 +4043,69 @@ public class ArrayUtils {
}
/**
- * <p>Checks if the value is in the given array.
- *
- * <p>The method returns {@code false} if a {@code null} array is passed in.
- *
- * @param array the array to search through
- * @param valueToFind the value to find
- * @return {@code true} if the array contains the object
- */
- public static boolean contains(final float[] array, final float valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
- }
-
- // boolean IndexOf
- //-----------------------------------------------------------------------
- /**
- * <p>Finds the index of the given value in the array.
+ * <p>Finds the last index of the given object within the array.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return the index of the value within the array,
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @return the last index of the object within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int indexOf(final boolean[] array, final boolean valueToFind) {
- return indexOf(array, valueToFind, 0);
+ public static int lastIndexOf(final Object[] array, final Object objectToFind) {
+ return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
}
/**
- * <p>Finds the index of the given value in the array starting at the given index.
+ * <p>Finds the last index of the given object in the array starting at the given index.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
+ * the array length will search from the end of the array.
*
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return the index of the value within the array,
- * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null}
- * array input
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the object within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int indexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
- if (isEmpty(array)) {
+ public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) {
+ if (array == null) {
return INDEX_NOT_FOUND;
}
if (startIndex < 0) {
- startIndex = 0;
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
}
- for (int i = startIndex; i < array.length; i++) {
- if (valueToFind == array[i]) {
- return i;
+ if (objectToFind == null) {
+ for (int i = startIndex; i >= 0; i--) {
+ if (array[i] == null) {
+ return i;
+ }
}
- }
- return INDEX_NOT_FOUND;
- }
-
- /**
- * Finds the indices of the given value in the array.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @return a BitSet of all the the indices of the value within the array,
- * an empty BitSet if not found or {@code null} array input
- * @since 3.10
- */
- public static BitSet indexesOf(final boolean[] array, final boolean valueToFind) {
- return indexesOf(array, valueToFind, 0);
- }
-
- /**
- * Finds the indices of the given value in the array starting at the given index.
- *
- * <p>This method returns an empty BitSet for a {@code null} input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return an empty BitSet ({@code -1}).</p>
- *
- * @param array the array to search through for the object, may be {@code null}
- * @param valueToFind the value to find
- * @param startIndex the index to start searching at
- * @return a BitSet of all the indices of the value within the array,
- * an empty BitSet if not found or {@code null}
- * array input
- * @since 3.10
- */
- public static BitSet indexesOf(final boolean[] array, final boolean valueToFind, int startIndex) {
- BitSet bitSet = new BitSet();
-
- if (array == null) {
- return bitSet;
- }
-
- while (startIndex < array.length) {
- startIndex = indexOf(array, valueToFind, startIndex);
-
- if (startIndex == INDEX_NOT_FOUND) {
- break;
+ } else if (array.getClass().getComponentType().isInstance(objectToFind)) {
+ for (int i = startIndex; i >= 0; i--) {
+ if (objectToFind.equals(array[i])) {
+ return i;
+ }
}
-
- bitSet.set(startIndex);
- ++startIndex;
}
-
- return bitSet;
+ return INDEX_NOT_FOUND;
}
/**
* <p>Finds the last index of the given value within the array.
*
- * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) if
- * {@code null} array input.
+ * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
* @param array the array to traverse backwards looking for the object, may be {@code null}
* @param valueToFind the object to find
* @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int lastIndexOf(final boolean[] array, final boolean valueToFind) {
+ public static int lastIndexOf(final short[] array, final short valueToFind) {
return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
@@ -4759,8 +4114,8 @@ public class ArrayUtils {
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
- * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
- * the array length will search from the end of the array.
+ * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
*
* @param array the array to traverse for looking for the object, may be {@code null}
* @param valueToFind the value to find
@@ -4768,8 +4123,8 @@ public class ArrayUtils {
* @return the last index of the value within the array,
* {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
*/
- public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
- if (isEmpty(array)) {
+ public static int lastIndexOf(final short[] array, final short valueToFind, int startIndex) {
+ if (array == null) {
return INDEX_NOT_FOUND;
}
if (startIndex < 0) {
@@ -4786,4598 +4141,5243 @@ public class ArrayUtils {
}
/**
- * <p>Checks if the value is in the given array.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>The method returns {@code false} if a {@code null} array is passed in.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array the array to search through
- * @param valueToFind the value to find
- * @return {@code true} if the array contains the object
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static boolean contains(final boolean[] array, final boolean valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static boolean[] nullToEmpty(final boolean[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_BOOLEAN_ARRAY;
+ }
+ return array;
}
- // Primitive/Object array converters
- // ----------------------------------------------------------------------
-
- // Character array converters
- // ----------------------------------------------------------------------
/**
- * <p>Converts an array of object Characters to primitives.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Character} array, may be {@code null}
- * @return a {@code char} array, {@code null} if null array input
- * @throws NullPointerException if array content is {@code null}
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static char[] toPrimitive(final Character[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_CHAR_ARRAY;
- }
- final char[] result = new char[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i].charValue();
+ public static Boolean[] nullToEmpty(final Boolean[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_BOOLEAN_OBJECT_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of object Character to primitives handling {@code null}.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Character} array, may be {@code null}
- * @param valueForNull the value to insert if {@code null} found
- * @return a {@code char} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static char[] toPrimitive(final Character[] array, final char valueForNull) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_CHAR_ARRAY;
- }
- final char[] result = new char[array.length];
- for (int i = 0; i < array.length; i++) {
- final Character b = array[i];
- result[i] = (b == null ? valueForNull : b.charValue());
+ public static byte[] nullToEmpty(final byte[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_BYTE_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of primitive chars to objects.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code char} array
- * @return a {@code Character} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static Character[] toObject(final char[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_CHARACTER_OBJECT_ARRAY;
- }
- final Character[] result = new Character[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = Character.valueOf(array[i]);
+ public static Byte[] nullToEmpty(final Byte[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_BYTE_OBJECT_ARRAY;
}
- return result;
- }
+ return array;
+ }
- // Long array converters
- // ----------------------------------------------------------------------
/**
- * <p>Converts an array of object Longs to primitives.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Long} array, may be {@code null}
- * @return a {@code long} array, {@code null} if null array input
- * @throws NullPointerException if array content is {@code null}
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static long[] toPrimitive(final Long[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_LONG_ARRAY;
- }
- final long[] result = new long[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i].longValue();
+ public static char[] nullToEmpty(final char[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_CHAR_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of object Long to primitives handling {@code null}.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Long} array, may be {@code null}
- * @param valueForNull the value to insert if {@code null} found
- * @return a {@code long} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static long[] toPrimitive(final Long[] array, final long valueForNull) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_LONG_ARRAY;
- }
- final long[] result = new long[array.length];
- for (int i = 0; i < array.length; i++) {
- final Long b = array[i];
- result[i] = (b == null ? valueForNull : b.longValue());
+ public static Character[] nullToEmpty(final Character[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_CHARACTER_OBJECT_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of primitive longs to objects.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code long} array
- * @return a {@code Long} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 3.2
*/
- public static Long[] toObject(final long[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_LONG_OBJECT_ARRAY;
- }
- final Long[] result = new Long[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = Long.valueOf(array[i]);
+ public static Class<?>[] nullToEmpty(final Class<?>[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_CLASS_ARRAY;
}
- return result;
+ return array;
}
- // Int array converters
- // ----------------------------------------------------------------------
/**
- * <p>Converts an array of object Integers to primitives.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Integer} array, may be {@code null}
- * @return an {@code int} array, {@code null} if null array input
- * @throws NullPointerException if array content is {@code null}
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static int[] toPrimitive(final Integer[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_INT_ARRAY;
- }
- final int[] result = new int[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i].intValue();
+ public static double[] nullToEmpty(final double[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_DOUBLE_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of object Integer to primitives handling {@code null}.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Integer} array, may be {@code null}
- * @param valueForNull the value to insert if {@code null} found
- * @return an {@code int} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static int[] toPrimitive(final Integer[] array, final int valueForNull) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_INT_ARRAY;
- }
- final int[] result = new int[array.length];
- for (int i = 0; i < array.length; i++) {
- final Integer b = array[i];
- result[i] = (b == null ? valueForNull : b.intValue());
+ public static Double[] nullToEmpty(final Double[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_DOUBLE_OBJECT_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of primitive ints to objects.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array an {@code int} array
- * @return an {@code Integer} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static Integer[] toObject(final int[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_INTEGER_OBJECT_ARRAY;
- }
- final Integer[] result = new Integer[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = Integer.valueOf(array[i]);
+ public static float[] nullToEmpty(final float[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_FLOAT_ARRAY;
}
- return result;
+ return array;
}
- // Short array converters
- // ----------------------------------------------------------------------
/**
- * <p>Converts an array of object Shorts to primitives.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Short} array, may be {@code null}
- * @return a {@code byte} array, {@code null} if null array input
- * @throws NullPointerException if array content is {@code null}
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static short[] toPrimitive(final Short[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_SHORT_ARRAY;
- }
- final short[] result = new short[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i].shortValue();
+ public static Float[] nullToEmpty(final Float[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_FLOAT_OBJECT_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of object Short to primitives handling {@code null}.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Short} array, may be {@code null}
- * @param valueForNull the value to insert if {@code null} found
- * @return a {@code byte} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static short[] toPrimitive(final Short[] array, final short valueForNull) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_SHORT_ARRAY;
- }
- final short[] result = new short[array.length];
- for (int i = 0; i < array.length; i++) {
- final Short b = array[i];
- result[i] = (b == null ? valueForNull : b.shortValue());
+ public static int[] nullToEmpty(final int[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_INT_ARRAY;
}
- return result;
+ return array;
}
+ // Primitive/Object array converters
+ // ----------------------------------------------------------------------
+
/**
- * <p>Converts an array of primitive shorts to objects.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code short} array
- * @return a {@code Short} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static Short[] toObject(final short[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_SHORT_OBJECT_ARRAY;
- }
- final Short[] result = new Short[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = Short.valueOf(array[i]);
+ public static Integer[] nullToEmpty(final Integer[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_INTEGER_OBJECT_ARRAY;
}
- return result;
+ return array;
}
- // Byte array converters
- // ----------------------------------------------------------------------
/**
- * <p>Converts an array of object Bytes to primitives.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Byte} array, may be {@code null}
- * @return a {@code byte} array, {@code null} if null array input
- * @throws NullPointerException if array content is {@code null}
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static byte[] toPrimitive(final Byte[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_BYTE_ARRAY;
- }
- final byte[] result = new byte[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i].byteValue();
+ public static long[] nullToEmpty(final long[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_LONG_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of object Bytes to primitives handling {@code null}.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Byte} array, may be {@code null}
- * @param valueForNull the value to insert if {@code null} found
- * @return a {@code byte} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static byte[] toPrimitive(final Byte[] array, final byte valueForNull) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_BYTE_ARRAY;
- }
- final byte[] result = new byte[array.length];
- for (int i = 0; i < array.length; i++) {
- final Byte b = array[i];
- result[i] = (b == null ? valueForNull : b.byteValue());
+ public static Long[] nullToEmpty(final Long[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_LONG_OBJECT_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of primitive bytes to objects.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code byte} array
- * @return a {@code Byte} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static Byte[] toObject(final byte[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_BYTE_OBJECT_ARRAY;
- }
- final Byte[] result = new Byte[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = Byte.valueOf(array[i]);
+ public static Object[] nullToEmpty(final Object[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_OBJECT_ARRAY;
}
- return result;
+ return array;
}
- // Double array converters
- // ----------------------------------------------------------------------
/**
- * <p>Converts an array of object Doubles to primitives.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Double} array, may be {@code null}
- * @return a {@code double} array, {@code null} if null array input
- * @throws NullPointerException if array content is {@code null}
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static double[] toPrimitive(final Double[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_DOUBLE_ARRAY;
- }
- final double[] result = new double[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i].doubleValue();
+ public static short[] nullToEmpty(final short[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_SHORT_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of object Doubles to primitives handling {@code null}.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Double} array, may be {@code null}
- * @param valueForNull the value to insert if {@code null} found
- * @return a {@code double} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static double[] toPrimitive(final Double[] array, final double valueForNull) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_DOUBLE_ARRAY;
- }
- final double[] result = new double[array.length];
- for (int i = 0; i < array.length; i++) {
- final Double b = array[i];
- result[i] = (b == null ? valueForNull : b.doubleValue());
+ public static Short[] nullToEmpty(final Short[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_SHORT_OBJECT_ARRAY;
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of primitive doubles to objects.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code double} array
- * @return a {@code Double} array, {@code null} if null array input
+ * <p>As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
*/
- public static Double[] toObject(final double[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_DOUBLE_OBJECT_ARRAY;
- }
- final Double[] result = new Double[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = Double.valueOf(array[i]);
+ public static String[] nullToEmpty(final String[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_STRING_ARRAY;
}
- return result;
+ return array;
}
- // Float array converters
- // ----------------------------------------------------------------------
+ // nullToEmpty
+ //-----------------------------------------------------------------------
/**
- * <p>Converts an array of object Floats to primitives.
+ * <p>Defensive programming technique to change a {@code null}
+ * reference to an empty one.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <p>This method returns an empty array for a {@code null} input array.
*
- * @param array a {@code Float} array, may be {@code null}
- * @return a {@code float} array, {@code null} if null array input
- * @throws NullPointerException if array content is {@code null}
+ * @param array the array to check for {@code null} or empty
+ * @param type the class representation of the desired array
+ * @param <T> the class type
+ * @return the same array, {@code public static} empty array if {@code null}
+ * @throws IllegalArgumentException if the type argument is null
+ * @since 3.5
*/
- public static float[] toPrimitive(final Float[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_FLOAT_ARRAY;
+ public static <T> T[] nullToEmpty(final T[] array, final Class<T[]> type) {
+ if (type == null) {
+ throw new IllegalArgumentException("The type must not be null");
}
- final float[] result = new float[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i].floatValue();
+
+ if (array == null) {
+ return type.cast(Array.newInstance(type.getComponentType(), 0));
}
- return result;
+ return array;
}
/**
- * <p>Converts an array of object Floats to primitives handling {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <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.
*
- * @param array a {@code Float} array, may be {@code null}
- * @param valueForNull the value to insert if {@code null} found
- * @return a {@code float} array, {@code null} if null array input
- */
- public static float[] toPrimitive(final Float[] array, final float valueForNull) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_FLOAT_ARRAY;
- }
- final float[] result = new float[array.length];
- for (int i = 0; i < array.length; i++) {
- final Float b = array[i];
- result[i] = (b == null ? valueForNull : b.floatValue());
- }
- return result;
- }
-
- /**
- * <p>Converts an array of primitive floats to objects.
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <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 a {@code float} array
- * @return a {@code Float} array, {@code null} if null array input
+ * @param array the array to remove the element from, may not be {@code null}
+ * @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}.
+ * @since 2.1
*/
- public static Float[] toObject(final float[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_FLOAT_OBJECT_ARRAY;
- }
- final Float[] result = new Float[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = Float.valueOf(array[i]);
- }
- return result;
+ public static boolean[] remove(final boolean[] array, final int index) {
+ return (boolean[]) remove((Object) array, index);
}
/**
- * <p>Create an array of primitive type from an array of wrapper types.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <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.
*
- * @param array an array of wrapper object
- * @return an array of the corresponding primitive type, or the original array
- * @since 3.5
- */
- public static Object toPrimitive(final Object array) {
- if (array == null) {
- return null;
- }
- final Class<?> ct = array.getClass().getComponentType();
- final Class<?> pt = ClassUtils.wrapperToPrimitive(ct);
- if (Integer.TYPE.equals(pt)) {
- return toPrimitive((Integer[]) array);
- }
- if (Long.TYPE.equals(pt)) {
- return toPrimitive((Long[]) array);
- }
- if (Short.TYPE.equals(pt)) {
- return toPrimitive((Short[]) array);
- }
- if (Double.TYPE.equals(pt)) {
- return toPrimitive((Double[]) array);
- }
- if (Float.TYPE.equals(pt)) {
- return toPrimitive((Float[]) array);
- }
- return array;
- }
-
- // Boolean array converters
- // ----------------------------------------------------------------------
- /**
- * <p>Converts an array of object Booleans to primitives.
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <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 a {@code Boolean} array, may be {@code null}
- * @return a {@code boolean} array, {@code null} if null array input
- * @throws NullPointerException if array content is {@code null}
+ * @param array the array to remove the element from, may not be {@code null}
+ * @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}.
+ * @since 2.1
*/
- public static boolean[] toPrimitive(final Boolean[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_BOOLEAN_ARRAY;
- }
- final boolean[] result = new boolean[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i].booleanValue();
- }
- return result;
+ public static byte[] remove(final byte[] array, final int index) {
+ return (byte[]) remove((Object) array, index);
}
/**
- * <p>Converts an array of object Booleans to primitives handling {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <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.
*
- * @param array a {@code Boolean} array, may be {@code null}
- * @param valueForNull the value to insert if {@code null} found
- * @return a {@code boolean} array, {@code null} if null array input
- */
- public static boolean[] toPrimitive(final Boolean[] array, final boolean valueForNull) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_BOOLEAN_ARRAY;
- }
- final boolean[] result = new boolean[array.length];
- for (int i = 0; i < array.length; i++) {
- final Boolean b = array[i];
- result[i] = (b == null ? valueForNull : b.booleanValue());
- }
- return result;
- }
-
- /**
- * <p>Converts an array of primitive booleans to objects.
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
- * <p>This method returns {@code null} for a {@code null} input array.
+ * <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 a {@code boolean} array
- * @return a {@code Boolean} array, {@code null} if null array input
+ * @param array the array to remove the element from, may not be {@code null}
+ * @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}.
+ * @since 2.1
*/
- public static Boolean[] toObject(final boolean[] array) {
- if (array == null) {
- return null;
- } else if (array.length == 0) {
- return EMPTY_BOOLEAN_OBJECT_ARRAY;
- }
- final Boolean[] result = new Boolean[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
- }
- return result;
+ public static char[] remove(final char[] array, final int index) {
+ return (char[]) remove((Object) array, index);
}
- // ----------------------------------------------------------------------
/**
- * <p>Checks if an array of Objects is empty or {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
+ * <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>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * <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}
+ * @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}.
* @since 2.1
*/
- public static boolean isEmpty(final Object[] array) {
- return getLength(array) == 0;
+ public static double[] remove(final double[] array, final int index) {
+ return (double[]) remove((Object) array, index);
}
/**
- * <p>Checks if an array of primitive longs is empty or {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
+ * <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>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * <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}
+ * @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}.
* @since 2.1
*/
- public static boolean isEmpty(final long[] array) {
- return getLength(array) == 0;
+ public static float[] remove(final float[] array, final int index) {
+ return (float[]) remove((Object) array, index);
}
/**
- * <p>Checks if an array of primitive ints is empty or {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
- * @since 2.1
- */
- public static boolean isEmpty(final int[] array) {
- return getLength(array) == 0;
- }
-
- /**
- * <p>Checks if an array of primitive shorts is empty or {@code null}.
+ * <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.
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
- * @since 2.1
- */
- public static boolean isEmpty(final short[] array) {
- return getLength(array) == 0;
- }
-
- /**
- * <p>Checks if an array of primitive chars is empty or {@code null}.
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
+ * <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}
+ * @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}.
* @since 2.1
*/
- public static boolean isEmpty(final char[] array) {
- return getLength(array) == 0;
+ public static int[] remove(final int[] array, final int index) {
+ return (int[]) remove((Object) array, index);
}
/**
- * <p>Checks if an array of primitive bytes is empty or {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
+ * <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>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * <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}
+ * @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}.
* @since 2.1
*/
- public static boolean isEmpty(final byte[] array) {
- return getLength(array) == 0;
+ public static long[] remove(final long[] array, final int index) {
+ return (long[]) remove((Object) array, index);
}
/**
- * <p>Checks if an array of primitive doubles is empty or {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
+ * <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>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @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}.
* @since 2.1
*/
- public static boolean isEmpty(final double[] array) {
- return getLength(array) == 0;
+ private static Object remove(final Object array, final int index) {
+ final int length = getLength(array);
+ if (index < 0 || index >= length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
+ }
+
+ final 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;
}
/**
- * <p>Checks if an array of primitive floats is empty or {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
+ * <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>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * <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}
+ * @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}.
* @since 2.1
*/
- public static boolean isEmpty(final float[] array) {
- return getLength(array) == 0;
+ public static short[] remove(final short[] array, final int index) {
+ return (short[]) remove((Object) array, index);
}
/**
- * <p>Checks if an array of primitive booleans is empty or {@code null}.
+ * <p>Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
*
- * @param array the array to test
- * @return {@code true} if the array is empty or {@code null}
+ * <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>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * <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 <T> the component type of the array
+ * @param array the array to remove the element from, may not be {@code null}
+ * @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}.
* @since 2.1
*/
- public static boolean isEmpty(final boolean[] array) {
- return getLength(array) == 0;
+ @SuppressWarnings("unchecked") // remove() always creates an array of the same type as its input
+ public static <T> T[] remove(final T[] array, final int index) {
+ return (T[]) remove((Object) array, index);
}
- // ----------------------------------------------------------------------
/**
- * <p>Checks if an array of Objects is not empty and not {@code null}.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
*
- * @param <T> the component type of the array
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
- */
- public static <T> boolean isNotEmpty(final T[] array) {
- return !isEmpty(array);
- }
-
- /**
- * <p>Checks if an array of primitive longs is not empty and not {@code null}.
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * <pre>
+ * ArrayUtils.removeAll([true, false, true], 0, 2) = [false]
+ * ArrayUtils.removeAll([true, false, true], 1, 2) = [true]
+ * </pre>
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
*/
- public static boolean isNotEmpty(final long[] array) {
- return !isEmpty(array);
+ public static boolean[] removeAll(final boolean[] array, final int... indices) {
+ return (boolean[]) removeAll((Object) array, indices);
}
/**
- * <p>Checks if an array of primitive ints is not empty and not {@code null}.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
*
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * <pre>
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ * </pre>
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
*/
- public static boolean isNotEmpty(final int[] array) {
- return !isEmpty(array);
+ public static byte[] removeAll(final byte[] array, final int... indices) {
+ return (byte[]) removeAll((Object) array, indices);
}
/**
- * <p>Checks if an array of primitive shorts is not empty and not {@code null}.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
*
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
- */
- public static boolean isNotEmpty(final short[] array) {
- return !isEmpty(array);
- }
-
- /**
- * <p>Checks if an array of primitive chars is not empty and not {@code null}.
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
- */
- public static boolean isNotEmpty(final char[] array) {
- return !isEmpty(array);
- }
-
- /**
- * <p>Checks if an array of primitive bytes is not empty and not {@code null}.
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
- */
- public static boolean isNotEmpty(final byte[] array) {
- return !isEmpty(array);
- }
-
- /**
- * <p>Checks if an array of primitive doubles is not empty and not {@code null}.
+ * <pre>
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ * </pre>
*
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
*/
- public static boolean isNotEmpty(final double[] array) {
- return !isEmpty(array);
+ public static char[] removeAll(final char[] array, final int... indices) {
+ return (char[]) removeAll((Object) array, indices);
}
/**
- * <p>Checks if an array of primitive floats is not empty and not {@code null}.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
*
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
- */
- public static boolean isNotEmpty(final float[] array) {
- return !isEmpty(array);
- }
-
- /**
- * <p>Checks if an array of primitive booleans is not empty and not {@code null}.
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array the array to test
- * @return {@code true} if the array is not empty and not {@code null}
- * @since 2.5
- */
- public static boolean isNotEmpty(final boolean[] array) {
- return !isEmpty(array);
- }
-
- /**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
* <pre>
- * ArrayUtils.addAll(null, null) = null
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
- * ArrayUtils.addAll([null], [null]) = [null, null]
- * ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
* </pre>
*
- * @param <T> the component type of the array
- * @param array1 the first array whose elements are added to the new array, may be {@code null}
- * @param array2 the second array whose elements are added to the new array, may be {@code null}
- * @return The new array, {@code null} if both arrays are {@code null}.
- * The type of the new array is the type of the first array,
- * unless the first array is null, in which case the type is the same as the second array.
- * @since 2.1
- * @throws IllegalArgumentException if the array types are incompatible
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
*/
- public static <T> T[] addAll(final T[] array1, @SuppressWarnings("unchecked") final T... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
- }
- final Class<?> type1 = array1.getClass().getComponentType();
- @SuppressWarnings("unchecked") // OK, because array is of type T
- final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- try {
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- } catch (final ArrayStoreException ase) {
- // Check if problem was due to incompatible types
- /*
- * We do this here, rather than before the copy because:
- * - it would be a wasted check most of the time
- * - safer, in case check turns out to be too strict
- */
- final Class<?> type2 = array2.getClass().getComponentType();
- if (!type1.isAssignableFrom(type2)) {
- throw new IllegalArgumentException("Cannot store " + type2.getName() + " in an array of "
- + type1.getName(), ase);
- }
- throw ase; // No, so rethrow original
- }
- return joinedArray;
+ public static double[] removeAll(final double[] array, final int... indices) {
+ return (double[]) removeAll((Object) array, indices);
}
/**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
* <pre>
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
* </pre>
*
- * @param array1 the first array whose elements are added to the new array.
- * @param array2 the second array whose elements are added to the new array.
- * @return The new boolean[] array.
- * @since 2.1
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
*/
- public static boolean[] addAll(final boolean[] array1, final boolean... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
- }
- final boolean[] joinedArray = new boolean[array1.length + array2.length];
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- return joinedArray;
+ public static float[] removeAll(final float[] array, final int... indices) {
+ return (float[]) removeAll((Object) array, indices);
}
/**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
* <pre>
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
* </pre>
*
- * @param array1 the first array whose elements are added to the new array.
- * @param array2 the second array whose elements are added to the new array.
- * @return The new char[] array.
- * @since 2.1
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
*/
- public static char[] addAll(final char[] array1, final char... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
- }
- final char[] joinedArray = new char[array1.length + array2.length];
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- return joinedArray;
+ public static int[] removeAll(final int[] array, final int... indices) {
+ return (int[]) removeAll((Object) array, indices);
}
/**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
*
- * <pre>
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
- * </pre>
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
*
- * @param array1 the first array whose elements are added to the new array.
- * @param array2 the second array whose elements are added to the new array.
- * @return The new byte[] array.
- * @since 2.1
- */
- public static byte[] addAll(final byte[] array1, final byte... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
- }
- final byte[] joinedArray = new byte[array1.length + array2.length];
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- return joinedArray;
- }
-
- /**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
* <pre>
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
* </pre>
*
- * @param array1 the first array whose elements are added to the new array.
- * @param array2 the second array whose elements are added to the new array.
- * @return The new short[] array.
- * @since 2.1
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
*/
- public static short[] addAll(final short[] array1, final short... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
- }
- final short[] joinedArray = new short[array1.length + array2.length];
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- return joinedArray;
+ public static long[] removeAll(final long[] array, final int... indices) {
+ return (long[]) removeAll((Object) array, indices);
}
/**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
- *
- * <pre>
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
- * </pre>
+ * Removes multiple array elements specified by indices.
*
- * @param array1 the first array whose elements are added to the new array.
- * @param array2 the second array whose elements are added to the new array.
- * @return The new int[] array.
- * @since 2.1
+ * @param array source
+ * @param indices to remove
+ * @return new array of same type minus elements specified by the set bits in {@code indices}
+ * @since 3.2
*/
- public static int[] addAll(final int[] array1, final int... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
+ // package protected for access by unit tests
+ static Object removeAll(final Object array, final BitSet indices) {
+ if (array == null) {
+ return null;
}
- final int[] joinedArray = new int[array1.length + array2.length];
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- return joinedArray;
+
+ final int srcLength = getLength(array);
+ // No need to check maxIndex here, because method only currently called from removeElements()
+ // which guarantee to generate on;y valid bit entries.
+// final int maxIndex = indices.length();
+// if (maxIndex > srcLength) {
+// throw new IndexOutOfBoundsException("Index: " + (maxIndex-1) + ", Length: " + srcLength);
+// }
+ final int removals = indices.cardinality(); // true bits are items to remove
+ final Object result = Array.newInstance(array.getClass().getComponentType(), srcLength - removals);
+ int srcIndex = 0;
+ int destIndex = 0;
+ int count;
+ int set;
+ while ((set = indices.nextSetBit(srcIndex)) != -1) {
+ count = set - srcIndex;
+ if (count > 0) {
+ System.arraycopy(array, srcIndex, result, destIndex, count);
+ destIndex += count;
+ }
+ srcIndex = indices.nextClearBit(set);
+ }
+ count = srcLength - srcIndex;
+ if (count > 0) {
+ System.arraycopy(array, srcIndex, result, destIndex, count);
+ }
+ return result;
}
/**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
- *
- * <pre>
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
- * </pre>
- *
- * @param array1 the first array whose elements are added to the new array.
- * @param array2 the second array whose elements are added to the new array.
- * @return The new long[] array.
- * @since 2.1
+ * Removes multiple array elements specified by index.
+ * @param array source
+ * @param indices to remove
+ * @return new array of same type minus elements specified by unique values of {@code indices}
+ * @since 3.0.1
*/
- public static long[] addAll(final long[] array1, final long... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
+ // package protected for access by unit tests
+ static Object removeAll(final Object array, final int... indices) {
+ final int length = getLength(array);
+ int diff = 0; // number of distinct indexes, i.e. number of entries that will be removed
+ final int[] clonedIndices = clone(indices);
+ Arrays.sort(clonedIndices);
+
+ // identify length of result array
+ if (isNotEmpty(clonedIndices)) {
+ int i = clonedIndices.length;
+ int prevIndex = length;
+ while (--i >= 0) {
+ final int index = clonedIndices[i];
+ if (index < 0 || index >= length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
+ }
+ if (index >= prevIndex) {
+ continue;
+ }
+ diff++;
+ prevIndex = index;
+ }
}
- final long[] joinedArray = new long[array1.length + array2.length];
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- return joinedArray;
+
+ // create result array
+ final Object result = Array.newInstance(array.getClass().getComponentType(), length - diff);
+ if (diff < length) {
+ int end = length; // index just after last copy
+ int dest = length - diff; // number of entries so far not copied
+ for (int i = clonedIndices.length - 1; i >= 0; i--) {
+ final int index = clonedIndices[i];
+ if (end - index > 1) { // same as (cp > 0)
+ final int cp = end - index - 1;
+ dest -= cp;
+ System.arraycopy(array, index + 1, result, dest, cp);
+ // Afer this copy, we still have room for dest items.
+ }
+ end = index;
+ }
+ if (end > 0) {
+ System.arraycopy(array, 0, result, 0, end);
+ }
+ }
+ return result;
}
/**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
* <pre>
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
* </pre>
*
- * @param array1 the first array whose elements are added to the new array.
- * @param array2 the second array whose elements are added to the new array.
- * @return The new float[] array.
- * @since 2.1
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
*/
- public static float[] addAll(final float[] array1, final float... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
- }
- final float[] joinedArray = new float[array1.length + array2.length];
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- return joinedArray;
+ public static short[] removeAll(final short[] array, final int... indices) {
+ return (short[]) removeAll((Object) array, indices);
}
/**
- * <p>Adds all the elements of the given arrays into a new array.
- * <p>The new array contains all of the element of {@code array1} followed
- * by all of the elements {@code array2}. When an array is returned, it is always
- * a new array.
+ * <p>Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * <p>If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
*
* <pre>
- * ArrayUtils.addAll(array1, null) = cloned copy of array1
- * ArrayUtils.addAll(null, array2) = cloned copy of array2
- * ArrayUtils.addAll([], []) = []
+ * ArrayUtils.removeAll(["a", "b", "c"], 0, 2) = ["b"]
+ * ArrayUtils.removeAll(["a", "b", "c"], 1, 2) = ["a"]
* </pre>
*
- * @param array1 the first array whose elements are added to the new array.
- * @param array2 the second array whose elements are added to the new array.
- * @return The new double[] array.
- * @since 2.1
- */
- public static double[] addAll(final double[] array1, final double... array2) {
- if (array1 == null) {
- return clone(array2);
- } else if (array2 == null) {
- return clone(array1);
- }
- final double[] joinedArray = new double[array1.length + array2.length];
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
- return joinedArray;
+ * @param <T> the component type of the array
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ @SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input
+ public static <T> T[] removeAll(final T[] array, final int... indices) {
+ return (T[]) removeAll((Object) array, indices);
}
/**
- * <p>Copies the given array and adds the given element at the end of the new array.
- *
- * <p>The new array contains the same elements of the input
- * array plus the given element in the last position. The component type of
- * the new array is the same as that of the input array.
+ * Removes the occurrences of the specified element from the specified boolean array.
*
- * <p>If the input array is {@code null}, a new one element array is returned
- * whose component type is the same as the element, unless the element itself is null,
- * in which case the return type is Object[]
+ * <p>
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
+ * </p>
*
- * <pre>
- * ArrayUtils.add(null, null) = IllegalArgumentException
- * ArrayUtils.add(null, "a") = ["a"]
- * ArrayUtils.add(["a"], null) = ["a", null]
- * ArrayUtils.add(["a"], "b") = ["a", "b"]
- * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
- * </pre>
+ * @param element the element to remove
+ * @param array the input array
*
- * @param <T> the component type of the array
- * @param array the array to "add" the element to, may be {@code null}
- * @param element the object to add, may be {@code null}
- * @return A new array containing the existing elements plus the new element
- * The returned array type will be that of the input array (unless null),
- * in which case it will have the same type as the element.
- * If both are null, an IllegalArgumentException is thrown
- * @since 2.1
- * @throws IllegalArgumentException if both arguments are null
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static <T> T[] add(final T[] array, final T element) {
- Class<?> type;
- if (array != null) {
- type = array.getClass().getComponentType();
- } else if (element != null) {
- type = element.getClass();
- } else {
- throw new IllegalArgumentException("Arguments cannot both be null");
- }
- @SuppressWarnings("unchecked") // type must be T
- final
- T[] newArray = (T[]) copyArrayGrow1(array, type);
- newArray[newArray.length - 1] = element;
- return newArray;
+ public static boolean[] removeAllOccurences(final boolean[] array, final boolean element) {
+ return (boolean[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
- *
- * <p>
- * The new array contains the same elements of the input array plus the given element in the first position. The
- * component type of the new array is the same as that of the input array.
- * </p>
+ * Removes the occurrences of the specified element from the specified byte array.
*
* <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element.
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
* </p>
*
- * <pre>
- * ArrayUtils.add(null, true) = [true]
- * ArrayUtils.add([true], false) = [false, true]
- * ArrayUtils.add([true, false], true) = [true, true, false]
- * </pre>
+ * @param element the element to remove
+ * @param array the input array
*
- * @param array the array to "add" the element to, may be {@code null}.
- * @param element the object to add.
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element.
- * @since 3.10
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static boolean[] addFirst(final boolean[] array, final boolean element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static byte[] removeAllOccurences(final byte[] array, final byte element) {
+ return (byte[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
- *
- * <p>
- * The new array contains the same elements of the input array plus the given element in the first position. The
- * component type of the new array is the same as that of the input array.
- * </p>
+ * Removes the occurrences of the specified element from the specified char array.
*
* <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element.
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
* </p>
*
- * <pre>
- * ArrayUtils.add(null, 1) = [1]
- * ArrayUtils.add([1], 0) = [0, 1]
- * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
- * </pre>
+ * @param element the element to remove
+ * @param array the input array
*
- * @param array the array to "add" the element to, may be {@code null}.
- * @param element the object to add.
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element.
- * @since 3.10
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static byte[] addFirst(final byte[] array, final byte element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static char[] removeAllOccurences(final char[] array, final char element) {
+ return (char[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
- *
- * <p>
- * The new array contains the same elements of the input array plus the given element in the first position. The
- * component type of the new array is the same as that of the input array.
- * </p>
+ * Removes the occurrences of the specified element from the specified double array.
*
* <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element.
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
* </p>
*
- * <pre>
- * ArrayUtils.add(null, '1') = ['1']
- * ArrayUtils.add(['1'], '0') = ['0', '1']
- * ArrayUtils.add(['1', '0'], '1') = ['1', '1', '0']
- * </pre>
+ * @param element the element to remove
+ * @param array the input array
*
- * @param array the array to "add" the element to, may be {@code null}.
- * @param element the object to add.
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element.
- * @since 3.10
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static char[] addFirst(final char[] array, final char element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static double[] removeAllOccurences(final double[] array, final double element) {
+ return (double[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
- *
- * <p>
- * The new array contains the same elements of the input array plus the given element in the first position. The
- * component type of the new array is the same as that of the input array.
- * </p>
+ * Removes the occurrences of the specified element from the specified float array.
*
* <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element.
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
* </p>
*
- * <pre>
- * ArrayUtils.add(null, 1) = [1]
- * ArrayUtils.add([1], 0) = [0, 1]
- * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
- * </pre>
+ * @param element the element to remove
+ * @param array the input array
*
- * @param array the array to "add" the element to, may be {@code null}.
- * @param element the object to add.
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element.
- * @since 3.10
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static double[] addFirst(final double[] array, final double element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static float[] removeAllOccurences(final float[] array, final float element) {
+ return (float[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
- *
- * <p>
- * The new array contains the same elements of the input array plus the given element in the first position. The
- * component type of the new array is the same as that of the input array.
- * </p>
+ * Removes the occurrences of the specified element from the specified int array.
*
* <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element.
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
* </p>
*
- * <pre>
- * ArrayUtils.add(null, 1) = [1]
- * ArrayUtils.add([1], 0) = [0, 1]
- * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
- * </pre>
+ * @param element the element to remove
+ * @param array the input array
*
- * @param array the array to "add" the element to, may be {@code null}.
- * @param element the object to add.
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element.
- * @since 3.10
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static float[] addFirst(final float[] array, final float element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static int[] removeAllOccurences(final int[] array, final int element) {
+ return (int[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
- *
- * <p>
- * The new array contains the same elements of the input array plus the given element in the first position. The
- * component type of the new array is the same as that of the input array.
- * </p>
+ * Removes the occurrences of the specified element from the specified long array.
*
* <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element.
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
* </p>
*
- * <pre>
- * ArrayUtils.add(null, 1) = [1]
- * ArrayUtils.add([1], 0) = [0, 1]
- * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
- * </pre>
+ * @param element the element to remove
+ * @param array the input array
*
- * @param array the array to "add" the element to, may be {@code null}.
- * @param element the object to add.
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element.
- * @since 3.10
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static int[] addFirst(final int[] array, final int element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static long[] removeAllOccurences(final long[] array, final long element) {
+ return (long[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
+ * Removes the occurrences of the specified element from the specified short array.
*
* <p>
- * The new array contains the same elements of the input array plus the given element in the first position. The
- * component type of the new array is the same as that of the input array.
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
* </p>
*
- * <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element.
- * </p>
- *
- * <pre>
- * ArrayUtils.add(null, 1) = [1]
- * ArrayUtils.add([1], 0) = [0, 1]
- * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
- * </pre>
+ * @param element the element to remove
+ * @param array the input array
*
- * @param array the array to "add" the element to, may be {@code null}.
- * @param element the object to add.
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element.
- * @since 3.10
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static long[] addFirst(final long[] array, final long element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static short[] removeAllOccurences(final short[] array, final short element) {
+ return (short[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
- *
- * <p>
- * The new array contains the same elements of the input array plus the given element in the first position. The
- * component type of the new array is the same as that of the input array.
- * </p>
+ * Removes the occurrences of the specified element from the specified array.
*
* <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element.
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * <code>null</code> will be returned if the input array is <code>null</code>.
* </p>
*
- * <pre>
- * ArrayUtils.add(null, 1) = [1]
- * ArrayUtils.add([1], 0) = [0, 1]
- * ArrayUtils.add([1, 0], 1) = [1, 1, 0]
- * </pre>
+ * @param <T> the type of object in the array
+ * @param element the element to remove
+ * @param array the input array
*
- * @param array the array to "add" the element to, may be {@code null}.
- * @param element the object to add.
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element.
- * @since 3.10
+ * @return A new array containing the existing elements except the occurrences of the specified element.
+ * @since 3.5
*/
- public static short[] addFirst(final short[] array, final short element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static <T> T[] removeAllOccurences(final T[] array, final T element) {
+ return (T[]) removeAll((Object) array, indexesOf(array, element));
}
/**
- * Copies the given array and adds the given element at the beginning of the new array.
- *
- * <p>
- * The new array contains the same elements of the input array plus the given element in the first positioaddFirstaddFirstaddFirstn. The
- * component type of the new array is the same as that of the input array.
- * </p>
+ * <p>Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
*
- * <p>
- * If the input array is {@code null}, a new one element array is returned whose component type is the same as the
- * element, unless the element itself is null, in which case the return type is Object[]
- * </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.
*
* <pre>
- * ArrayUtils.add(null, null) = IllegalArgumentException
- * ArrayUtils.add(null, "a") = ["a"]
- * ArrayUtils.add(["a"], null) = [null, "a"]
- * ArrayUtils.add(["a"], "b") = ["b", "a"]
- * ArrayUtils.add(["a", "b"], "c") = ["c", "a", "b"]
+ * 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 <T> the component type of the array
- * @param array the array to "add" the element to, may be {@code null}
- * @param element the object to add, may be {@code null}
- * @return A new array containing the existing elements plus the new element The returned array type will be that of
- * the input array (unless null), in which case it will have the same type as the element. If both are null,
- * an IllegalArgumentException is thrown
- * @since 3.10
- * @throws IllegalArgumentException if both arguments are null
+ * @param array the array to remove the element from, may be {@code null}
+ * @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 <T> T[] addFirst(final T[] array, final T element) {
- return array == null ? add(array, element) : insert(0, array, element);
+ public static boolean[] removeElement(final boolean[] array, final boolean element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
}
/**
- * <p>Copies the given array and adds the given element at the end of the new array.
- *
- * <p>The new array contains the same elements of the input
- * array plus the given element in the last position. The component type of
- * the new array is the same as that of the input array.
+ * <p>Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
*
- * <p>If the input array is {@code null}, a new one element array is returned
- * whose component type is the same as the element.
+ * <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.
*
* <pre>
- * ArrayUtils.add(null, true) = [true]
- * ArrayUtils.add([true], false) = [true, false]
- * ArrayUtils.add([true, false], true) = [true, false, true]
+ * 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 copy and add the element to, may be {@code null}
- * @param element the object to add at the last index of the new array
- * @return A new array containing the existing elements plus the new element
+ * @param array the array to remove the element from, may be {@code null}
+ * @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[] add(final boolean[] array, final boolean element) {
- final boolean[] newArray = (boolean[]) copyArrayGrow1(array, Boolean.TYPE);
- newArray[newArray.length - 1] = element;
- return newArray;
+ public static byte[] removeElement(final byte[] array, final byte element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
}
/**
- * <p>Copies the given array and adds the given element at the end of the new array.
- *
- * <p>The new array contains the same elements of the input
- * array plus the given element in the last position. The component type of
- * the new array is the same as that of the input array.
+ * <p>Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
*
- * <p>If the input array is {@code null}, a new one element array is returned
- * whose component type is the same as the element.
+ * <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.
*
* <pre>
- * ArrayUtils.add(null, 0) = [0]
- * ArrayUtils.add([1], 0) = [1, 0]
- * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * 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 copy and add the element to, may be {@code null}
- * @param element the object to add at the last index of the new array
- * @return A new array containing the existing elements plus the new element
+ * @param array the array to remove the element from, may be {@code null}
+ * @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[] add(final byte[] array, final byte element) {
- final byte[] newArray = (byte[]) copyArrayGrow1(array, Byte.TYPE);
- newArray[newArray.length - 1] = element;
- return newArray;
+ public static char[] removeElement(final char[] array, final char element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
}
/**
- * <p>Copies the given array and adds the given element at the end of the new array.
- *
- * <p>The new array contains the same elements of the input
- * array plus the given element in the last position. The component type of
- * the new array is the same as that of the input array.
+ * <p>Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
*
- * <p>If the input array is {@code null}, a new one element array is returned
- * whose component type is the same as the element.
+ * <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.
*
* <pre>
- * ArrayUtils.add(null, '0') = ['0']
- * ArrayUtils.add(['1'], '0') = ['1', '0']
- * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
+ * 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 copy and add the element to, may be {@code null}
- * @param element the object to add at the last index of the new array
- * @return A new array containing the existing elements plus the new element
+ * @param array the array to remove the element from, may be {@code null}
+ * @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[] add(final char[] array, final char element) {
- final char[] newArray = (char[]) copyArrayGrow1(array, Character.TYPE);
- newArray[newArray.length - 1] = element;
- return newArray;
+ public static double[] removeElement(final double[] array, final double element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
}
/**
- * <p>Copies the given array and adds the given element at the end of the new array.
- *
- * <p>The new array contains the same elements of the input
- * array plus the given element in the last position. The component type of
- * the new array is the same as that of the input array.
+ * <p>Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
*
- * <p>If the input array is {@code null}, a new one element array is returned
- * whose component type is the same as the element.
+ * <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.
*
* <pre>
- * ArrayUtils.add(null, 0) = [0]
- * ArrayUtils.add([1], 0) = [1, 0]
- * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * 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 copy and add the element to, may be {@code null}
- * @param element the object to add at the last index of the new array
- * @return A new array containing the existing elements plus the new element
+ * @param array the array to remove the element from, may be {@code null}
+ * @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[] add(final double[] array, final double element) {
- final double[] newArray = (double[]) copyArrayGrow1(array, Double.TYPE);
- newArray[newArray.length - 1] = element;
- return newArray;
- }
-
- /**
- * <p>Copies the given array and adds the given element at the end of the new array.
- *
- * <p>The new array contains the same elements of the input
- * array plus the given element in the last position. The component type of
- * the new array is the same as that of the input array.
- *
- * <p>If the input array is {@code null}, a new one element array is returned
- * whose component type is the same as the element.
- *
- * <pre>
- * ArrayUtils.add(null, 0) = [0]
- * ArrayUtils.add([1], 0) = [1, 0]
- * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
- * </pre>
- *
- * @param array the array to copy and add the element to, may be {@code null}
- * @param element the object to add at the last index of the new array
- * @return A new array containing the existing elements plus the new element
- * @since 2.1
- */
- public static float[] add(final float[] array, final float element) {
- final float[] newArray = (float[]) copyArrayGrow1(array, Float.TYPE);
- newArray[newArray.length - 1] = element;
- return newArray;
+ public static float[] removeElement(final float[] array, final float element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
}
/**
- * <p>Copies the given array and adds the given element at the end of the new array.
- *
- * <p>The new array contains the same elements of the input
- * array plus the given element in the last position. The component type of
- * the new array is the same as that of the input array.
+ * <p>Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
*
- * <p>If the input array is {@code null}, a new one element array is returned
- * whose component type is the same as the element.
+ * <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.
*
* <pre>
- * ArrayUtils.add(null, 0) = [0]
- * ArrayUtils.add([1], 0) = [1, 0]
- * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+ * 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 copy and add the element to, may be {@code null}
- * @param element the object to add at the last index of the new array
- * @return A new array containing the existing elements plus the new element
+ * @param array the array to remove the element from, may be {@code null}
+ * @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[] add(final int[] array, final int element) {
- final int[] newArray = (int[]) copyArrayGrow1(array, Integer.TYPE);
- newArray[newArray.length - 1] = element;
- return newArray;
+ public static int[] removeElement(final int[] array, final int element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
}
/**
- * <p>Copies the given array and adds the given element at the end of the new array.
- *
- * <p>The new array contains the same elements of the input
- * array plus the given element in the last position. The component type of
- * the new array is the same as that of the input array.
+ * <p>Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
*
- * <p>If the input array is {@code null}, a new one element array is returned
- * whose component type is the same as the element.
+ * <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.
*
* <pre>
... 6022 lines suppressed ...