How to Insert an element at a specific position in an Array in Java
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:
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 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)); } } |
chevron_right
filter_none
Output:
Initial 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]
Recommended Posts:
- Remove an Element at specific index from an Array in Java
- How to add an element to an Array in Java?
- Find the index of an array element in Java
- Remove all occurrences of an element from Array in Java
- First strictly greater element in a sorted array in Java
- First strictly smaller element in a sorted array in Java
- Java Program to find largest element in an array
- Java Servlet and JDBC Example | Insert data in MySQL
- Find closest smaller value for every element in array
- First element that appears even number of times in an array
- Set to Array in Java
- Array vs ArrayList in Java
- List to array in Java
- Jagged Array in Java
- Array of ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



