Java Program to Delete an Element From an Array

Java Program to Delete an Element From an Array. using this program we can delete the array element in java. We begin iterating from the position where the element is to be removed. The reason for this is that all elements after the deleted element will be shifted to the left by one position.

import java.util.*;
class Main
public static void main(String args[])
Scanner sc = new Scanner(;
int i,n,pos;
System.out.println("Enter no of elements in the array") ;
n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements") ;
a[i] = sc.nextInt();
System.out.println("Enter the element location of an array to delete the number");
pos = sc.nextInt();
System.out.println("After deleting the element\n");
System.out.println("a["+i+"] = "+a[i]);



Enter no of elements in the array
Enter the elements
Enter the element location of an array to delete the number
Nothing is selected to erase is

a[0] = 12
a[1] = 45
a[2] = 89
a[3] = 56

Array in Delete Program

A data structure that contains a group of elements is known as an array. These elements are typically all of the same data type, such as an integer or a string. In computer programs, arrays are commonly used to organize data so that a related set of values can be easily sorted or searched.

Initilize Array in Java Program for Delete Program

In Java, we declare an array by giving it a type and a name, just like we do other variables: myArray;  We can use the following shorthand syntax to initialise or instantiate an array as we declare it, meaning we assign values as we create the array: int[] myArray = {13, 14, 15};

Steps to Delete the Element in Java

Get the index and the array.
With the array elements, create an ArrayList.
The remove() method is used to remove the specified index element.
Using the mapToInt() and toArray() methods, create a new ArrayList array.
Return the array that has been formed

For Loop Uses in Array

To visit each element of an array, we can use iteration with a for a loop. The process is known as traversing the array. Simply begin at 0 and loop until the index is less than the array’s length. The for loop or for Each loop can be used to traverse an array. Instead of printing each element one by one, you can use the for loop to iterate the index from 0 to the length of the array (ArrayName. length) and access elements at each index. Using this looping concept we can execute this java program to delete an element in an array.