How to Insert an element at a specific position in an Array in Java
Last Updated :
12 Mar, 2024
An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in Java.
Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos.

Approach 1:
Here's how to do it.
- First get the element to be inserted, say x.
- Then get the position at which this element is to be inserted, say pos.
- Create a new array with the size one greater than the previous size.
- Copy all the elements from previous array into the new array till the position pos.
- Insert the element x at position pos.
- Insert the rest of the elements from the previous array into the new array after the pos.
Below is the implementation of the above approach:
Java
// Java Program to Insert an element
// at a specific position in an Array
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to insert x in arr at position pos
public static int[] insertX(int n, int arr[],
int x, 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
// insert all elements till pos
// then insert x at pos
// then insert rest of the elements
for (i = 0; i < n + 1; i++) {
if (i < pos - 1)
newarr[i] = arr[i];
else if (i == pos - 1)
newarr[i] = x;
else
newarr[i] = arr[i - 1];
}
return newarr;
}
// Driver code
public static void main(String[] args)
{
int n = 10;
int i;
// initial array of size 10
int arr[]
= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// print the original array
System.out.println("Initial Array:\n"
+ Arrays.toString(arr));
// element to be inserted
int x = 50;
// position at which element
// is to be inserted
int pos = 5;
// call the method to insert x
// in arr at position pos
arr = insertX(n, arr, x, pos);
// print the updated array
System.out.println("\nArray with " + x
+ " inserted at position "
+ pos + ":\n"
+ Arrays.toString(arr));
}
}
OutputInitial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Array with 50 inserted at position 5:
[1, 2, 3, 4, 50, 5, 6, 7, 8, 9, 10]
Approach 2:
Here's how to do it.
- First get the element to be inserted, say element.
- Then get the position at which this element is to be inserted, say position.
- Convert array to ArrayList.
- Add element at position using list.add(position, element).
- Convert ArrayList back to array and print.
Below is the implementation of the above approach:
Java
// Java Program to Insert an element
// at a specific position in an Array
// using ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class AddElementAtPositionInArray {
// Method to add element at position
private static void
addElement(Integer[] arr, int element, int position)
{
// Printing the original array
System.out.println("Initial Array:\n"
+ Arrays.toString(arr));
// Converting 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 updated array
System.out.println("\nArray with " + element
+ " inserted at position "
+ position + ":\n"
+ Arrays.toString(arr));
}
// Drivers Method
public static void main(String[] args)
{
// Sample array
Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Element to be inserted
int element = 50;
// Position to insert
int position = 5;
// Calling the function to insert
addElement(arr, element, position);
}
}
OutputInitial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Array with 50 inserted at position 5:
[1, 2, 3, 4, 50, 5, 6, 7, 8, 9, 10]
Similar Reads
How to Add an Element to an Array in Java? In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array. In this article, we will discuss the NewDifferent Ways to Add an Element to an ArrayThere are two different approaches we c
3 min read
How to Add Element at First and Last Position of LinkedList in Java? LinkedList is a part of Collection framework present inside java.util package. This class is an implementation of LinkedList data structure which is a linear data structure where the elements are not stored in a contiguous manner and every element is a separate object with a data field and address f
2 min read
List add(int index, E element) method in Java The add(int index, E ele) method of List interface in Java is used to insert the specified element at the given index in the current list. Implementation:Java// Java code to illustrate add(int index, E elements) import java.util.*; public class ArrayListDemo { public static void main(String[] args)
2 min read
AbstractList add(int index, E element) method in Java with Examples The add(int index, E element) method of AbstractList is used to insert an element at the specified position in this list. The new element is inserted at the specified index in this list. Syntax: public void add(int index, E element) Parameters: This method takes 2 parameters :- index: index at which
2 min read
How to implement our own Dynamic Array class in Java? Given task is to implement a class in Java which behaves just like the Dynamic array using ArrayList. ArrayList is same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. ArrayList
4 min read