[
https://issues.apache.org/jira/browse/LANG-1074?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
haiyang li updated LANG-1074:
-----------------------------
Description:
Could we add the method: removeElementAll to remove all the occurrences of the
specified element from the specified
(boolean/char/byte/short/int/long/float/double/Object) array:
{code:title=org.apache.commons.lang3.ArrayUtils.java|borderStyle=solid}
public static <T> T[] removeElementAll(final T[] array, final Object
element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return clone(array);
}
int[] indices = new int[array.length - index];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(array, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (T[]) removeAll((Object) array, Arrays.copyOfRange(indices, 0,
count));
}
{code}
or maybe better:
{code:title=org.apache.commons.lang3.ArrayUtils.java|borderStyle=solid}
public static <T> T[] removeElement(final T[] a, final Object element,
boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length - index];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (T[]) removeAll((Object) a, Arrays.copyOfRange(indices, 0,
count));
}
}
{code}
was:
Could we add the method: removeElementAll to remove all the occurrences of the
specified element from the specified
(boolean/char/byte/short/int/long/float/double/Object) array:
{code:title=org.apache.commons.lang3.ArrayUtils.java|borderStyle=solid}
public static <T> T[] removeElementAll(final T[] array, final Object
element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return clone(array);
}
int[] indices = new int[array.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(array, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (T[]) removeAll((Object) array, Arrays.copyOfRange(indices, 0,
count));
}
{code}
or maybe better:
{code:title=org.apache.commons.lang3.ArrayUtils.java|borderStyle=solid}
public static <T> T[] removeElement(final T[] array, final Object element,
boolean removeAll) {
if(!removeAll) {
return removeElement(array, element)
}
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return clone(array);
}
int[] indices = new int[array.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(array, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (T[]) removeAll((Object) array, Arrays.copyOfRange(indices, 0,
count));
}
{code}
Here are complete code and unit test:
{code:title=org.apache.commons.lang3.ArrayUtils.java|borderStyle=solid}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static boolean[] removeElement(final boolean[] a, final boolean
element, boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (boolean[]) removeAll((Object) a,
Arrays.copyOfRange(indices, 0, count));
}
}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static char[] removeElement(final char[] a, final char element,
boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (char[]) removeAll((Object) a, Arrays.copyOfRange(indices,
0, count));
}
}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static byte[] removeElement(final byte[] a, final byte element,
boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (byte[]) removeAll((Object) a, Arrays.copyOfRange(indices,
0, count));
}
}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static short[] removeElement(final short[] a, final short element,
boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (short[]) removeAll((Object) a, Arrays.copyOfRange(indices,
0, count));
}
}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static int[] removeElement(final int[] a, final int element, boolean
removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (int[]) removeAll((Object) a, Arrays.copyOfRange(indices, 0,
count));
}
}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static long[] removeElement(final long[] a, final long element,
boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (long[]) removeAll((Object) a, Arrays.copyOfRange(indices,
0, count));
}
}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static float[] removeElement(final float[] a, final float element,
boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (float[]) removeAll((Object) a, Arrays.copyOfRange(indices,
0, count));
}
}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static double[] removeElement(final double[] a, final double
element, boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (double[]) removeAll((Object) a, Arrays.copyOfRange(indices,
0, count));
}
}
/**
* Removes the occurrences 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. <code>null</code> will be returned if the input
array is <code>null</code>
*
* @param array
* @param element
* @param removeAll
* removes all the occurrences of the specified element it's
true. otherwise, removes the first
* occurrence of the specified element
* @return A new array containing the existing elements except the
occurrences of the specified element.
*/
public static <T> T[] removeElement(final T[] a, final Object element,
boolean removeAll) {
int index = indexOf(a, element);
if (index == INDEX_NOT_FOUND) {
return clone(a);
} else if (!removeAll || index >= a.length - 1) {
return remove(a, index);
} else {
int[] indices = new int[a.length];
int count = 0;
indices[count++] = index;
for (;;) {
index = indexOf(a, element, ++index);
if (index == INDEX_NOT_FOUND) {
break;
} else {
indices[count++] = index;
}
}
return (T[]) removeAll((Object) a, Arrays.copyOfRange(indices, 0,
count));
}
}
{code}
{code:title=org.apache.commons.lang3.ArrayUtilsRemoveTest.java|borderStyle=solid}
@Test
public void testRemoveElementForAllArray() {
{
boolean[] a = null;
assertNull(ArrayUtils.removeElement(a, true, true));
a = new boolean[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY,
ArrayUtils.removeElement(a, true, true)));
a = new boolean[] { true };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY,
ArrayUtils.removeElement(a, true, true)));
a = new boolean[] { true, true };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY,
ArrayUtils.removeElement(a, true, true)));
a = new boolean[] { false, true, true, false, true };
assertTrue(Arrays.equals(new boolean[] { false, false },
ArrayUtils.removeElement(a, true, true)));
a = new boolean[] { false, true, true, false, true };
assertTrue(Arrays.equals(new boolean[] { true, true, true },
ArrayUtils.removeElement(a, false, true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, true, false));
a = new boolean[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY,
ArrayUtils.removeElement(a, true, false)));
a = new boolean[] { true };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY,
ArrayUtils.removeElement(a, true, false)));
a = new boolean[] { true, true };
assertTrue(Arrays.equals(new boolean[] { true },
ArrayUtils.removeElement(a, true, false)));
a = new boolean[] { false, true, true, false, true };
assertTrue(Arrays.equals(new boolean[] { false, true, false, true
}, ArrayUtils.removeElement(a, true, false)));
a = new boolean[] { false, true, true, false, true };
assertTrue(Arrays.equals(new boolean[] { true, true, false, true },
ArrayUtils.removeElement(a, false, false)));
}
{
char[] a = null;
assertNull(ArrayUtils.removeElement(a, '2', true));
a = new char[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY,
ArrayUtils.removeElement(a, '2', true)));
a = new char[] { '2' };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY,
ArrayUtils.removeElement(a, '2', true)));
a = new char[] { '2', '2' };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY,
ArrayUtils.removeElement(a, '2', true)));
a = new char[] { '1', '2', '2', '3', '2' };
assertTrue(Arrays.equals(new char[] { '1', '3' },
ArrayUtils.removeElement(a, '2', true)));
a = new char[] { '1', '2', '2', '3', '2' };
assertTrue(Arrays.equals(new char[] { '1', '2', '2', '3', '2' },
ArrayUtils.removeElement(a, '4', true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, '2', false));
a = new char[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY,
ArrayUtils.removeElement(a, '2', false)));
a = new char[] { '2' };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY,
ArrayUtils.removeElement(a, '2', false)));
a = new char[] { '2', '2' };
assertTrue(Arrays.equals(new char[] { '2' },
ArrayUtils.removeElement(a, '2', false)));
a = new char[] { '1', '2', '2', '3', '2' };
assertTrue(Arrays.equals(new char[] { '1', '2', '3', '2' },
ArrayUtils.removeElement(a, '2', false)));
a = new char[] { '1', '2', '2', '3', '2' };
assertTrue(Arrays.equals(new char[] { '1', '2', '2', '3', '2' },
ArrayUtils.removeElement(a, '4', false)));
}
{
byte[] a = null;
assertNull(ArrayUtils.removeElement(a, (byte) 2, true));
a = new byte[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY,
ArrayUtils.removeElement(a, (byte) 2, true)));
a = new byte[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY,
ArrayUtils.removeElement(a, (byte) 2, true)));
a = new byte[] { 2, 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY,
ArrayUtils.removeElement(a, (byte) 2, true)));
a = new byte[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new byte[] { 1, 3 },
ArrayUtils.removeElement(a, (byte) 2, true)));
a = new byte[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new byte[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, (byte) 4, true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, (byte) 2, false));
a = new byte[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY,
ArrayUtils.removeElement(a, (byte) 2, false)));
a = new byte[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY,
ArrayUtils.removeElement(a, (byte) 2, false)));
a = new byte[] { 2, 2 };
assertTrue(Arrays.equals(new byte[] { 2 },
ArrayUtils.removeElement(a, (byte) 2, false)));
a = new byte[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 2 },
ArrayUtils.removeElement(a, (byte) 2, false)));
a = new byte[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new byte[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, (byte) 4, false)));
}
{
short[] a = null;
assertNull(ArrayUtils.removeElement(a, (short) 2, true));
a = new short[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY,
ArrayUtils.removeElement(a, (short) 2, true)));
a = new short[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY,
ArrayUtils.removeElement(a, (short) 2, true)));
a = new short[] { 2, 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY,
ArrayUtils.removeElement(a, (short) 2, true)));
a = new short[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new short[] { 1, 3 },
ArrayUtils.removeElement(a, (short) 2, true)));
a = new short[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new short[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, (short) 4, true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, (short) 2, false));
a = new short[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY,
ArrayUtils.removeElement(a, (short) 2, false)));
a = new short[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY,
ArrayUtils.removeElement(a, (short) 2, false)));
a = new short[] { 2, 2 };
assertTrue(Arrays.equals(new short[] { 2 },
ArrayUtils.removeElement(a, (short) 2, false)));
a = new short[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new short[] { 1, 2, 3, 2 },
ArrayUtils.removeElement(a, (short) 2, false)));
a = new short[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new short[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, (short) 4, false)));
}
{
int[] a = null;
assertNull(ArrayUtils.removeElement(a, 2, true));
a = new int[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new int[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new int[] { 2, 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new int[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new int[] { 1, 3 },
ArrayUtils.removeElement(a, 2, true)));
a = new int[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new int[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, 4, true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, 2, false));
a = new int[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY,
ArrayUtils.removeElement(a, 2, false)));
a = new int[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY,
ArrayUtils.removeElement(a, 2, false)));
a = new int[] { 2, 2 };
assertTrue(Arrays.equals(new int[] { 2 },
ArrayUtils.removeElement(a, 2, false)));
a = new int[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new int[] { 1, 2, 3, 2 },
ArrayUtils.removeElement(a, 2, false)));
a = new int[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new int[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, 4, false)));
}
{
long[] a = null;
assertNull(ArrayUtils.removeElement(a, 2, true));
a = new long[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new long[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new long[] { 2, 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new long[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new long[] { 1, 3 },
ArrayUtils.removeElement(a, 2, true)));
a = new long[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new long[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, 4, true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, 2, false));
a = new long[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY,
ArrayUtils.removeElement(a, 2, false)));
a = new long[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY,
ArrayUtils.removeElement(a, 2, false)));
a = new long[] { 2, 2 };
assertTrue(Arrays.equals(new long[] { 2 },
ArrayUtils.removeElement(a, 2, false)));
a = new long[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new long[] { 1, 2, 3, 2 },
ArrayUtils.removeElement(a, 2, false)));
a = new long[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new long[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, 4, false)));
}
{
float[] a = null;
assertNull(ArrayUtils.removeElement(a, 2, true));
a = new float[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new float[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new float[] { 2, 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new float[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new float[] { 1, 3 },
ArrayUtils.removeElement(a, 2, true)));
a = new float[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new float[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, 4, true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, 2, false));
a = new float[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY,
ArrayUtils.removeElement(a, 2, false)));
a = new float[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY,
ArrayUtils.removeElement(a, 2, false)));
a = new float[] { 2, 2 };
assertTrue(Arrays.equals(new float[] { 2 },
ArrayUtils.removeElement(a, 2, false)));
a = new float[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new float[] { 1, 2, 3, 2 },
ArrayUtils.removeElement(a, 2, false)));
a = new float[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new float[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, 4, false)));
}
{
double[] a = null;
assertNull(ArrayUtils.removeElement(a, 2, true));
a = new double[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new double[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new double[] { 2, 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY,
ArrayUtils.removeElement(a, 2, true)));
a = new double[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new double[] { 1, 3 },
ArrayUtils.removeElement(a, 2, true)));
a = new double[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new double[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, 4, true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, 2, false));
a = new double[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY,
ArrayUtils.removeElement(a, 2, false)));
a = new double[] { 2 };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY,
ArrayUtils.removeElement(a, 2, false)));
a = new double[] { 2, 2 };
assertTrue(Arrays.equals(new double[] { 2 },
ArrayUtils.removeElement(a, 2, false)));
a = new double[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new double[] { 1, 2, 3, 2 },
ArrayUtils.removeElement(a, 2, false)));
a = new double[] { 1, 2, 2, 3, 2 };
assertTrue(Arrays.equals(new double[] { 1, 2, 2, 3, 2 },
ArrayUtils.removeElement(a, 4, false)));
}
{
String[] a = null;
assertNull(ArrayUtils.removeElement(a, "2", true));
a = new String[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY,
ArrayUtils.removeElement(a, "2", true)));
a = new String[] { "2" };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY,
ArrayUtils.removeElement(a, "2", true)));
a = new String[] { "2", "2" };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY,
ArrayUtils.removeElement(a, "2", true)));
a = new String[] { "1", "2", "2", "3", "2" };
assertTrue(Arrays.equals(new String[] { "1", "3" },
ArrayUtils.removeElement(a, "2", true)));
a = new String[] { "1", "2", "2", "3", "2" };
assertTrue(Arrays.equals(new String[] { "1", "2", "2", "3", "2" },
ArrayUtils.removeElement(a, "4", true)));
// test false ...
a = null;
assertNull(ArrayUtils.removeElement(a, "2", false));
a = new String[0];
assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY,
ArrayUtils.removeElement(a, "2", false)));
a = new String[] { "2" };
assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY,
ArrayUtils.removeElement(a, "2", false)));
a = new String[] { "2", "2" };
assertTrue(Arrays.equals(new String[] { "2" },
ArrayUtils.removeElement(a, "2", false)));
a = new String[] { "1", "2", "2", "3", "2" };
assertTrue(Arrays.equals(new String[] { "1", "2", "3", "2" },
ArrayUtils.removeElement(a, "2", false)));
a = new String[] { "1", "2", "2", "3", "2" };
assertTrue(Arrays.equals(new String[] { "1", "2", "2", "3", "2" },
ArrayUtils.removeElement(a, "4", false)));
}
}
{code}
> Add removeElementAll method in class ArrayUtils
> -----------------------------------------------
>
> Key: LANG-1074
> URL: https://issues.apache.org/jira/browse/LANG-1074
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.*
> Affects Versions: 3.3.2
> Reporter: haiyang li
> Priority: Minor
> Fix For: Patch Needed
>
> Attachments: LANG-1074.patch.txt
>
>
> Could we add the method: removeElementAll to remove all the occurrences of
> the specified element from the specified
> (boolean/char/byte/short/int/long/float/double/Object) array:
> {code:title=org.apache.commons.lang3.ArrayUtils.java|borderStyle=solid}
> public static <T> T[] removeElementAll(final T[] array, final Object
> element) {
> int index = indexOf(array, element);
> if (index == INDEX_NOT_FOUND) {
> return clone(array);
> }
> int[] indices = new int[array.length - index];
> int count = 0;
> indices[count++] = index;
> for (;;) {
> index = indexOf(array, element, ++index);
> if (index == INDEX_NOT_FOUND) {
> break;
> } else {
> indices[count++] = index;
> }
> }
> return (T[]) removeAll((Object) array, Arrays.copyOfRange(indices, 0,
> count));
> }
> {code}
> or maybe better:
> {code:title=org.apache.commons.lang3.ArrayUtils.java|borderStyle=solid}
> public static <T> T[] removeElement(final T[] a, final Object element,
> boolean removeAll) {
> int index = indexOf(a, element);
> if (index == INDEX_NOT_FOUND) {
> return clone(a);
> } else if (!removeAll || index >= a.length - 1) {
> return remove(a, index);
> } else {
> int[] indices = new int[a.length - index];
> int count = 0;
> indices[count++] = index;
> for (;;) {
> index = indexOf(a, element, ++index);
> if (index == INDEX_NOT_FOUND) {
> break;
> } else {
> indices[count++] = index;
> }
> }
> return (T[]) removeAll((Object) a, Arrays.copyOfRange(indices, 0,
> count));
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)