Learn to remove the array items in Java by the index positions as well as by the item values.
Note that theoretically, we can remove an array item in two ways:
- Create a new array and copy all items from the original array, except the index or item to be deleted, into a new array. It creates a new array so it may not be a good fit for large-size arrays that require a sizable amount of memory. In this technique, the original array is unaffected.
- Overwrite all the array index locations with the value stored in its next index, starting from index to be deleted to the end of the array. This effectively removes the item at the specified index.
As we do not create a new array, it is more memory efficient. Note that we might want to replace the last index location value with null so that we do not duplicate items in the last of the array.
1. Removing Array Items with ArrayUtils
The ArrayUtils class is from Apache Commons Lang library that provides many practical methods to work with arrays in Java.
For removing array items, it provides the following methods. All methods return a new array, and the original array is not modified. After removing the items, all subsequent array elements are shifted left.
- remove(array, index) – removes the element at the specified
index
from the specifiedarray
. It throws IndexOutOfBoundsException if the index is out of range. - removeAll(array, indices…) – removes all the elements at the specified
indices
from the specifiedarray
. - removeElement(array, item) – removes the first occurrence of the specified
item
from the specifiedarray
. If the array doesn’t contains such an element, no elements are removed from the array. - removeElements(array, items…) – removes occurrences of specified
items
, in specified quantities, from the specifiedarray
. For any element-to-be-removed specified in greater quantities than contained in the original array, no change occurs beyond the removal of the existing matching items. - removeAllOccurrences(array, item) – removes all the occurrences of the specified
item
from the specifiedarray
. If the array doesn’t contains such an element, no elements are removed from the array.
Let us look at a few examples of how to use these APIs.
Java program to remove an item at index position 5.
Integer[] originalArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Integer[] reducedArray = ArrayUtils.remove(originalArray, 5);
//[0, 1, 2, 3, 4, 6, 7, 8, 9]
Java program to remove items at index positions 5, 6 and 7.
Integer[] originalArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Integer[] reducedArray = ArrayUtils.removeAll(originalArray, 5, 6, 7);
//[0, 1, 2, 3, 4, 8, 9]
Java program to remove value 7 from the array.
Integer[] originalArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Integer[] reducedArray = ArrayUtils.removeElement(originalArray, 7);
//[0, 1, 2, 3, 4, 5, 6, 8, 9]
Java program to remove multiple items from an array.
Integer[] originalArray = {1, 1, 2, 2, 3, 3, 3, 4, 4, 4};
Integer[] reducedArray = ArrayUtils.removeElements(originalArray, 1, 2, 3);
//[1, 2, 3, 3, 4, 4, 4]
Integer[] reducedArray = ArrayUtils.removeElements(originalArray, 1, 1, 2, 2, 3);
//[3, 3, 4, 4, 4]
Java program to remove all occurrences of an item from an array.
Integer[] originalArray = {1, 1, 2, 2, 3, 3, 3, 4, 4, 4};
Integer[] reducedArray = ArrayUtils.removeAllOccurrences(originalArray, 4);
//[1, 1, 2, 2, 3, 3, 3]
2. Removing Array Items with Collections
If memory is not a constraint for smaller lists, we may consider using the native Collection APIs to write our custom logic for removing array items.
The most straightforward way will be to convert the array into List because Lists supports a range of easy to use methods for such simple usecases such as :
- remove(index) – removes an item from list at specified index.
- remove(item) – removes a specified item from array.
- removeAll(item) – remove all occurrences of a specified item from array.
- removeIf(predicate) – removes items matching with the specified predicate.
Integer[] originalArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
List<Integer> tempList = new ArrayList<>(Arrays.asList(originalArray));
tempList.remove(7);
tempList.removeAll(5);
Integer[] reducedArray = tempList.toArray(new Integer[0]);
3. Remove and Shift Items in For-loop
Suppose we are in a high memory-sensitive application, and we cannot afford to create a new array. In that case, we can loop the array items, remove the specified item or index, and shift the subsequent items to the left.
The last item will not move in this process, and the last and second-last elements will be duplicates. We can replace the last item with the null
to fix this behavior if needed.
Integer[] originalArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
removeIndexAndShift(originalArray, 6);
//[0, 1, 2, 3, 4, 5, 7, 8, 9, null]
removeIndexAndShift(originalArray, 2);
//[0, 1, 3, 4, 5, 7, 8, 9, null, null]
static <T> void removeIndexAndShift(T[] array, int indexToRemove) {
for (int i = indexToRemove; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
//optionally we can set the last element to null
array[array.length - 1] = null;
}
4. Conclusion
This short tutorial taught us to remove the elements from an array using different techniques. Most techniques create a new array, but we can make changes in the original array using the custom code if we want.
For shorter arrays, we can use the inbuilt APIs such as Collections
or ArrayUtils
. We need to think very carefully about large arrays and then use any of the above-suggested solutions.
Happy Learning !!
Leave a Reply