Remove Element From Array Java

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

Remove Element From Array Java

Example: remove element from array 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 = 4;

      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:

Remove Element From Array Java


Java program to delete an element in an array using Arrays toString method

Here let’s learn how to remove an element from an array using Arrays.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.


Java program to delete an element in an array using apache commons ArrayUtils

Now let’s delete an element in 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}


How to remove an element from ArrayList in Java?

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

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

public class RemoveElementDemo
{
   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]

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 how to remove elements by value and passed parameter is considered as index,

Also Read – Remove Vowels From String Java

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

public class RemoveElementExample
{
   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. Instead use Iterator.remove() method


Removing element from ArrayList through Iterator

Here we will learn removing element from an array using Iterator.remove() method,

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

public class IteratorRemoveDemo
{
   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]


Java program to delete an element in an array using scanner class

Let’s learn java program to delete specified integer from an array using scanner class. Here’s the example,

import java.util.Scanner;

public class DeleteElementDemo
{
   public static void main(String[] args)
   {
      int num, p, temp = 1, delete = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter array size/no. of elements : ");
      num = sc.nextInt();
      int arr[] = new int[num];
      System.out.println("Please enter array elements : ");
      for(int a = 0; a < num; a++) 
      {
         arr[a] = sc.nextInt();
      }
      System.out.print("Please enter element you want to delete : ");
      p = sc.nextInt();
      for(int a = 0; a < num; a++) 
      {
         if(arr[a] == p)
         {
            temp = 1;
            delete = a;
            break;
         }
         else
         {
            temp = 0;
         }
      }
      if(temp == 1)
      {
         for(int a = delete + 1; a < num; a++)
         {
            arr[a - 1] = arr[a];
         }
         System.out.print("After deleting elements from array : ");
         for(int a = 0; a < num - 2; a++) 
         {
            System.out.print(arr[a] + ",");
         }
         System.out.print(arr[num - 2]);
      }
      else
      {
         System.out.println("Sorry!!Element not found");
      }
      sc.close();
   }
}

Output:

Please enter array size/no. of elements : 5
Please enter array elements :
63
86
82
66
95
Please enter element you want to delete : 82
After deleting elements from array : 63,86,66,95


Conclusion

That’s it guys. This is all about how to 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.

Related Posts