Java Program To Insert an Element at Specified Position in an Array
In this tutorial, we will learn how to add an element to a given position in an array. The easiest way to do this is by shifting the elements and then inserting the element at a specific position. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input:
Original Array: 5 7 2 3 1 5 6 8
Element: 55
Position: 2
Output: 5 7 55 2 3 1 5 6 8
Program 1: Add an Element in a given Position in an Array
In this approach, we will use loops to insert an element at a specific position.
Algorithm
- Start
- Declare an Array
- Initialize the Array.
- Declare the element to be inserted and position where to be inserted.
- Declare a new Array with +1 size.
- Use a for loop to traverse through each element.
- First insert all the elements till the position.
- Then, insert the element at the specific position.
- Insert the rest of the elements.
- Return the new array.
- Print the updated array.
- Stop.
Below is the code for the same.
The below program demonstrates how to add an element at a specific position in an array using loops.
/*Java Program to add an element in an Array at a specific position*/
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
//Method to add an element in the given specific position
public static int[] addElement(int n, int arr[], int ele, int pos)
{
int i;
// create a new array of size n+1
int newarr[] = new int[n + 1];
// insert the elements from the old array into the new array
for (i = 0; i < n + 1; i++)
{
if (i < pos - 1)
newarr[i] = arr[i]; // insert all elements till position
else if (i == pos - 1)
newarr[i] = ele; // then insert element at specific position
else
newarr[i] = arr[i - 1]; // then insert rest of the elements
}
return newarr;
}
//Driver Method
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n; //Array Size Declaration
System.out.println("Enter the number of elements :");
n=sc.nextInt(); //Array Size Initialization
int arr[]=new int[n]; //Array Declaration
System.out.println("Enter the elements of the array :");
for(int i=0;i<n;i++) //Array Initialization
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the elements you want to insert :");
int ele = sc.nextInt();
// Position to insert
System.out.println("Enter the position where you want to insert :");
int pos = sc.nextInt();
arr = addElement(n, arr, ele, pos);
// print the updated array
System.out.println("\nArray with " + ele + " inserted at position " + pos + ":\n" + Arrays.toString(arr));
}
}
Enter the number of elements: 10
Enter the elements of the array: 8 7 6 9 5 3 4 1 2 9
Enter the elements you want to insert: 22
Enter the position where you want to insert: 2
Array with 22 inserted at position 2:
[8, 22, 7, 6, 9, 5, 3, 4, 1, 2, 9]
Program 2: Add an Element in a given Position in an Array
In this approach, we will convert the array to an array list in order to insert an element at a specific position.
Algorithm
- Start
- Declare an Array
- Initialize the Array.
- Declare the element to be inserted and position where to be inserted.
- Declare a separate method that will insert the element.
- Convert the array to the array list.
- Add the element at the position.
- Convert the list back to the array.
- Now, print the original array.
- Display the updated array.
- Stop.
Below is the code for the same.
The below program demonstrates how to add an element at a specific position in an array using Array Lists.
/*Java Program to add an element in an Array at a specific position*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Collections;
public class Main
{
//Method to add an element in the given specific position
private static void addElement(Integer[] arr, int element, int position)
{
// Coverting array to ArrayList
List<Integer> list = new ArrayList<>(Arrays.asList(arr));
// Adding the element at position
list.add(position - 1, element);
// Converting the list back to array
arr = list.toArray(arr);
// Printing the original array
System.out.println("Initial Array:\n" + Arrays.toString(arr));
// Printing the updated array
System.out.println("\nArray with " + element + " inserted at position "+ position + ":\n" + Arrays.toString(arr));
}
//Driver Method
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n; //Array Size Declaration
System.out.println("Enter the number of elements :");
n=sc.nextInt(); //Array Size Initialization
Integer arr[]=new Integer[n]; //Array Declaration
System.out.println("Enter the elements of the array :");
for(int i=0;i<n;i++) //Array Initialization
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the elements you want to insert :");
int ele = sc.nextInt();
// Position to insert
System.out.println("Enter the position where you want to insert :");
int pos = sc.nextInt();
// Calling the function to insert
addElement(arr, ele, pos);
}
}
Enter the number of elements :
10
Enter the elements of the array :
4 5 3 6 8 9 1 2 7 6
Enter the elements you want to insert :
21
Enter the position where you want to insert :
2
Initial Array:
[4, 5, 3, 6, 8, 9, 1, 2, 7, 6]
Array with 21 inserted at position 2:
[4, 21, 5, 3, 6, 8, 9, 1, 2, 7, 6]