The three ways to add to an array in Java
Java arrays have a fixed size set when you create them, so you cannot add elements directly to an existing array the way you might in other languages. Instead, you have three practical approaches: create a new larger array and copy elements over, use the ArrayList class which grows automatically, or use the Arrays.copyOf() method for a cleaner syntax.
The method you choose depends on what you are building. If you need a collection that changes size often, ArrayList is almost always the right choice. If you are working with a fixed-size array and need to add one element, Arrays.copyOf() is the cleanest. If you are managing arrays manually for performance reasons, you can grow them yourself, but this is rare in modern Java code.
Key Takeaways
- Java arrays cannot grow after creation, so adding an element requires creating a new array and copying the old values into it.
- ArrayList is the standard tool for collections that need to change size, and it handles the copying work for you automatically.
- Arrays.copyOf() lets you expand an array in one line and is useful when you must work with arrays rather than ArrayList.
- If you add elements one at a time in a loop, ArrayList is far more efficient than repeatedly creating new arrays.
Using ArrayList when you need a growing collection
ArrayList is a class in the java.util package that wraps an array and handles resizing automatically. When you add an element and the internal array is full, ArrayList creates a larger array behind the scenes and copies everything over. You do not see this happening.
To use ArrayList, declare it with the type of element it will hold in angle brackets. Here is the pattern:
ArrayList<String> fruits = new ArrayList<>(); fruits.add("apple"); fruits.add("banana"); fruits.add("orange");
The add() method puts an element at the end. If you need to insert at a specific position, use add(index, element) — for example, fruits.add(1, "grape") inserts "grape" at position 1 and shifts everything after it forward. To remove an element, call remove(index) or remove(object).
When you need to loop through the ArrayList, use a for-each loop just as you would with an array: for (String fruit : fruits) { System.out.println(fruit); }. If you need the actual array later, call fruits.toArray(new String[0]) to convert it back.
Using Arrays.copyOf() to expand a single array
If you are working with a regular array and need to add one element, Arrays.copyOf() creates a new array of the size you specify and copies the old values into it. This is cleaner than writing the copy loop yourself.
The syntax is: array = Arrays.copyOf(array, array.length + 1); This creates a new array one element longer, copies all the old values, and leaves the last position empty. Then assign the new value: array[array.length - 1] = newElement;
Arrays.copyOf() is useful when you have a small array and need to add a few elements, or when you are converting code that used arrays and want to keep the same structure. For anything larger, switch to ArrayList. If you add elements in a loop using Arrays.copyOf() each time, your code will be slow because each addition creates a new array and copies everything — ArrayList avoids this by growing in larger chunks.
Manually copying arrays the long way
If you cannot use ArrayList and Arrays.copyOf() is not available in your Java version (very old versions), you can write the copy yourself. Create a new array one element larger, loop through the old array and copy each value, then add the new element.
Here is the pattern:
int[] oldArray = {1, 2, 3}; int[] newArray = new int[oldArray.length + 1]; for (int i = 0; i < oldArray.length; i++) { newArray[i] = oldArray[i]; } newArray[newArray.length - 1] = 4; oldArray = newArray;
This approach is verbose and error-prone, which is why Arrays.copyOf() and ArrayList exist. Use this only if you are working in an environment where those tools are not available, which is extremely rare in modern Java.
When to use ArrayList versus arrays
Use ArrayList if your collection will change size during the program, or if you do not know the final size when you create it. ArrayList handles all the resizing work and is the standard choice in professional Java code. It is also safer because you cannot accidentally access an index that does not exist — ArrayList will throw a clear error instead of reading garbage memory.
Use a regular array if the size is truly fixed and will never change, or if you are working with a very large collection and need to squeeze out every bit of performance. Arrays use slightly less memory than ArrayList because they do not store extra capacity. In practice, this matters only in specialized situations like processing millions of records.
If you inherit code that uses arrays and you need to add elements, convert it to ArrayList unless there is a specific reason not to. The conversion is straightforward: ArrayList<Integer> list = new ArrayList<>(Arrays.asList(oldArray)); This creates an ArrayList from an existing array in one line.
Adding elements at specific positions
ArrayList makes it easy to insert at a specific index. The add(index, element) method shifts everything at that index and after it one position forward. For example, if you have ["apple", "banana", "orange"] and call add(1, "grape"), you get ["apple", "grape", "banana", "orange"].
With a regular array, inserting in the middle is more work. You must create a new array, copy elements before the insertion point, add the new element, then copy elements after it. This is another reason ArrayList is the standard tool — it handles this complexity for you.
If you need to insert at the beginning often, ArrayList is still the right choice, but be aware that it has to shift every element. If performance matters and you are inserting at the beginning constantly, you might use a different structure like a LinkedList, but that is a specialized case.
Frequently Asked Questions
Can I add null to an ArrayList?
Yes, ArrayList allows null values. If you call add(null), it will store null at that position. When you loop through and encounter null, you need to check for it before using the value, or you will get a NullPointerException when you try to call methods on it.
What happens if I add to an ArrayList while looping through it?
You will get a ConcurrentModificationException. If you need to add or remove during iteration, use an Iterator instead of a for-each loop, or collect the changes and apply them after the loop finishes.
How do I add all elements from one ArrayList to another?
Use the addAll() method: list1.addAll(list2); This appends every element from list2 to the end of list1. You can also use addAll(index, collection) to insert them at a specific position.
Is ArrayList slower than a regular array?
ArrayList is slightly slower for reading because it has to go through a wrapper, but the difference is negligible in most programs. ArrayList is faster overall if you are adding elements, because it avoids the cost of copying the entire array each time. Use ArrayList unless you have measured a real performance problem.