HOWTO · Java

How to Add an Element to a Java Array

Learn why Java arrays have no built-in push() method and how to add values with a stack, list, or array copy.

On this page

Java arrays have a fixed length and no built-in push() method. To add an item, choose a collection based on where it should go: a stack puts it on top, LinkedList.push() inserts it at the front, and ArrayList.add() appends it to a resizable list. If you must keep an array, copy its elements into a new, longer array and use the returned array.

All five Java examples below were executed successfully in the supplied runtime, each with exit code 0.

Use Stack.push() for LIFO behavior

Stack.push() adds an element to the top of a last-in, first-out (LIFO) stack. The most recently pushed item is therefore the first one returned by pop(). The example pushes three strings, then prints and removes the top item.

import java.util.Stack;

public class StackExample {
  public static void main(String[] args) {
    Stack<String> stack = new Stack<>();
    stack.push("Java");
    stack.push("is");
    stack.push("useful");

    System.out.println("Top: " + stack.peek());
    System.out.println("Removed: " + stack.pop());
    System.out.println("Stack: " + stack);
  }
}
Top: useful
Removed: useful
Stack: [Java, is]

Choose this API when later values should be handled before earlier ones, such as when tracking undo actions or pending work. The displayed stack contains the remaining elements after pop() removes the top item. If you only need to keep values in their original sequence, a stack adds behavior you do not need; use an appending list instead.

Use LinkedList.push() to add at the front

LinkedList.push() inserts at the front, rather than appending to the end. Each successive call becomes the new first element, so the list’s order is the reverse of the call order.

import java.util.LinkedList;

public class LinkedListExample {
  public static void main(String[] args) {
    LinkedList<String> values = new LinkedList<>();
    values.push("Java");
    values.push("is");
    values.push("useful");

    System.out.println(values);
  }
}
[useful, is, Java]

This is also a front-insertion operation, so the newest value appears first when the list is printed. Use it when that ordering is intentional, such as maintaining a sequence with the newest item at the front. If the task is to add at the end, choose ArrayList.add() instead; the two calls have different results even though both add an element to a collection.

Use ArrayList.add() to append to a resizable list

Use ArrayList.add() when the goal is to append values to a list that can grow. The call adds each value at the end. An ArrayList is a collection, not a Java array, so it does not have an array’s fixed length.

import java.util.ArrayList;

public class ArrayListExample {
  public static void main(String[] args) {
    ArrayList<String> values = new ArrayList<>();
    values.add("Java");
    values.add("is");
    values.add("useful");

    System.out.println(values);
  }
}
[Java, is, useful]

This is the closest collection choice when “add to an array” means keep the existing order and place another value after the last one. The list can grow as values are added, and add() does not require the caller to allocate and reassign a larger array for each value. Choose a fixed array when its length is part of the data shape or required by another API; choose a list when the number of values can change and end-appending is the desired behavior.

Append to a Java array with a custom method

A custom push() method can return a longer array containing the original elements and the new value. The caller must assign that returned array because Java arrays do not grow in place. Copying takes time proportional to the number of existing elements and requires memory for the new array.

public class ArrayAppendExample {
  private static String[] push(String[] array, String value) {
    String[] longer = new String[array.length + 1];
    System.arraycopy(array, 0, longer, 0, array.length);
    longer[array.length] = value;
    return longer;
  }

  public static void main(String[] args) {
    String[] values = {"a", "b", "c"};
    values = push(values, "d");
    System.out.println(java.util.Arrays.toString(values));
  }
}
[a, b, c, d]

The returned reference is important: the original array still has its original length, while push() creates and returns a separate array. Assigning the result makes subsequent reads use the longer array; ignoring the return value leaves the caller using the original one. This approach is useful when an array is required by a method signature or library. Repeating the operation copies the elements already present each time, so for a sequence of additions a resizable collection is usually simpler and avoids writing this copying logic yourself.

Use ArrayDeque.push() for a stack

For new stack code, ArrayDeque is a resizable deque that supports insertion and removal at either end. Its push() method adds at the front, and pop() removes from that same end, giving LIFO behavior. Iterating over the deque starts at the front.

import java.util.ArrayDeque;

public class ArrayDequeExample {
  public static void main(String[] args) {
    ArrayDeque<String> values = new ArrayDeque<>();
    values.push("Java");
    values.push("is");
    values.push("useful");

    System.out.println("Top: " + values.peek());
    System.out.println("Removed: " + values.pop());
    System.out.println("Deque: " + values);
  }
}
Top: useful
Removed: useful
Deque: [is, Java]

For stack behavior, ArrayDeque.push() offers the same top-first model shown with Stack.push(), while the deque also supports operations at its other end. Use this when you need a stack and want a deque collection; use ArrayList.add() when the required result is an ordered list with new items at the end. The choice is about where values go and how they will be retrieved, rather than about Java arrays having a built-in push operation.