
- Java Data Structures Resources
- Java Data Structures - Quick Guide
- Java Data Structures - Resources
- Java Data Structures - Discussion
Inserting Elements in an Array
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.
Algorithm
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA −
Step 1 - Start Step 2 - Set J = N Step 3 - Set N = N+1 Step 4 - Repeat steps 5 and 6 while J >= K Step 5 - Set LA[J+1] = LA[J] Step 6 - Set J = J-1 Step 7 - Set LA[K] = ITEM Step 8 - Stop
Example
Since the array size in Java is fixed after insertion operation excess elements of the array will not be displayed. Therefore, if you insert the element in the middle of the array in order to display the last element you need to create a new array with size n+1 (where n is the size of the current array) and insert elements to it, and display it or, print the last element in a separate statement after printing the contents of the array.
public class InsertingElements { public static void main(String args[]) { int[] myArray = {10, 20, 30, 45, 96, 66}; int pos = 3; int data = 105; int j = myArray.length; int lastElement = myArray[j-1]; for(int i = (j-2); i >= (pos-1); i--) { myArray[i+1] = myArray[i]; } myArray[pos-1] = data; System.out.println("Contents of the array after insertion ::"); for(int i = 0; i < myArray.length; i++) { System.out.print(myArray[i]+ ", "); } System.out.print(lastElement); } }
Output
Contents of the array after insertion :: 10, 20, 105, 30, 45, 96, 66
The apache commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add library to your project.
<dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency> </dependencies>
This package provides a class named ArrayUtils. You can add an element at a particular position in an array using the add() method of this class.
Example
import java.util.Scanner; import org.apache.commons.lang3.ArrayUtils; public class InsertingElements { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of elements needed :"); int n = sc.nextInt(); int[] myArray = new int[n]; System.out.println("Enter the elements ::"); for(int i = 0; i < n; i++) { myArray[i] = sc.nextInt(); } System.out.println("Enter the position to insert the element :"); int pos = sc.nextInt(); System.out.println("Enter the element:"); int element = sc.nextInt(); int [] result = ArrayUtils.add(myArray, pos, element); System.out.println("Contents of the array after insertion ::"); for(int i = 0; i < result.length; i++) { System.out.print(result[i]+ " "); } } }
Output
Enter the number of elements needed : 5 Enter the elements :: 55 45 25 66 45 Enter the position to insert the element : 3 Enter the element: 404 Contents of the array after insertion :: 55 45 25 404 66 45