The simplest way to add to an array in Java

Java arrays have a fixed size set when you create them, so you cannot add items directly to an existing array the way you might in other languages. Instead, you create a new array that is one size larger, copy the old items into it, and add the new item. Most of the time, though, you will use an ArrayList instead — a built-in Java class that handles this resizing automatically and lets you add items with a single line of code.

If you are working with an ArrayList, you call the add() method. If you must work with a fixed-size array, you manually create a larger array, copy the contents, and insert the new value. This guide covers both approaches and shows you when to use each one.

Key Takeaways

  • ArrayList is the practical choice for most situations because it grows automatically when you add items, while arrays stay the same size forever.
  • To add to an ArrayList, write myList.add(value) or myList.add(index, value) to insert at a specific position.
  • If you must use a fixed array, you create a new array one size larger, copy the old items over, and manually place the new item.
  • ArrayList stores objects, not primitive types like int or double directly — you use Integer, Double, and similar wrapper classes instead.
  • The add() method returns true if the item was added successfully, which you can check if your code needs to verify the operation.

Adding items to an ArrayList

An ArrayList is a resizable list that lives in the java.util package. You declare it by writing the type of items it will hold in angle brackets. For example, ArrayList<String> names = new ArrayList<>() creates an empty list that holds strings.

To add an item to the end of the list, call the add() method with the value:

names.add("Alice"); names.add("Bob");

If you want to insert an item at a specific position instead of at the end, pass the index (position) as the first argument:

names.add(1, "Charlie");

This inserts "Charlie" at index 1, shifting "Bob" to index 2. Remember that indexing starts at 0, so the first item is at index 0.

Adding to a fixed-size array

If your code requires a traditional array instead of an ArrayList, you must manually handle the resizing. Create a new array one size larger than the original, copy all the old items into it, and then place the new item in the empty slot.

Here is the pattern:

int[] numbers = {1, 2, 3}; int[] newNumbers = new int[numbers.length + 1]; System.arraycopy(numbers, 0, newNumbers, 0, numbers.length); newNumbers[newNumbers.length - 1] = 4; numbers = newNumbers;

System.arraycopy() is a built-in method that copies items from one array to another. The arguments are: the source array, the starting index in the source, the destination array, the starting index in the destination, and how many items to copy. After the copy, you place the new value in the last position and reassign the reference so numbers now points to the larger array.

This approach works but is verbose and inefficient if you need to add many items. That is why ArrayList exists — it does this resizing work behind the scenes.

Inserting at a specific position in an array

If you need to insert a value in the middle of a fixed array, you must shift all items after that position one slot to the right. Create a new array, copy items up to the insertion point, place the new item, then copy the remaining items:

int[] numbers = {1, 2, 4, 5}; int[] newNumbers = new int[numbers.length + 1]; int insertIndex = 2; System.arraycopy(numbers, 0, newNumbers, 0, insertIndex); newNumbers[insertIndex] = 3; System.arraycopy(numbers, insertIndex, newNumbers, insertIndex + 1, numbers.length - insertIndex); numbers = newNumbers;

The first arraycopy() moves items 0 and 1 to the new array. You then place the new value at index 2. The second arraycopy() moves items starting from the old index 2 (which are 4 and 5) to the new array starting at index 3, making room for the inserted value. With ArrayList, you would straightforward write numbers.add(2, 3).

Working with ArrayList and primitive types

ArrayList cannot directly hold primitive types like int, double, or boolean. Instead, you use wrapper classes — Integer, Double, Boolean — which are object versions of these types.

When you declare an ArrayList for integers, you write ArrayList<Integer> numbers = new ArrayList<>(). Java automatically converts between int and Integer in most situations, so you can write numbers.add(5) and Java handles the conversion. This automatic conversion is called autoboxing.

When you retrieve a value, Java converts it back automatically: int value = numbers.get(0) works even though the ArrayList holds Integer objects. However, if the ArrayList is empty or the index does not exist, you will get an error, so check the size first if you are unsure.

Checking if an item was added successfully

The add() method returns a boolean value — true if the item was added, false if it was not. Most of the time with ArrayList, the add always succeeds, so you do not need to check. However, if you are working with a custom list class or a special collection, checking the return value can be useful:

if (names.add("Diana")) {   System.out.println("Item added successfully"); } else {   System.out.println("Item was not added"); }

For standard ArrayList operations, this check is optional. Most developers skip it because ArrayList add operations are reliable. You would only need this pattern if you were working with a collection that has special rules about what can be added.

Frequently Asked Questions

Can I add null to an ArrayList?

Yes, ArrayList allows null values. You can write myList.add(null) and it will be stored. However, when you retrieve it later with get(), you need to check whether the value is null before using it, or you will get a NullPointerException if you try to call methods on it.

What happens if I add to an ArrayList while looping through it?

Adding items to an ArrayList while iterating over it with a for-each loop will cause a ConcurrentModificationException. Use an Iterator or a traditional for loop with an index if you need to add items during iteration. A safer approach is to collect the items to add in a separate list, then add them all after the loop finishes.

How do I add all items from one ArrayList to another?

Use the addAll() method: list1.addAll(list2) adds every item from list2 to the end of list1. You can also specify a position: list1.addAll(2, list2) inserts all items from list2 starting at index 2 in list1.

Is there a performance difference between adding to an ArrayList and adding to an array?

ArrayList is slightly slower because it may need to resize and copy items behind the scenes. However, ArrayList uses an efficient algorithm that does not resize on every add — it grows in larger chunks. For most programs, the difference is negligible, and the convenience of ArrayList outweighs the tiny performance cost.