group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
05 Feb 2019

How to remove elements from array in java?

Many a times, you may have come across the requirement of removing one or more elements from an array. Arrays in java don’t have in-built methods. Java.util.Arrays class provides some utility methods which can be used to perform some operations like sort, search, copy etc on arrays in java. But, there are no methods to remove elements from an array in java.util.Arrays class. 

There are some third party libraries which can be used to remove elements from arrays in java. In this post, I have demonstrated how to remove one or more elements from an existing array using third party library org.apache.commons.lang3.ArrayUtils class. Let’s see some array element removal programs in java using org.apache.commons.lang3.ArrayUtils class.

1) Removing an element from a specific index from an array:

ArrayUtils.remove(T[] inputArray, int index) is used to remove an element at specific index from an array.

2)Removing specific element from an array:

ArrayUtils.removeElement(T[] inputArray, T element) method removes a specific element from an array.

3)Removing multiple elements at different indices from an array

ArrayUtils.removeAll(T[] inputArray, int … indices) method removes multiple elements at different indices from an array.

4) Removing multiple specific elements from an array:

ArrayUtils.removeElements(T[] inputArray, T … elements) method is used to remove multiple given elements from an array

5) Removing all occurrences of an element in an array:

ArrayUtils.removeAllOccurences(T[] inputArray, T element) method removes all occurrences of a given element from an array.