Add element to specific position in a Java array
This post will discuss how to add an element to a specific position in an array in Java.
1. Using System.arraycopy() method
We can use the System.arraycopy() method to copy elements from one array to another. This method can efficiently copy an array from the specified source array, beginning at the specified position, to the specified position in the destination array. We can use this method to create a new array with one more element and copy the elements from the original array to the new array, leaving a space for the element to be inserted at the specified index. This method is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; public class Main { public static int[] insert(int[] arr, int index, int value) { int[] result = new int[arr.length + 1]; System.arraycopy(arr, 0, result, 0, index); result[index] = value; System.arraycopy(arr, index, result, index + 1, arr.length - index); return result; } public static void main(String[] args) { int[] arr = {4, 3, 6, 5}; int index = 2; int value = 1; arr = insert(arr, index, value); System.out.println(Arrays.toString(arr)); // [4, 3, 1, 6, 5] } } |
2. Using a loop
Instead of calling the System.arraycopy() method, we can write our own custom routine to shift the elements in the array to the right by one position from the specified index. The idea is to declare a new array with one more element to have an extra space in the array to accommodate the new element. Then we can use a for loop to iterate over the array from right to left and assign each element to the next position until we reach the specified index. Then we can assign the element to be inserted at that index. Here’s a working code that is logically the same as the System.arraycopy() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Arrays; public class Main { public static int[] insert(int[] arr, int index, int value) { int[] result = new int[arr.length + 1]; for (int i = 0; i < index; i++) { result[i] = arr[i]; } result[index] = value; for (int i = index + 1; i < result.length; i++) { result[i] = arr[i - 1]; } return result; } public static void main(String[] args) { int[] arr = {4, 3, 6, 5}; int index = 2; int value = 1; arr = insert(arr, index, value); System.out.println(Arrays.toString(arr)); // [4, 3, 1, 6, 5] } } |
3. Using Apache Commons Lang Library
Another option to add an element to a specific position in an array in Java is to use the Apache Commons Lang library, which provides the ArrayUtils class with various utility methods for working with arrays. The ArrayUtils class has a method called insert() that can insert an element or multiple elements into an array at the given index. This method is overloaded for all primitive types and object arrays. The insert() method returns a new array with the inserted element(s), and does not modify the original array. Here is an example of how to use this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = {4, 3, 6, 5}; int index = 2; int value = 1; arr = ArrayUtils.insert(index, arr, value); System.out.println(Arrays.toString(arr)); // [4, 3, 1, 6, 5] } } |
4. Using a List
We can also use a List instead of an array to add elements into an array at the given index. A List is a collection that allows dynamic resizing and insertion of elements at any position. We can use the add() method to insert an element at the specified index in a List. We can also convert a primitive array to a List and vice versa using the Stream API. Here’s an example of this approach:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static int[] insert(int[] arr, int index, int value) { List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList()); list.add(index, value); return list.stream().mapToInt(Integer::intValue).toArray(); } public static void main(String[] args) { int[] arr = {4, 3, 6, 5}; int index = 2; int value = 1; arr = insert(arr, index, value); System.out.println(Arrays.toString(arr)); // [4, 3, 1, 6, 5] } } |
That’s all about adding an element to a specific position in an array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)