Write a java program to delete or erase the element in the array in a exact location

Write a java program to delete or erase the element in the array in an exact location. 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(System.in);
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") ;
for(i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
System.out.println("Enter the element location of an array to delete the number");
pos = sc.nextInt();
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
System.out.println("After deleting the element\n");
for(i=0;i<n;i++)
{
System.out.println("a["+i+"] = "+a[i]);
}
}
}

Output:

Enter no of elements in the array
5
Enter the elements
12
45
67
89
56
Enter the element location of an array to delete the number
2
Nothing is selected to erase is

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

What is 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.

How to 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 programs

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.