How to remove element from Array in Java with Example

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

How to remove or delete object from Array in Java with exampleHere 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.

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

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 Array using Apache Commons ArrayUtils
        test = ArrayUtils.remove(test, 2); //removing element at index 2
        //Size of array must be 1 less than original array after deleting an element
        System.out.println("Size of array after removing an element  : " + test.length);
        System.out.println("Content of Array after removing an object : "
                           + Arrays.toString(test));

Original Array : size : 5
Contents : [101, 102, 103, 104, 105]
Size of 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 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.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java

Other Java programming tutorials from Java67 blog you may like


  1. thanks for the post

  2. THIS IS USELESS import org.apache.commons.lang.ArrayUtils; DOESN'T NAME A TYPE IN A PACKAGE.

  3. Yeah.. THIS IS USELESS (import org.apache.commons.lang.ArrayUtils;) doesn't recognize by the package.

  4. You don't have to import the ArrayUtils class, it comes from the java.lang package which means it can be used without importing.. Just cancel the second import and your code should work just fine

  5. it doesnt come in java.lang pkg...not working

  6. You need the apache commons JAR file in your build path of course....

    impost org.apache.commons.lang.ArrayUtils;

  7. it is not there in java package and we ned to add dependency or add jar in classpath which is apache commons

  8. thanks for this post, i would appreciate if u could give a hint about solving this problem (remove an element from an array) without using collections(ArrayList etc)

  9. This is something which cant be considered as the best solution. Lets have a look on my below mentioned code:
    public class DeletionFromArray {

    public static int[] deleteArray(int arr[], int i )

    for(int j =i;j<arr.length - 1;j++)
    arr[j]= arr[j+1];
    return arr;

    public static void main(String[] args) {

    int input[] = {20, 10, 2, 4, 3};
    int output[] = deleteArray(input, 1);
    for(int k=0; k<output.length - 1;k++)



  10. I guess in some of these questions they won't want you to use inbuilt classes and all. Here is a way to get it done with plain primitives.

    static int removeElement(int [] arr, int elem) {
    int length = 0;

    for (int i = 0; i < arr.length; i++) {
    if (arr[i] != elem) arr[length++] = arr[i];

    System.out.println(Arrays.toString(Arrays.copyOf(arr, length)));
    return length;

    1. You are my saviour! i just imported java.util.Arrays; and this worked flawlessly!

  11. this does't works for me.what do we have to import

  12. Object myStore= new Object[10];
    int actSize=myStore.length-1;
    public Object remove(int index){
    Object obj=myStore[index];
    int temp=index;
    return obj;


  13. This function will remove the index element and return the new array:

    public static int[] removeElement(int[] arr, int index){

    int[] newArr = new int[arr.length-1];
    int j = 0;
    for(int i = 0; i<arr.length; i++){
    if(i != index){
    newArr[j] = arr[i];

    return newArr;