Removing An Element From An Array In Java

Hey guys!! Welcome to flower brackets blog. This program will show you removing an element from an array in java. Below is java program,

Example: Removing An Element From An Array In Java

public class DeleteElementFromArray
{
   public static void main(String[] args)
   {
      System.out.println("Before removing or deleting an element from an array:");
      int[] example = {2,4,6,8,10};
      int position = 5;

      for(int x = 0; x < example.length; x++)
      {
         System.out.println(example[x]);
      }

      for(int i = 0; i < example.length; i++)
      {
         if(position == i)
         {
            for(int y = i + 1; i < example.length - 1; y++)
            {
               example[i] = example[y];
            }
         }
      }

      System.out.println("After removing or deleting an element from an array:");
      for(int a = 0; a < example.length - 1; a++)
      {
         System.out.println(example[a]);
      }
   }
}

Output:

Removing An Element From An Array In Java


using Arrays toString method

Here let’s learn how to remove an element from an array using arrays dot tostring method. Here’s the example,

import java.util.Arrays;

public class RemoveElementArray
{
   public static void main(String[] args)
   {
      int[] deleteArray = {63, 86, 82, 66, 95, 69, 55, 64, 23, 32};
      System.out.println("Delete element from array java : "); 
      System.out.println("Before : " + Arrays.toString(deleteArray)); 
      int remove = 2;

      for(int a = remove; a < deleteArray.length - 1; a++)
      {
         deleteArray[a] = deleteArray[a + 1];
      }
 
      System.out.println("After : " + Arrays.toString(deleteArray));
   }
}

Output:

Delete element from array java :
Before : [63, 86, 82, 66, 95, 69, 55, 64, 23, 32]
After : [63, 86, 66, 95, 69, 55, 64, 23, 32, 32]

Also Read – Insertion Sort In Java

As you can see from the output, the last and second last element in the array (after removing an element) is repeated twice. This is because in arrays, we cannot alter the size of an array after the removal.


using Apache Commons ArrayUtils

Now let’s delete an element from an array java using Apache Commons ArrayUtils. Here’s the code,

import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

public class DeleteElement
{
   public static void main(String[] args)
   {
      int[] example = new int[] {63, 86, 82, 66, 95, 69, 55, 64, 23, 32};
      System.out.println("Delete element from array java : ");
      System.out.println("Before array size : " + example.length);
      System.out.println("Elements : " + Arrays.toString(example));
      example = ArrayUtils.remove(example, 3); // removing element at index 3
      System.out.println("After array size : " + example.length);
      System.out.println("Elements : " + Arrays.toString(example));
   }
}

Output:

Delete element from array java :
Before array size : 10
Elements : {63, 86, 82, 66, 95, 69, 55, 64, 23, 32}
After array size : 9
Elements : {63, 86, 82, 95, 69, 55, 64, 23, 32}


using remove method

Here we will learn removing elements from an array in java using remove method. First we will see the example and then go to the explanation,

import java.util.ArrayList;
import java.util.List;

public class DeleteElement
{
   public static void main(String[] args)
   {
      List li = new ArrayList();
      li.add(63);
      li.add(86);
      li.add(82);
      li.add(66);
      li.add(95);
 
      li.remove(1); // removes element at index 1, that is, 86
 
      li.remove(1); // now element 82 is removed
 
      System.out.println("After removing elements from ArrayList : " + li);
   }
}

Output:

After removing elements from ArrayList : [63, 66, 95]

In ArrayList we have two overloaded remove methods. One is, remove (int index) and the example is as shown above and the is other explained below.

So remove (int index) accepts index of object to be removed. As you can see the output the passed parameter is considered as index.


Now we will see remove (Object obj) which accepts object to be removed with an example,

import java.util.ArrayList;
import java.util.List;

public class DeleteElement1
{
   public static void main(String[] args)
   {
      List li = new ArrayList();
      li.add(63);
      li.add(86);
      li.add(82);
      li.add(66);
      li.add(95);
 
      li.remove(new Integer(1)); // removes element at index 1, that is, 86
 
      li.remove(new Integer(2)); // now element 82 is removed
 
      System.out.println("After removing elements from ArrayList : " + li);
   }
}

Output:

After removing elements from ArrayList : [63, 86, 82]

NOTE:

ArrayList.remove() is not recommended to use while iterating over elements. Because it may lead to ConcurrentModificationException.


using Iterator remove method

Now we will learn how to remove a value from an array in java using Iterator.remove() method.

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class DeleteArrayJava
{
   public static void main(String[] args)
   {
      List li = new ArrayList();
      li.add(10);
      li.add(20);
      li.add(30);
      li.add(07);
      li.add(06);
 
      Iterator ite = li.iterator();
      while (ite.hasNext())
      {
         int a = (Integer)ite.next();
         if (a < 10)
            ite.remove();
      }
 
      System.out.println("After removing elements from ArrayList : " + li);
   }
}

Output:

After removing elements from ArrayList : [10, 20, 30]


using scanner class

Let’s learn how to delete an element from an array in java using scanner class. Here’s the example,

import java.util.Scanner;

public class DeleteArrayDemo
{
   public static void main(String[] args)
   {
      int arraySize, a, del, number = 0;
      int arr[] = new int[50];
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Please enter array size : ");
      arraySize = sc.nextInt();
 
      System.out.print("Please enter array elements : ");
      for(a = 0; a < arraySize; a++)
      {
         arr[a] = sc.nextInt();
      }
 
      System.out.print("Please enter element to be deleted : ");
      del = sc.nextInt();
      for(a = 0; a < arraySize; a++)
      {
         if(arr[a] == del)
         {
            for(int b = a; b < (arraySize - 1); b++)
            {
               arr[b] = arr[b + 1];
            }
            number++;
            break;
         }
      }
      if(number == 0)
      {
         System.out.print("Sorry!! Element not found..!!!");
      }
      else
      {
         System.out.print("Hurray!!! Element Deleted.....");
         System.out.print("\nAfter deleting elements from ArrayList : \n");
         for(a = 0; a < (arraySize - 1); a++)
         {
            System.out.print(arr[a] + " ");
         }
      }
      sc.close();
   }
}

Output:

Please enter array size : 5
Please enter array elements :
63
86
82
66
95
Please enter element to be deleted : 82
Hurray!!! Element Deleted…..
After deleting elements from ArrayList :
63 86 66 95


conclusion

That’s it guys. This is all about how to remove or delete an element from an array in java.

I hope you have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

You May Also Like