Add new elements to an array in Java
This post will discuss how to add new elements to an array in Java.
We know that the size of an array can’t be modified once it is created. There is no way to resize the fixed-size arrays in Java to accommodate additional element(s).
If we need a dynamic array-based data structure, the recommended solution is to use an ArrayList. An ArrayList is a resizable-array implementation of the List interface. This class provides methods to manipulate the size of the array easily. As elements are added to the ArrayList, its capacity grows automatically. The capacity is the size of the array used to store the elements in the list.
But if you insist on using arrays, you have to instantiate a new array to accommodate the additional element. We can do this with any of the following methods:
1. Using List
The idea is to convert our array into a list, then append the specified element at the end of this list, and then use the method List.toArray() method to returns an array containing all the elements in our list. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { private static Integer[] append(Integer[] arr, int element) { List<Integer> list = new ArrayList<>(Arrays.asList(arr)); list.add(element); return list.toArray(new Integer[0]); } public static void main(String[] args) { Integer[] nums = { 1, 2, 3, 4 }; nums = append(nums, 5); // add 5 to `nums[]` System.out.println(Arrays.toString(nums)); } } |
Output:
[1, 2, 3, 4, 5]
2. Using System.arraycopy() method
The idea is to allocate a new array of size one greater than the original array. Then call the System.arraycopy() method, which copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; class Main { private static Integer[] append(Integer[] arr, int element) { Integer[] array = new Integer[arr.length + 1]; System.arraycopy(arr, 0, array, 0, arr.length); array[arr.length] = element; return array; } public static void main(String[] args) { Integer[] nums = { 1, 2, 3, 4 }; nums = append(nums, 5); // add 5 to `nums[]` System.out.println(Arrays.toString(nums)); } } |
Output:
[1, 2, 3, 4, 5]
3. Using Arrays.copyOf() method
We can also use Arrays.copyOf() to allocate the larger array for accommodating the new element. It internally uses System.arraycopy() but provides a much simpler signature.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; class Main { private static <T> T[] append(T[] arr, T element) { T[] array = Arrays.copyOf(arr, arr.length + 1); array[arr.length] = element; return array; } public static void main(String[] args) { Integer[] nums = { 1, 2, 3, 4 }; nums = append(nums, 5); // add 5 to `nums[]` System.out.println(Arrays.toString(nums)); } } |
Output:
[1, 2, 3, 4, 5]
4. Using Apache Commons Lang
Another good alternative is to leverage Apache Commons ArrayUtils class add() method designed specifically for this purpose. Note that this also internally calls System.arraycopy().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; class Main { private static <T> T[] append(T[] arr, T element) { return ArrayUtils.add(arr, element); } public static void main(String[] args) { Integer[] nums = { 1, 2, 3, 4 }; nums = append(nums, 5); // add 5 to `nums[]` System.out.println(Arrays.toString(nums)); } } |
Output:
[1, 2, 3, 4, 5]
That’s all about adding new elements to 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 :)