Java Program to Delete Element of Array At Given Location

delete element at given location java

In this tutorial, we will learn writing Java program to delete an element of an array at given index and print the updated array. Our program will delete the element from the position specified by the user from the given array.

For Example

Case 1: if the given array is [1, 2, 3, 4] and the user gives input 3 to remove element at position 2.

              The output should be [1, 2, 4].

Case 2: if the given array is {9, 2, 4, 8} and the user gives input 4 to remove element at position 4.

          The output should be [9, 2, 4].

Java Program to Delete Element of Array At Given Location

Output

Explanation

From the input array [1, 2, 3, 4, 5] user input 2, to remove the element at position 2 in the array. Our program will iterate till position 2 in the array and removes the element using statement arr[i] = arr[i+1]. And then returns the new updated array as the output of the program.

So After deleting element at index 2, output will be [1, 2, 4, 5]