Using ArrayList as Intermediate Storage
Instead of manually handling arrays, we can use ArrayList, which provides dynamic resizing and easy element addition.
import java.util.ArrayList;
import java.util.Arrays;
public class AddUsingArrayList {
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 4};
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(arr));
// Adding a new element
list.add(5);
arr = list.toArray(new Integer[0]);
System.out.println("New Array: " + Arrays.toString(arr));
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
New Array: [1, 2, 3, 4, 5]
Using ArrayList is an efficient way to handle arrays dynamically in Java.
Convert an Array to a List
Another way to add an element to an array is to convert it into a list, add elements, and then convert it back.
import java.util.Arrays;
import java.util.List;
public class ConvertArrayToList {
public static void main(String[] args) {
String[] arr = {"A", "B", "C"};
List<String> list = Arrays.asList(arr);
// Convert list to ArrayList to allow modification
list = new ArrayList<>(list);
list.add("D");
arr = list.toArray(new String[0]);
System.out.println("New Array: " + Arrays.toString(arr));
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
New Array: [A, B, C, D]
Create a New Array with Larger Capacity and Add a New Element
Since arrays in Java are fixed in size, we can create a new array with a larger capacity and copy the old elements into it.
import java.util.Arrays;
public class ExpandArray {
public static void main(String[] args) {
int[] arr = {10, 20, 30};
int newElement = 40;
int[] newArr = Arrays.copyOf(arr, arr.length + 1);
newArr[arr.length] = newElement;
System.out.println("New Array: " + Arrays.toString(newArr));
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
New Array: [10, 20, 30, 40]
This approach is useful when handling arrays without additional libraries.
Copying Arrays Using Apache Commons
When working with array in Java, there are times when you need to copy one array into another. While Java provides built-in methods like `System.arraycopy()` or the `Arrays.copyOf()` method, using Apache Commons Lang library can make this process even simpler & more efficient. Apache Commons is a popular library that provides reusable Java components, including utilities for working with arrays.
To use Apache Commons, you first need to add the library to your project. If you're using Maven, add the following dependency to your `pom.xml` file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
If you're not using Maven, you can download the JAR file from the [Apache Commons website](https://commons.apache.org/proper/commons-lang/) & add it to your project manually.
Once the library is added, you can use the `ArrayUtils` class to copy arrays. For example:
import org.apache.commons.lang3.ArrayUtils;
public class Main {
public static void main(String[] args) {
// Original array
int[] originalArray = {1, 2, 3, 4, 5};
// Copying the array using ArrayUtils
int[] copiedArray = ArrayUtils.clone(originalArray);
// Printing the copied array
System.out.println("Copied Array: ");
for (int num : copiedArray) {
System.out.print(num + " ");
}
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, the `ArrayUtils.clone()` method creates a deep copy of the original array. This means that any changes made to the copied array won't affect the original array. The `clone()` method is easy to use, making it a great choice for copying arrays.
Applying the `Arrays.copyOf()` Method
Java provides a built-in method called `Arrays.copyOf()` that allows you to copy an array easily. This method is part of the `java.util.Arrays` class, so you don’t need to install any external libraries. It’s a simple & efficient way to create a copy of an array, either with the same length or a different length.
Let’s see how the `Arrays.copyOf()` method works:
1. It takes two parameters:
- The original array you want to copy.
- The length of the new array.
2. If the new length is greater than the original array’s length, the extra elements are filled with default values (e.g., `0` for integers, `null` for objects).
3. If the new length is smaller, the method truncates the array to the specified length.
Let’s look at a complete example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Original array
int[] originalArray = {10, 20, 30, 40, 50};
// Copying the array with the same length
int[] copiedArraySameLength = Arrays.copyOf(originalArray, originalArray.length);
// Copying the array with a larger length
int[] copiedArrayLargerLength = Arrays.copyOf(originalArray, 7);
// Copying the array with a smaller length
int[] copiedArraySmallerLength = Arrays.copyOf(originalArray, 3);
// Printing the copied arrays
System.out.println("Copied Array (Same Length): " + Arrays.toString(copiedArraySameLength));
System.out.println("Copied Array (Larger Length): " + Arrays.toString(copiedArrayLargerLength));
System.out.println("Copied Array (Smaller Length): " + Arrays.toString(copiedArraySmallerLength));
}
}

You can also try this code with Online Java Compiler
Run Code
In this Code:
1. Original Array: We start with an array `{10, 20, 30, 40, 50}`.
2. Same Length Copy: The `Arrays.copyOf()` method creates a copy of the original array with the same length. The output will be `[10, 20, 30, 40, 50]`.
3. Larger Length Copy: When we specify a larger length (e.g., 7), the extra elements are filled with default values. The output will be `[10, 20, 30, 40, 50, 0, 0]`.
4. Smaller Length Copy: When we specify a smaller length (e.g., 3), the array is truncated. The output will be `[10, 20, 30]`.
This method is useful when you need to resize an array or create a copy for manipulation without affecting the original array.
Implementing System.arraycopy()
The System.arraycopy() method is a faster way to copy elements from one array to another.
import java.util.Arrays;
public class UsingSystemArrayCopy {
public static void main(String[] args) {
int[] arr = {5, 10, 15};
int newElement = 20;
int[] newArr = new int[arr.length + 1];
System.arraycopy(arr, 0, newArr, 0, arr.length);
newArr[arr.length] = newElement;
System.out.println("New Array: " + Arrays.toString(newArr));
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
New Array: [5, 10, 15, 20]
This method is efficient for copying large arrays.
Frequently Asked Questions
Why can't we directly add elements to an array in Java?
Java arrays have a fixed size, meaning we cannot increase their length dynamically. To add elements, we need to create a new array or use dynamic collections like ArrayList.
What is the most efficient way to add an element to an array in Java?
Using ArrayList is the most efficient way as it handles resizing automatically. If working with plain arrays, System.arraycopy() is the fastest approach.
What happens if we try to add an element beyond the array's capacity?
Java will throw an ArrayIndexOutOfBoundsException, as arrays do not support dynamic resizing.
Conclusion
In this article, we discussed different ways to add elements to an array in Java. Since arrays have a fixed size, we used approaches like creating a new larger array, using ArrayList, and utilizing System.arraycopy() for efficient copying. Each method has its advantages depending on the use case. Understanding these techniques helps in effectively managing dynamic data structures in Java.