Signup/Sign In

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

  1. Start
  2. Declare an Array
  3. Initialize the Array.
  4. Declare the element to be inserted and position where to be inserted.
  5. Declare a new Array with +1 size.
  6. Use a for loop to traverse through each element.
  7. First insert all the elements till the position.
  8. Then, insert the element at the specific position.
  9. Insert the rest of the elements.
  10. Return the new array.
  11. Print the updated array.
  12. 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

  1. Start
  2. Declare an Array
  3. Initialize the Array.
  4. Declare the element to be inserted and position where to be inserted.
  5. Declare a separate method that will insert the element.
  6. Convert the array to the array list.
  7. Add the element at the position.
  8. Convert the list back to the array.
  9. Now, print the original array.
  10. Display the updated array.
  11. 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]



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.