There is no direct way to remove elements from Array in Java. Though Array in Java is objects, it doesn't provide any methods to add(), remove() or search an element in Array. This is the reason Collection classes like ArrayList and HashSet are very popular. Thanks to Apache Commons Utils, You can use there ArrayUtils class to remove an element from array more easily than by doing it yourself. One thing to remember is that Arrays are fixed size in Java, once you create an array you can not change their size, which means removing or deleting an item doesn't reduce the size of the array. This is in-fact main difference between Array and ArrayList in Java. What you need to do is create a new array and copy remaining content of this array into a new array using System.arrayCopy() or any other means. For Object arrays, You can also convert Array to List and then remove a particular object and convert List back to the array. One way to avoid this hassle is using ArrayList instead of Array in the first place.
How to delete element from Array in Java
Here is a complete code example of how to remove an element from Array in Java . In this example, we have used a primitive array, particularly int array and Apache commons ArrayUtils to remove an integer based on its index. ArrayUtils also provided several overloaded remove() method for the different type of primitive arrays e.g. int, long, float and double.
That's all on How to remove an element from Array in Java. You can create your own method to delete objects from Array as well but ArrayUtils is tried and tested by Java community and offer a convenient way to delete an element from Array in Java. It’s always better to leverage a library method instead of creating new in Java development.
Other Java programming tutorials from Java67 blog you may like