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 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
Assigning values to an array without a loop? (Java)
This post was bulk deleted with Redact which also removes your info from data brokers. Works on Reddit, Twitter, Discord, Instagram and all major social media platforms. knee market pie run steer apparatus dinner ring summer roof More on reddit.com
๐ŸŒ r/AskProgramming
3
1
May 19, 2022
๐ŸŒ
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.
๐ŸŒ
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 - Once we have created a new array, we can easily append the new element to the array: ... Inserting an element at a given index without losing the previously added elements is not a simple task in arrays.
๐ŸŒ
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

๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ java-add-to-array
Adding Elements to an Array in Java: A How-To Guide
February 27, 2024 - While adding elements to an array in Java might seem straightforward, it can sometimes lead to unexpected issues. Letโ€™s discuss some common problems and their solutions. One common issue when working with arrays is the ArrayIndexOutOfBoundsException. This exception is thrown to indicate that an array has been accessed with an illegal index...
๐ŸŒ
Reddit
reddit.com โ€บ r/askprogramming โ€บ assigning values to an array without a loop? (java)
r/AskProgramming on Reddit: Assigning values to an array without a loop? (Java)
May 19, 2022 -

I'm looking for a way to assign values to an array without the need for a for loop. Ideally my array would look like this:

int[] arr = new int[n]

Where n is input by the user. For example, if the user input "3", I want the array elements to have the values of

arr[0] = 0; arr[1] = 1; arr[2] = 2;

...and so on. I know how to do this with a for loop, but I am running into issues because I have to pull the values later, but I can't because they're nested in the loop. I also can't really use {...} because the array size is determined by the user. Is there a way to do this without a loop?

๐ŸŒ
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 - Arrays in Java have fixed sizes, so you canโ€™t directly add elements. Instead, you often use ArrayList. ArrayList list = new ArrayList<>(); list.add(1); list.add(2); ... Arrays in some languages (like Python, JavaScript) are dynamic and can grow.
๐ŸŒ
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.
๐ŸŒ
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 - Add element at position using list.add(position, element). Convert ArrayList back to array and print. Below is the implementation of the above approach: ... // Java Program to Insert an element // at a specific position in an Array // using ArrayList import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class AddElementAtPositionInArray { // Method to add element at position private static void addElement(Integer[] arr, int element, int position) { // Printing the original array System.out.println("Initial Array:\n" + Arrays.toString(arr)); // Converting array to A