If you are actually using the array as a stack (and thus, will only add or remove items at the top of the stack) then you could keep in another variable the next free index in the array.

int[] array = new int[21];
int nextIndex = 0;

public void push(int e) {
    array[nextIndex] = e;
    ++nextIndex;
}

public int pop() {
    --nextIndex;
    return array[nextIndex];
}

If removals can occur anywhere, then I don't see a better solution than iterating over the array to find a free spot.

Answer from ARRG on Stack Overflow
๐ŸŒ
onlyxcodes
onlyxcodes.com โ€บ 2023 โ€บ 05 โ€บ add-value-to-array-java.html
How to Add Value to an Array Java - onlyxcodes
May 1, 2023 - Java's java.util.ArrayList class can be used in place of a regular array if you want to add an element to an array without giving an index.
๐ŸŒ
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 - In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array.
Discussions

Without using an ArrayList how would you insert a value into the middle of an array? Java
Probably not the best way but create an array one longer than the original Add elements from array until index i, insert extra element and then carry on adding from the array. Pretty simple for loop More on reddit.com
๐ŸŒ r/learnprogramming
4
0
February 4, 2022
Java array, add item into next empty index - Stack Overflow
If removals can occur anywhere, ... over the array to find a free spot. ... Sign up to request clarification or add additional context in comments. ... What happens to the element at [nextIndex] after you poped it? Would it leak? I think this example appears in Effective Java... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to add item to array without using built-in methods - Stack Overflow
You need to watch as the internal array grows and shrinks, and determine when it's time to re-allocate it with more empty space. grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/โ€ฆ ... You should keep the current index for the size of populated elements which looks like you do. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to add an element to Array and shift indexes? - Stack Overflow
I need to add an element to Array specifying position and value. For example, I have Array ... I understand that here should be a shift of Array's indexes, but don't see how to implement it in code. ... You can't shift indexes for arrays in Java. Arrays are fixed size. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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 - Oh, Java arrays. They are the object of intense love and hatred of hundreds of beginner software developers. Adding elements to an array that was already initialised is impossible, they saidโ€ฆ Actually, it is possible, but not in a classical meaningโ€ฆ and it isnโ€™t very convenient.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ without using an arraylist how would you insert a value into the middle of an array? java
r/learnprogramming on Reddit: Without using an ArrayList how would you insert a value into the middle of an array? Java
February 4, 2022 -

So for my homework I have to do a lot of array editing and already have a some code for the original array and for adding one element to the end. Iโ€™m not sure where I should fit this into the code. I know I need the length of the updated array but am not sure which method to use. Browsing the Java API I saw a couple possibilities like copyOf() or addPos()โ€ฆI could add the code but am curious to hear your approach to this and appreciate your help.

Thank you

๐ŸŒ
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 - In other words, adding n elements to an ArrayList requires O(n) time. An array can contain primitive as well as non-primitive data types, depending on the definition of the array. However, an ArrayList can only contain non-primitive data types. When we insert elements with primitive data types into an ArrayList, the Java compiler automatically converts the primitive data type into its corresponding object wrapper class.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-add-an-element-into-an-array-in-Java
How to add an element into an array in Java - Quora
Answer (1 of 10): There are two ways by which you can insert elements in java. One is by looping through all the element and another one is manually data inserting. Manual: [code] Scanner scan=new Scanner(System.in ); String[] name=new String[5]; name[0]="John"; name[1]...
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ add-elements-to-array-in-java
Add elements to Array in Java - Javatpoint
Add elements to Array in Java - Add elements to Array in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
๐ŸŒ
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 Discusses Various Methods to add Elements to the Array in Java. Some Options are to Use a New Array, to use an ArrayList etc.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_add.asp
Java ArrayList add() Method
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); System.out.println(cars); } } ... The add() method adds an item to the list. If an index is provided then the new item will be placed at the specified index, pushing all of the following elements in the list ahead by one.
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ array โ€บ java-array-exercise-9.php
Java - Insert an element into an array
May 9, 2025 - System.out.println("Original Array : " + Arrays.toString(my_array)); // Loop to shift elements to make space for the new element. for (int i = my_array.length - 1; i > Index_position; i--) { my_array[i] = my_array[i - 1]; } // Insert the new element at the specified position. my_array[Index_position] = newValue; // Print the modified array with the new element. System.out.println("New Array: " + Arrays.toString(my_array)); } } ... Original Array : [25, 14, 56, 15, 36, 56, 77, 18, 29, 49] New Array: [25, 14, 5, 56, 15, 36, 56, 77, 18, 29] ... Write a Java program to insert an element into a sorted array while maintaining the order.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ java-add-to-array
Adding Elements to an Array in Java: A How-To Guide
February 27, 2024 - However, the downside is that itโ€™s not efficient for large arrays or frequent additions. Each time you add an element, a new array is created, which can be memory-intensive and slow. Additionally, arrays in Java are fixed in size, so you canโ€™t add or remove elements from an existing array without creating a new one.
๐ŸŒ
The IoT Academy
theiotacademy.co โ€บ home โ€บ how to add elements in array in java? [with code examples]
How to Add Elements in Array in Java?
September 3, 2025 - If you find yourself frequently needing to add a new element to array in Java, consider using an ArrayList instead of an array. ArrayList is a resizable array implementation of the List interface, which allows you to add and remove elements dynamically. In this example, we create an ArrayList called numbers and add elements using the add() method. This method allows us to add elements dynamically without worrying about the size of the collection.
๐ŸŒ
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 - Initial Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Array with 50 inserted at position 5: [1, 2, 3, 4, 50, 5, 6, 7, 8, 9, 10] Here's how to do it. First get the element to be inserted, say element.
๐ŸŒ
KoderHQ
koderhq.com โ€บ tutorial โ€บ java โ€บ array
Java Arrays Tutorial | KoderHQ
Learn how to store multiple values of the same type in a single data container called an array. We discuss how to declare and initialize an array literal, how to create a new array instance object as well as how to add, access and mutate elements in an array.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-append-to-an-array-in-java
How to append to an array in Java
If you have Apache Commons Lang on your classpath, you can use the ArrayUtils.add() method to append an element to an array in a more readable and simplified way. This method performs the same operations under the hood as Arrays.copyOf(), but it abstracts the details, making your code cleaner. int[] arr = { 10, 20, 30 }; arr = ArrayUtils.add(arr, 40); Under the hood, add performs the same three steps described in the beginning. It just makes the process more readable. While arrays are great for storing data in Java, they have a fixed size.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 77367584 โ€บ how-to-add-elements-to-an-array-without-using-arraylist
java - How to add elements to an array without using ArrayList? - Stack Overflow
So if xmin is 1 and incretement is 2, the array would be { 1, 3, 5, 7 } etc, and the program fills the array up to length n (another user input). The code for this part works currently but I just realized we're not allowed to use StringBuilder, Arrays class (so no ArrayList), or any class in java.util.stream.