In Java an array has a fixed size (after initialisation), meaning that you can't add or remove items from an array.

int[] i = new int[10];

The above snippet mean that the array of integers has a length of 10. It's not possible add an eleventh integer, without re-assign the reference to a new array, like the following:

int[] i = new int[11];

In Java the package java.util contains all kinds of data structures that can handle adding and removing items from array-like collections. The classic data structure Stack has methods for push and pop.

Answer from Kennet on Stack Overflow
Top answer
1 of 5
56

In Java an array has a fixed size (after initialisation), meaning that you can't add or remove items from an array.

int[] i = new int[10];

The above snippet mean that the array of integers has a length of 10. It's not possible add an eleventh integer, without re-assign the reference to a new array, like the following:

int[] i = new int[11];

In Java the package java.util contains all kinds of data structures that can handle adding and removing items from array-like collections. The classic data structure Stack has methods for push and pop.

2 of 5
25

For those who don't have time to refactor the code to replace arrays with Collections (for example ArrayList), there is an alternative. Unlike Collections, the length of an array cannot be changed, but the array can be replaced, like this:

array = push(array, item);

The drawbacks are that

  • the whole array has to be copied each time you push, and
  • the original array Object is not changed, so you have to update the variable(s) as appropriate.

Here is the push method for String:
(You can create multiple push methods, one for String, one for int, etc)

private static String[] push(String[] array, String push) {
    String[] longer = new String[array.length + 1];
    for (int i = 0; i < array.length; i++)
        longer[i] = array[i];
    longer[array.length] = push;
    return longer;
}

This alternative is more efficient, shorter & harder to read:

private static String[] push(String[] array, String push) {
    String[] longer = new String[array.length + 1];
    System.arraycopy(array, 0, longer, 0, array.length);
    longer[array.length] = push;
    return longer;
}
๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Array Push in Java | AlgoCademy
.add() takes one parameter and "pushes" it onto the end of the ArrayList: List<Integer> arr1 = new ArrayList<>(List.of(1, 2, 3)); arr1.add(4); // arr1 is now {1, 2, 3, 4} Assignment Follow the Coding Tutorial and let's play with some arrays. Hint Look at the examples above if you get stuck. In this lesson, we will explore how to append an item to an ArrayList in Java.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ java tutorial โ€บ java array.push
Java array.push | Guide to Java array.push with Programming Examples
June 16, 2023 - The push method works similarly in Stack, LinkedList, and Deque. To achieve the desired result, you can follow these steps: 1. Create a stack, LinkedList, or Deque based on the requirement. Stack<String>s = new Stack<String>(); LinkedList<Integer>li = new LinkedList<>(); Deque<Integer>dq = new ArrayDeque<Integer>(8); ... import java.util.*; public class PushMethodExample { public static void main(String args[]) { // create a stack Stack<String> s = new Stack<String>(); s.push("Happy"); s.push("Sad"); s.push("Confused"); s.push("Tensed"); s.push("Mixed Emotions"); // Print elements in stack System.out.println("Stack Elements: " + s); // Push new elements s.push("Emotions"); s.push("Exists"); // Stack after adding new elements System.out.println("Stack after adding new elements " + s); } }
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ push function for array java
The Push() Function in Java | Delft Stack
December 22, 2023 - In this article, weโ€™ll explore the concept of a user-defined push() function, a versatile tool that allows developers to seamlessly add elements to arrays in a personalized manner. Letโ€™s dive into the code to grasp the essence of a user-defined push() function: import java.util.*; public class Push_Arr { private static String[] push(String[] array, String push) { String[] longer = new String[array.length + 1]; for (int i = 0; i < array.length; i++) longer[i] = array[i]; longer[array.length] = push; return longer; } public static void main(String args[]) { String[] arr = new String[] {"a", "b", "c"}; arr = Push_Arr.push(arr, "d"); for (int i = 0; i < arr.length; i++) System.out.println(arr[i]); } }
Find elsewhere
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ push-array-java
Push Array in Java: A Comprehensive Guide โ€” javaspring.net
While Java arrays themselves don't have a built - in `push` method like some other programming languages (e.g., JavaScript arrays), the concept of pushing an element onto an array can be translated into adding an element to the end of an array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-add-an-element-to-an-array-in-java
How to Add an Element to an Array in Java? - GeeksforGeeks
October 16, 2025 - Print the new array. ... // Java Program to add an element // into a new array import java.io.*; import java.lang.*; import java.util.*; class Geeks { // Function to add x in arr public static int[] addX(int n, int arr[], int x) { int newarr[] = new int[n + 1]; // insert the elements from // the old array into the new array // insert all elements till n // then insert x at n+1 for (int i = 0; i < n; i++) newarr[i] = arr[i]; newarr[n] = x; return newarr; } public static void main(String[] args) { int n = 5; int arr[] = { 10, 20, 30, 40, 50}; int x = 70; // call the method to add x in arr arr = addX(n, arr, x); System.out.println(Arrays.toString(arr)); } }
๐ŸŒ
Lucee Documentation
docs.lucee.org โ€บ reference โ€บ functions โ€บ arraypush()
array.push() :: Lucee Documentation
Inserts an array element at the end of an array and return the new size of the array. ... numbers = [ 1, 2, 3, 4 ]; Dump( numbers.Push( 0 ) ); // Outputs 5 moreNumbers = [ 5, 6, 7, 8 ]; Dump( moreNumbers.Push( 4 ) ); // Outputs 5
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ java โ€บ how-to-add-new-elements-to-an-array
How to add new elements to an array? - Java
September 2, 2025 - Use push() to add elements at the end. ... Use unshift() to add elements at the beginning. Use splice() to insert elements at any position. ... Arrays in Java have fixed sizes, so you canโ€™t directly add elements.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ adding an element to a java array vs an arraylist
Adding an Element to a Java Array vs an ArrayList | Baeldung
April 4, 2025 - So, to append an element, first, we need to declare a new array that is larger than the old array and copy the elements from the old array to the newly created array. After that, we can append the new element to this newly created array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arraydeque-push-method-in-java
ArrayDeque push() Method in Java - GeeksforGeeks
December 10, 2018 - The Java.util.ArrayDeque.push(E element) method is used to push an element into the Deque. The operation is similar to the operation in the stack. The element gets pushed onto the top of the deque.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-insert-an-element-at-a-specific-position-in-an-array-in-java
How to Insert an element at a specific position in an Array in Java - GeeksforGeeks
July 12, 2025 - Insert the rest of the elements from the previous array into the new array after the pos. Below is the implementation of the above approach: ... // Java Program to Insert an element // at a specific position in an Array import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to insert x in arr at position pos public static int[] insertX(int n, int arr[], int x, int pos) { int i; // create a new array of size n+1 int newarr[] = new int[n + 1]; // insert the elements from // the old array into the new array // insert all elements till pos // then insert x at pos // then
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-append-to-an-array-in-java
How to append to an array in Java
Use ArrayList.add() If we need a dynamic data structure that can grow and shrink automatically, ArrayList is the best option. Kickstart your programming journey with "Learn Java." Master essential concepts like input/output methods, user-defined methods, and basic data types.
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ how to add elements to an array in java
How To Add Elements To An Array In Java
April 1, 2025 - This Tutorial will Explain the Various Methods to Print Elements of an Array in Java. Methods Explained are - Arrays.toString, For Loop, For Each Loop, & DeepToString: In our previous tutorial, we discussed the creation of Array Initialization. To begin with, we declare instantiate and initialize the array.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-add-an-element-to-an-array-in-java
Inserting Elements in an Array
July 20, 2023 - You can add an element at a particular position in an array using the add() method of this class. import java.util.Scanner; import org.apache.commons.lang3.ArrayUtils; public class InsertingElements { public static void main(String args[]) { ...
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java arrays โ€บ how to add a new element to an array in java
How To Add a new Element To An Array In Java
September 28, 2023 - If you want to learn easy ways to add an element to Java arrays, this is a full guide on how to add to array a new element
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 609985 โ€บ java โ€บ add-elements-left-side-array
How to add elements to the left side of an array? (Beginning Java forum at Coderanch)
Dan Rana wrote:the array is [1 2 3] and i'm trying to get it to become [4 1 2 3]. a method named add will add 4. Just to let you know, an operation like that is not generally called an "add"; it's called a "push" (or in some classes 'addFirst()'), so if you do decide to implement it that way, make sure you document it WELL.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ java-add-to-array
Adding Elements to an Array in Java: A How-To Guide
February 27, 2024 - IOFloodโ€™s Java Arrays Guide explains how to iterate through arrays using loops and iterators in Java. Essential Array Methods in Java โ€“ Master utilizing array methods to streamline array processing tasks in Java.