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
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
How is my implementation of a dynamic array in Java, and especially the unit tests? I know it's a bit much to ask, but a quick glance and some feedback would mean a lot!
generally looks like it makes sense! a few small-ish points many other implementations use the word capacity instead of allocatedLength in doubleMemoAllocation you calculate the new capacity twice - assign it to a variable & reuse it so you've got one source of truth and the code is easier to maintain + reason about rather than having if (count == allocatedLength) doubleMemoAllocation(); in two places i'd create a method called ensureCapacity() which contains that logic & then in your inset methods you can call it. this happens to be what the java arraylist does insert is a special case of insertAt, so you can just call it with the final index. also group these methods next to each other i may be wrong, but is insertAt missing an increment to count? your test Array keeps resizing never actually confirms that it resizes, internally you could have just set the capacity to 100 & the test would be functionally the same. other languages will have a getCapacity() method which tells you how big the array list can get, but the java stdlib one doesn't reverse could be done without allocating a new array if you swap indices using a temporary variable print should be toString and should use a StringBuilder for more efficiency than adding strings together return strRepr == "[" you are lucky if this works since == in java checks object identity. use strRepr.equals("[") instead in the case someone passes an initial capacity of 0 you could avoid creating a 0 sized array and just leave that field as null More on reddit.com
๐ŸŒ r/learnprogramming
6
5
November 8, 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.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ 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. Convert the Array list to array. import java.util.ArrayList; import java.util.Arrays; import ...
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:
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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).
๐ŸŒ
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 ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ dynamic array in java
Dynamic Array in Java - Scaler Topics
May 4, 2023 - The size of the ArrayList is dynamic which means it can be modified at runtime. ArrayList is a part of Java Collections framework and is present in java.util package. We all are familiar with Array data structure. It is one of the most commonly used data structures in all programming languages. ... data structures. Arrays have a fixed size and it has to be specified at the time of creation. This causes a menace when we want to add elements beyond its initial size at runtime.