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.
Discussions

[JAVA] How Do I Use push() and pop() With an Array?
You use an array to implement a stack but the array does not have push or pop methods as part of the array class More on reddit.com
๐ŸŒ r/learnprogramming
14
0
September 6, 2017
java howto ArrayList push, pop, shift, and unshift - Stack Overflow
I heard that linked lists can be quite slower than array lists even for inserts/deletions. stackoverflow.com/questions/34170566/โ€ฆ So I wonder, what about Java? 2018-07-25T10:57:23.587Z+00:00 ... This is not a solution. a linked list is simply a different data structure with different characteristics. This questions pertains to a contiguous list 2022-05-21T12:16:25.07Z+00:00 ... Save this answer. ... Show activity on this post. maybe you want to take a look java.util.Stack class. it has push... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Push an item onto the end of array in Java - Code Review Stack Exchange
Small tip: Since you are pushing a new value to the end of the array, you may want to consider reordering the method parameters so that the incoming value is indeed to the right of the array. ... Just to be an annoyance and since this is code review not give me an answer... the more important question is what is the actual use case of this method? Do you have control over the code that is calling it or is this a library method? I ask because Java ... More on codereview.stackexchange.com
๐ŸŒ codereview.stackexchange.com
[Java] I'm having trouble understanding what .push does.
This code looks for the character "b" in the text, and stores the next 3 (myName.length) letters (including the "b") in the hits array. I'm not sure if this is what you are looking for. If you look at the results, this is what you get: ["b", "o", "b", "b", ".", " ", "b", "o", "b", "b", " ", "y", "b", "o", "b", "b", " ", "t", "b", "e", " ", "b", "o", "b", "b", " ", "y", "b", "o", "b", "b", ".", undefined] To answer your question: push pushes an element to the end of an array. Try this code, it might clear things up: > fruits = [] [] > fruits.push("pear") // pushes to the end of the array 1 > fruits.push("orange") 2 > fruits.push("banana") 3 > fruits // let's see what's stored in fruits ["pear", "orange", "banana"] > fruits.pop() // removes from the end of the array "banana" > fruits ["pear", "orange"] A few remaks: This is JavaScript, not Java; don't confuse the two, they aren't remotely the same. Please try to format your code in the future. Even if you are writing a few lines like this, it's a really good practice to format it. When you look at the last 3 lines the empty braces ({}) could be removed. Messy code could confuse things in the future when you write bigger stuff. More on reddit.com
๐ŸŒ r/learnprogramming
14
12
March 25, 2013
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays.asp
Java Arrays
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] i'm having trouble understanding what .push does.
r/learnprogramming on Reddit: [Java] I'm having trouble understanding what .push does.
March 25, 2013 -

I have been learning Java for a few days now through Codecademy and have been writing down most of things I've learnt. Now I have just came across .push and I am trying to figure out what it does. Codecademy provides an answer but it still makes me lost.

https://gist.github.com/anonymous/5237420

The link on top is the link to the code. When I run it, it gives me the number 33 and Codecademy tells me it's correct! I'm just completely clueless on the code on the 12th line, can someone help me out?

๐ŸŒ
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.