How to Remove Element From an Array in Java

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated October 22, 2026

Learn Various Methods to Delete or Remove an element from an Array in Java such as Using another array, Using Java 8 Streams, Using ArrayList:

In Java programming, an array is a fixed-sized data structure, so there is no way to remove/delete an element and make an array smaller. You have to create another array that will contain all the elements except the element that you wish to delete or use classes/methods like ArrayList, Stream, and System.arraycopy().

In this article, we will discuss various ways to remove an element from an array in Java, be it by Index or Value..

=> See The Full Java Training Tutorial Series For Beginners

 

Remove Delete an element from an Array

How to Remove an Element From an Array in Java

The procedure of deleting an element based on its index involves creating a new array in which all the elements apart from the one that needs to be deleted are placed. The arrays in java have a fixed size; therefore, deleting an element from the array does not affect the size of the array.

In this tutorial, we will discuss the various methods to delete an element from an array.

It includes:

  • Using another Array
  • Using Java 8 streams
  • Using ArrayList
  • Using System.arraycopy()

Removing an Array Element Using a New Array

This is the traditional and somewhat inefficient method of deleting an array element. Here we define a new array with size less than 1 to the original array. Then we copy the elements from the original array to the new array. But while doing this copying, we skip the element at the specified index.

This way we copy all the elements except the element to be deleted to the new array indicating that the element is deleted.

We can represent this operation pictorially as shown below.

Using another array diagram

Let’s implement this method in a Java program.

import java.util.Arrays;
class Main {
public static void main(String[] args)
    {
        // define original array
        int[] tensArray = { 10,20,30,40,50,60}; 

        // Print the original array
        System.out.println("Original Array: " + Arrays.toString(tensArray)); 

        // the index at which the element in the array is to be removed
        int rm_index = 2; 

        // display index
        System.out.println("Element to be removed at index: " + rm_index); 

        // if array is empty or index is out of bounds, removal is not possible
          if (tensArray == null
            || rm_index< 0
            || rm_index>= tensArray.length) { 

            System.out.println("No removal operation can be performed!!");
        }
        // Create a proxy array of size one less than original array
        int[] proxyArray = new int[tensArray.length - 1]; 

        // copy all the elements in the original to proxy array except the one at index
        for (int i = 0, k = 0; i <tensArray.length; i++) { 

            // check if index is crossed, continue without copying
            if (i == rm_index) {
                continue;
            } 

            // else copy the element
            proxyArray[k++] = tensArray[i];
        } 

       // Print the copied proxy array
       System.out.println("Array after removal operation: " + Arrays.toString(proxyArray));
    }
}

Output:

Using another array

Removing an Array Element Using Java 8 Streams

Streams are a new addition to Java from version 8 onwards. Using Java8 streams, we can delete an element from an array. In order to do this, first, the array is converted to a stream. Then the element at the specified index is deleted using the filter method of streams.

Once the element is deleted, using the ‘map’ and ‘toArray’ methods, the stream is converted back to the array.

The implementation of removing an element from an array using stream is shown below.

import java.util.Arrays;
import java.util.stream.IntStream;
  class Main {
    // Function to remove the element
    public static int[] removeArrayElement(int[] oddArray,
                      int index)
    {
         //array is empty or index is beyond array bounds
         if (oddArray == null || index < 0 || index >= oddArray.length) { 

              return oddArray;
        }
          // delete the element at specified index and return the array
          return IntStream.range(0, oddArray.length) .filter(i -> i != index)
            .map(i ->oddArray[i]).toArray();
    } 

    public static void main(String[] args)  {
         int[] oddArray = { 1, 3,5,7,9,11};     // define array of odd numbers 

       System.out.println("Original Array: " + Arrays.toString(oddArray));   // Print the resultant array
       int index = 2; 	// index at which element is to be removed
       System.out.println("Element to be removed at index: " + index); 	// display index

        // function call removeArrayElement
        oddArray = removeArrayElement(oddArray, index); 

        // Print the resultant array
        System.out.println("Array after deleting element: "  + Arrays.toString(oddArray));
    }
}

Output:

Using Java 8 streams

Removing an Array Element Using ArrayList

We can use an ArrayList to perform this operation. To remove an element from an array, we first convert the array to an ArrayList and then use the ‘remove’ method of ArrayList to remove the element at a particular index.

Once removed, we convert the ArrayList back to the array.

The following implementation shows removing the element from an array using ArrayList.

import java.util.*;
import java.util.stream.*;

class Main {
     public static int[] remove_Element(int[] myArray,  int index)
    {
	if (myArray == null || index < 0 || index >= myArray.length) {
	    System.out.println("non-existing index");
	    return myArray;
	}
	//array to arrayList
	List<Integer>arrayList = IntStream.of(myArray) .boxed().collect(Collectors.toList());
	// Remove the specified element
	arrayList.remove(index); 

	// return the resultant array
	returnarrayList.stream().mapToInt(Integer::intValue).toArray();
    }
    public static void main(String[] args)  {
       int[] myArray = { 11,22,33,44,55,66,77,88,99,111 };
       System.out.println("Original Array: " + Arrays.toString(myArray));
       int index = 10;
       System.out.println("Index at which element is to be deleted: " + index);
       myArray = remove_Element(myArray, index);
       System.out.println("Resultant Array: " + Arrays.toString(myArray) + "\n"); 

      index = 2;
      System.out.println("Index at which element is to be deleted: " + index);
      myArray = remove_Element(myArray, index);
      System.out.println("Resultant Array: " + Arrays.toString(myArray));
    }
}

Output:

UsingArrayList

The above program produces output for two conditions. First, a non-existing index (10) is passed i.e. beyond the current array size. The program displays an appropriate message and does not delete any element.

In the second case, an index = 2 is passed. This time the element at position 2 is deleted and the resultant array is passed.

Removing an Array Element Using System.arraycopy()

This method is similar to the first method except that we use the ‘arrayCopy’ method for copying the elements of the original array into the new array.

First, we copy the elements of the original array from 0 to index into the new array. Next, we copy the elements from index+1 until length into the new array. Thus while copying, we skip the element at the specified index and generate a new array.

This new array indicates the resultant array that is obtained after deleting an element at the specified index.

import java.util.Arrays; 

class Main { 

    public static void main(String[] args)
    {
        // define the array of integers
        int[] intArray = { 10,20,30,40,50 }; 

        // display the original array
        System.out.println("Original Array: "
                           + Arrays.toString(intArray)); 

        // index at which the element is to be deleted
        int index = 2; 

        // the index
        System.out.println("Element to be deleted at index: "
                           + index); 

        // check if the array is empty or index is out of bounds
        if (intArray == null || index < 0  || index >= intArray.length) {
            System.out.println("No removal operation can be performed!!");
        } 

        // create an array to hold elements after deletion
       int[] copyArray = new int[intArray.length - 1]; 

        // copy elements from original array from beginning till index into copyArray
        System.arraycopy(intArray, 0, copyArray, 0, index); 

        // copy elements from original array from index+1 till end into copyArray
        System.arraycopy(intArray, index + 1, copyArray, index, intArray.length - index - 1); 

        // display the copied array after deletion
        System.out.println("Array after deleting an element: "
                           + Arrays.toString(copyArray));
    }
}

Output:

Using System.arraycopy ()

Remove an Element From an Array by Index

Arrays in Java have a fixed size allocation in memory, making it impossible to remove elements dynamically from an array. One way around this is to create an array with one less element compared to the original and then copy all the elements except for the one that has to be removed into the new array.

Array indexes in Java begin with 0, meaning that the first element will be at index 0, the second element will be at index 1, up to index length – 1. The index of the element that has to be removed has to be stated precisely.

Example Method

Here is a sample utility method to remove an element by its index:

Java
import java.util.Arrays;

public class ArrayUtils {
public static int[] removeElement(int[] original, int indexToRemove) {
// Validate the index
if (indexToRemove < 0 || indexToRemove >= original.length) {
throw new IndexOutOfBoundsException("Index: " + indexToRemove + ", Length: " + original.length);
}

// Create a new array with a size one less than the original
int[] result = new int[original.length - 1];

// Copy elements before the target index
for (int i = 0; i < indexToRemove; i++) {
result[i] = original[i];
}

// Copy elements after the target index, shifting them left
for (int i = indexToRemove + 1; i < original.length; i++) {
result[i - 1] = original[i];
}

return result;
}

public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};

// Remove the element at index 2 (which is the value 30)
int[] updatedNumbers = removeElement(numbers, 2);

System.out.println(Arrays.toString(updatedNumbers)); // Output: [10, 20, 40, 50]
}
}

Which Method Should You Use to Remove an Array Element?

The choice between index-based and value-based removal will depend on whether you know the exact location of the item or just its value.

1. Use Index-Based Removal if:

  • You know the exact position: For example, deleting the first element (position 0), the last element, or a particular element chosen from a user interface list based on its position.
  • You care about performance a little more: This approach eliminates the need for a search phase and performs the operation using a known coordinate.
  • You want strict bounds checking: It allows you to instantly detect invalid positions using IndexOutOfBoundsException.

2. Use Value-Based Removal if:

  • You know what must be removed but not its position: For example, removing a particular username, error code, or product code from an array, without searching for the index of the value first.
  • There is a need to handle duplicate values: In this scenario, you can create your algorithm in a way that it either removes all matches or stops after the first match only.

The Optimal Way of Frequent Removal Operations

If your program often requires adding and removing elements, it is not very efficient to use Java arrays (int[], String[], etc.), since in each case a new array will be created. Rather, you should use java.util.ArrayList.:

Java
import java.util.ArrayList;
import java.util.Arrays;

public class ListExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(10, 20, 30, 40));

// Remove by value (removes the Integer object 30)
numbers.remove(Integer.valueOf(30));

// Remove by index (removes the element at index 1, which is 20)
numbers.remove(1);

System.out.println(numbers); // Output: [10, 40]
}
}

Frequently Asked Questions

How to remove one element from an Array?

Java does not provide a direct method to remove an element from the array. But given an index at which the element is to be deleted, we can use ArrayList to remove the element at the specified index.
For this, first, we convert the array to ArrayList and using the remove method we remove the element. Once that is done, we convert the ArrayList back to the array. There are also several other workarounds that we can employ for this purpose.

What does ArrayList remove do?

ArrayList remove method removes the element in the ArrayList at a given index that is provided as an argument.

How do you remove Duplicates from an Array in Java?

Duplicate elements from an array can be removed by using a temporary array that will count the elements one by one and only put the unique elements in the temporary array. An array needs to be sorted to remove the duplicates.

Can you directly remove an element from an array in Java?

No, because arrays in Java are static in nature. The length of an array does not change once declared, therefore an element cannot be directly removed from an array in Java. A new array needs to be created which includes only those elements which need to be retained in the array.

How do you remove an element from an array by index in Java?

A new array with one less element than the original array is created and then all elements except the element at the specified index position are copied into this new array.

How do you remove an element from an array by value in Java?

Determine the index of the item you want to delete, and then create a new array without that particular element. You can use Java 8 Streams to filter out those items. You should determine whether you want to delete the first item found or all such items in case of duplicates.

How do you remove the last element from an array in Java?

Create a new array having a size lesser than the size of the old array by one, and copy all elements except for the last one into the new array. Ensure that the original array has at least one element.

What is the difference between removing an element using an array and an ArrayList?

Arrays have fixed size, which means that removing an element requires creation of a smaller array. The ArrayList class provides dynamic sizing capabilities like remove(index) and remove(object) to make working with frequently added and removed elements easier.

Conclusion

For removing an element from the array in Java, another method needs to be used since the size of the array is fixed. Such tasks can be performed through creating a new array with the required elements, using System.arraycopy(), Java 8 Streams for filtering the elements, or transforming the array into an ArrayList so that it becomes possible to delete elements.

The choice of the optimal method depends on the user’s needs. For example, in case when deletion should be done by the index, such methods as creation of a new array or use of System.arraycopy() are good choices. In the other case, filtering of the elements or other operations will suit better.

=> Check Out The Intensive Java Training Guide Here

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



Leave a Comment