All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Remove Element From Array Java

Last Updated : Mar 11, 2024

Remove Element From Array Java

In this article we will show you the solution of remove element from array java, a data structure containing several elements is called an array. The use of arrays in computer programs is common to organize data so that related values can be quickly searched or sorted.

One continuous memory region can store all array components. In essence, the process involves finding and removing the element at the specified index.

In the next step, the rest of the elements will be copied into a new array.

This would result in the array size being one less than it was initially. We will now discuss the idea of how to remove element from array in java with an example.

Step By Step Guide On Remove Element From Array Java :-

// Java program to remove an element
// from a specific index from an array
import java.util.Arrays;
class TC {
    // Function to remove the element
    public static int[] removeTheElement(int[] arr, int index)
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (arr == null || index < 0
            || index >= arr.length) {
            return arr;
        // Create another array of size one less
        int[] anotherArray = new int[arr.length - 1];
        // Copy the elements except the index
        // from original array to the other array
        for (int i = 0, k = 0; i < arr.length; i++) {
            // if the index is
            // the removal element index
            if (i == index) {
            // if the index is not
            // the removal element index
            anotherArray[k++] = arr[i];
        // return the resultant array
        return anotherArray;
    // Driver Code
    public static void main(String[] args)
        // Get the array
        int[] arr = { 1, 2, 3, 4, 5 };
        // Print the resultant array
        System.out.println("Original Array: "
                           + Arrays.toString(arr));
        // Get the specific index
        int index = 2;
        // Print the index
        System.out.println("Index to be removed: " + index);
        // Remove the element
        arr = removeTheElement(arr, index);
        // Print the resultant array
        System.out.println("Resultant Array: "
                           + Arrays.toString(arr));
  1. The code defines a class named "TC".
  2. The class contains a static method named "removeTheElement" that takes two arguments - an array of integers "arr" and an integer "index".
  3. The function first checks if the array is empty or if the index is not within the array range. If either of the conditions is true, the function returns the original array.
  4. If the array is not empty and the index is within the array range, a new array of one less size is created by allocating memory to anotherArray[].
  5. The elements of the original array except for the element at the specified index are copied into anotherArray[] using a for loop.
  6. The function returns anotherArray[] as its output.
  7. The "main" function of the class initializes an array named "arr" with 5 elements, prints it using Arrays.toString() method, and defines a variable named "index".
  8. The function then calls the removeTheElement method with "arr" and "index" as arguments, replace the value of arr with the resultant array.
  9. Finally, the function prints the resultant array using the Arrays.toString() method.

Conclusion :-

As a result, we have successfully learned how to remove element from array in java with an example.

The methods for deleting elements in an array were illustrated using various examples.

Deleting an element from an ArrayList is much more complex than deleting an element from an Array.

ArrayList should be utilised to access its built-in functions if deletion is being done repeatedly.

A similar process occurs when elements are added to an array. An array can be altered more easily and efficiently using an arrayList due to its dynamic nature.

I hope this article on remove element from array java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪