In this tutorial, we will learn writing java program to insert an element at the given position of an array. Our program will add an element at the given position (index) of the given array.
For example
Case 1: if the given array is {1, 2, 3, 4} and the user gives input 9 to add at index 2.
The output should be {1, 2, 9, 3, 4}.
Case 2: if the given array is {9, 2, 4, 8} and the user gives input 10 to add at index 1.
The output should be {9, 10, 2, 4, 8}.
Java Program to Insert Element in Array at given Location
Output

Explanation
For the given array {1, 2, 3, 4, 5, 6, }, the user inputs element 7 to add at the index 2 of the array. So we iterate through the elements of the array 2 times and as arr[2] = arr[1], the elements at 2nd index become the 3rd element of the array leaving 2nd index empty which is free to add any element , so as under input 7, so 7 is placed at index 2, using arr[loc]= 7.