Look at java.util.LinkedList or java.util.ArrayList

List<Integer> x = new ArrayList<Integer>();
x.add(1);
x.add(2);
Answer from corsiKa on Stack Overflow
Top answer
1 of 11
114

Look at java.util.LinkedList or java.util.ArrayList

List<Integer> x = new ArrayList<Integer>();
x.add(1);
x.add(2);
2 of 11
67

Arrays in Java have a fixed size, so you can't "add something at the end" as you could do in PHP.

A bit similar to the PHP behaviour is this:

int[] addElement(int[] org, int added) {
    int[] result = Arrays.copyOf(org, org.length +1);
    result[org.length] = added;
    return result;
}

Then you can write:

x = new int[0];
x = addElement(x, 1);
x = addElement(x, 2);

System.out.println(Arrays.toString(x));

But this scheme is horribly inefficient for larger arrays, as it makes a copy of the whole array each time. (And it is in fact not completely equivalent to PHP, since your old arrays stays the same).

The PHP arrays are in fact quite the same as a Java HashMap with an added "max key", so it would know which key to use next, and a strange iteration order (and a strange equivalence relation between Integer keys and some Strings). But for simple indexed collections, better use a List in Java, like the other answerers proposed.

If you want to avoid using List because of the overhead of wrapping every int in an Integer, consider using reimplementations of collections for primitive types, which use arrays internally, but will not do a copy on every change, only when the internal array is full (just like ArrayList). (One quickly googled example is this IntList class.)

Guava contains methods creating such wrappers in Ints.asList, Longs.asList, etc.

๐ŸŒ
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 - Add the new element in the n+1th position. 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)); } }
Discussions

java - How to add new elements to an array? - Stack Overflow
I'm not that experienced in Java but I have always been told that arrays are static structures that have a predefined size. You have to use an ArrayList or a Vector or any other dynamic structure. ... and you want to add some elements to it like numbers please us StringBuilder which is much ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Simplest way to add elements to an array?
If you had wanted a C-like API, you should have programmed in C. In Java we can allocate a known number of elements in an array. Upon allocation, each element is null. We initialize each either in a loop, or, if your batch is small, by direct index addressing. But there are easier ways. You may want to consider using the ArrayList generic class. That collection class does expose add and remove methods. More on reddit.com
๐ŸŒ r/learnjava
8
4
April 12, 2019
java - add arrays in array dynamically - Stack Overflow
I have tried the for loops inside ... but that did not worked.it says "java.lang.ArrayIndexOutOfBoundsException: 0" ... Your e_list is an empty array, yet you want to set a value into the first element. There is no first element in an empty array. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to dynamically allocate an array and add strings to it? (In C)
Important question: Do you want to return an array of characters, or an array of strings? More on reddit.com
๐ŸŒ r/learnprogramming
8
1
February 17, 2022
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ creating-a-dynamic-array-in-java
Dynamic Array in Java - GeeksforGeeks
November 13, 2024 - The size of the new array increases to double the size of the original array. Now all elements are retained in a new array which is in the specified array domain size and the rest are added after them in the newly formed array. This array keeps on growing dynamically. Below are the Steps to create dynamic array in Java:
๐ŸŒ
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 - Instead, you often use ArrayList. ArrayList list = new ArrayList<>(); list.add(1); list.add(2); Key Points: Arrays in some languages (like Python, JavaScript) are dynamic and can grow.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-append-to-an-array-in-java
How to append to an array in Java
Method 1: Arrays.copyOf() method creates a new, larger array, copies the original data, and adds the new element at the end. Method 2: ArrayUtils.add() is a part of Apache Commons Lang.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ How-to-add-items-to-an-array-in-java-dynamically
How to add items to an array in java dynamically?
July 30, 2019 - Since the size of an array is fixed you cannot add elements to it dynamically. But, if you still want to do it then, Convert the array to ArrayList object. Add the required element to the array list.
๐ŸŒ
Java67
java67.com โ€บ 2018 โ€บ 01 โ€บ how-to-add-new-elements-to-array-java.html
How to add and remove Elements from an Dynamic Array in Java? Example Tutorial | Java67
What do we do? if we have an array of active users and new user logs in how do we store that into an array? how do we remove a user when he logs out. Well, you can use ArrayList instead of an array, which allows you to add or remove elements. It's like a dynamic array that can grow and shrink in size if needed. In this article, I will show you how to add and remove elements from an ArrayList in Java.
Find elsewhere
Top answer
1 of 16
477

The size of an array can't be modified. If you want a bigger array you have to instantiate a new one.

A better solution would be to use an ArrayList which can grow as you need it. The method ArrayList.toArray( T[] a ) gives you back your array if you need it in this form.

List<String> where = new ArrayList<String>();
where.add( ContactsContract.Contacts.HAS_PHONE_NUMBER+"=1" );
where.add( ContactsContract.Contacts.IN_VISIBLE_GROUP+"=1" );

If you need to convert it to a simple array...

String[] simpleArray = new String[ where.size() ];
where.toArray( simpleArray );

But most things you do with an array you can do with this ArrayList, too:

// iterate over the array
for( String oneItem : where ) {
    ...
}

// get specific items
where.get( 1 );
2 of 16
124

Use a List<String>, such as an ArrayList<String>. It's dynamically growable, unlike arrays (see: Effective Java 2nd Edition, Item 25: Prefer lists to arrays).

import java.util.*;
//....

List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
System.out.println(list); // prints "[1, 2, 3]"

If you insist on using arrays, you can use java.util.Arrays.copyOf to allocate a bigger array to accomodate the additional element. This is really not the best solution, though.

static <T> T[] append(T[] arr, T element) {
    final int N = arr.length;
    arr = Arrays.copyOf(arr, N + 1);
    arr[N] = element;
    return arr;
}

String[] arr = { "1", "2", "3" };
System.out.println(Arrays.toString(arr)); // prints "[1, 2, 3]"
arr = append(arr, "4");
System.out.println(Arrays.toString(arr)); // prints "[1, 2, 3, 4]"

This is O(N) per append. ArrayList, on the other hand, has O(1) amortized cost per operation.

See also

  • Java Tutorials/Arrays
    • An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
  • Java Tutorials/The List interface
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ java-add-to-array
Adding Elements to an Array in Java: A How-To Guide
February 27, 2024 - We then create an ArrayList and add elements to it using the add() method. The add() method allows us to add elements to the ArrayList without worrying about its size. The primary difference between using ArrayList and standard arrays is the ...
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ dynamic array in java | working, uses & more (+examples)
Dynamic Array In Java | Working, Uses & More (+Examples)
January 10, 2025 - When you add more elements than ... a dynamic array in Java is straightforward, especially with the help of the ArrayList class from the Java Collections Framework....
๐ŸŒ
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 need to add multiple elements at once and you are thinking about how to add multiple elements in arraylist in Java, then you can use the addAll() method of the ArrayList class.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 656606 โ€บ java โ€บ Populate-Array-list-dynamically
Populate Array or list dynamically (Beginning Java forum at Coderanch)
October 14, 2015 - Find the right place (within your existing code) to - Declare and initialize the list so that it gets set up once, and stays there for the whole game - add the number of guesses it took the user to get the number into that list. You will need a loop to go all the elements in the list and find the "maximum" Using a standard for loop:
๐ŸŒ
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 - Add all the elements of the previous data range to the new one, as well as the new values. Print the resulting array. Try creating such an array on your own and compare your code to that in the example below: // Java Program to add an element in an Array import java.lang.*; import java.util.*; class ArrayDemo { //Method to add an element x into array myArray public static int[] addX(int myArray[], int x) { int i; // create a new array of a bigger size (+ one element) int newArray[] = new int[myArray.length + 1]; // insert the elements from the old array into the new one for (i = 0; i < myArray
๐ŸŒ
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 - In this technique, you simply create a new array larger than the original by one element. You copy all the elements of the original array to the new array and then insert a new element at the end of the new array.
๐ŸŒ
onlyxcodes
onlyxcodes.com โ€บ 2023 โ€บ 05 โ€บ add-value-to-array-java.html
How to Add Value to an Array Java?
May 1, 2023 - Then, I added three string elements to the dynamic array using the add() method. Then I used The toArray () method to convert the dynamic array into a regular array. The code that follows first creates a dynamic array of strings and then expands it with three additional entries before changing ...
๐ŸŒ
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
Java arrays are fixed-size; you cannot change their length once created. To "add" an element you either create a new array and copy, or use a dynamic collection (preferred).
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-add-an-element-to-an-array-in-java
How to add an element to an Array in Java?
July 20, 2023 - The given array: 25 30 35 40 45 The new array after appending the element: [25, 30, 35, 40, 45, 50] Unlike other programming languages, Java does not provide any in-built methods for appending elements to an array. The reason is that array is of fixed size.
๐ŸŒ
Study.com
study.com โ€บ business courses โ€บ java programming tutorial & training
Adding to Arrays in Java - Lesson | Study.com
January 13, 2019 - Trying to access or add a bucket outside that range results in an out of bounds error, meaning you tried to add an element with an index outside the size of the array. Java does not support dynamic arrays, and so you can't add to the end of ...