How to Remove an Element from Array in Java with Example (original) (raw)
There is no direct way to remove elements from an Array in Java. Though Array in Java 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 their ArrayUtils class to remove an element from the 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, the main difference between Array and ArrayList in Java.
What you need to do is create a new array and copy the remaining content of this array into a new array using System.arrayCopy() or any other means. In fact, all other APIs and functions you will use do this but then you don't need to reinvent the wheel.
For an Object or Reference array, 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 by using ArrayList instead of Array in the first place.
Btw, if you are a complete beginner in the Java world and don't know much about Java API, particularly the Java Collection framework which provides ready-made classes for common data structure like an array, list, set, hash table, stack, queue, etc, I suggest you to first go through these free Java courses from Coursera and Udemy.
It's one of the most comprehensive courses and covers Java from length to breadth. It's also the most up-to-date course and recently updated for the latest Java version.
How to delete an 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.
The ArrayUtils also provided several overloaded remove() methods for the different types of primitive arrays like int, long, float, and double.
Btw, if you are not familiar with array data structure itself, like it stores elements in a contiguous memory location, provides fast search capability in O(1) time if you know the index and adding and removing elements is very difficult then I also suggest you go through a comprehensive data structure and algorithm course like Data Structures and Algorithms: Deep Dive Using Java on Udemy.
It's one of the most important topics and you just cannot afford to ignore it. This is also your first step towards becoming the better computer Programmer you always wanted to be.
Anyway, here is our Java program to delete an array from a primitive array in Java:
import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;
/**
*
* Java program to show how to remove element from Array in Java
* This program shows How to use Apache Commons ArrayUtils
* to delete elements from primitive array.
*
* @author http://java67.com
*/
public class RemoveObjectFromArray{
public static void main(String args[]) {
//let's create an array for demonstration purpose
int[] test = new int[] { 101, 102, 103, 104, 105};
System.out.println("Original Array : size : "
+ test.length );
System.out.println("Contents : " + Arrays.toString(test));
// let's remove or delete an element from an Array
// using Apache Commons ArrayUtils
test = ArrayUtils.remove(test, 2); //removing element at index 2
// Size of an array must be 1 less than the original array
// after deleting an element
System.out.println("Size of the array after
removing an element : " + test.length);
System.out.println("Content of Array after
removing an object : "
+ Arrays.toString(test));
}
}
Output:
Original Array: size: 5
Contents : [101, 102, 103, 104, 105]
Size of the array after removing an element: 4
Content of Array after removing an object : [101, 102, 104, 105]
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 the 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 a new in Java development.
Other Data Structure and Algorithms You may like
- How to reverse an array in Java? (solution)
- How to remove duplicate elements from the array in Java? (solution)
- 50+ Data Structure and Algorithms Problems from Interviews (list)
- 5 Books to Learn Data Structure and Algorithms in-depth (books)
- How to implement a binary search tree in Java? (solution)
- Postorder binary tree traversal without recursion (solution)
- How to implement a recursive preorder algorithm in Java? (solution)
- 100+ Data Structure Coding Problems from Interviews (questions)
- How to print leaf nodes of a binary tree without recursion? (solution)
- How to remove duplicate elements from an array without using API? (solution)
- Iterative PreOrder traversal in a binary tree (solution)
- How to count the number of leaf nodes in a given binary tree in Java? (solution)
- 75+ Coding Interview Questions for Programmers (questions)
- Recursive InOrder traversal Algorithm (solution)
- Recursive Post Order traversal Algorithm (solution)
- 10 Free Data Structure and Algorithm Courses for Programmers (courses)
Thanks for reading this article so far. If you like this Java Array tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment.
P. S. - If you are looking for some Free Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check these free Data structure courses on Udemy. It's authored by a Google Software Engineer and Algorithm expert and it's completely free of cost.