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 );
Answer from tangens on Stack Overflow
Top answer
1 of 16
478

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
🌐
Educative
educative.io › answers › how-to-append-to-an-array-in-java
How to append to an array in Java
Java arrays can’t grow once they have been created; so, to add an element, you need to use the one of the following methods: ... One of the most common ways to append an element to an array is by using the Arrays.copyOf() method, which creates ...
Discussions

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 add something to primitive array in Java?
Allocate a new bigger array, copy existing values over, add the new one. You probably don't really want to be using an array for what you're doing if you're doing this a lot. More on reddit.com
🌐 r/learnprogramming
15
1
December 2, 2015
Can't append to an array of Strings inside my view
You are using ForEach in your function which returns a View. You should use the for in loop instead and it should fix it. More on reddit.com
🌐 r/SwiftUI
5
2
November 21, 2023
How to append multiple values to an array without spamming ".append( )".
You could put the values into a new array, and then call append_array on the array you want them in, with the new array as the argument. More on reddit.com
🌐 r/godot
10
4
February 13, 2023
🌐
Reddit
reddit.com › r/learnprogramming › how to add something to primitive array in java?
r/learnprogramming on Reddit: How to add something to primitive array in Java?
December 2, 2015 -

Hi all, so I have an empty array I just created like this:

int[] array = new int[5];

How do I add numbers to it? I tried array.append() but it's not working. I don't want to do it manually like array[0] etc I want to just keep adding to the tail.

EDIT: I'm sure I won't go over the limit of what the array can contain. I just want to know how to add to the tail of the array without having to specify what position is being added.

🌐
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 - The second approach is we can use ArrayList to add elements to an array because ArrayList handles dynamic resizing automatically.
🌐
TutorialKart
tutorialkart.com › java › java-array › java-append-to-array
Java - Append to Array
November 23, 2020 - Java Append to Array - To append element(s) or another array to array in Java, create a new array with required size, which is more than the original array. Now, add the original array elements and element(s) you would like to append to this ...
🌐
Blogger
javahungry.blogspot.com › 2020 › 04 › append-array-java.html
2 ways : append to an Array in Java | Java Hungry
2. Add the element to the ArrayList using the add() method. 3. Convert the ArrayList to Array using the toArray() method. import java.util.*; public class JavaHungry { public static void main(String args[]) { // Initialize givenArray Integer[] num = {3,6,9,12,15};
🌐
JanBask Training
janbasktraining.com › community › java › how-can-i-append-an-array-in-java-programming-language
How can I append an array in Java programming language? | JanBask Training Community
January 10, 2024 - Here is the example given of how you can append the element of an Array list in the context of Java programming language:- ... Public class GradeManager { Public static void main(String[] args) { // Suppose you have an ArrayList to store grades ArrayList grades = new ArrayList<>(); // Add existing grades Grades.add(85); Grades.add(90); Grades.add(78); // New student’s grade Int newGrade = 92; // Append the new grade Grades.add(newGrade); // Now, ‘grades’ ArrayList contains the new grade System.out.println(“Updated grades: “ + grades); }}
Find elsewhere
🌐
Quora
quora.com › How-can-I-append-a-number-into-a-java-array
How to append a number into a java array - Quora
Answer (1 of 9): The other answers solve your problem, but I want to note an important difference here: Python lists are not arrays! They are objects with a lot of nice methods. The recommended Java ArrayList is the analogous structure. It's also an object with a lot of nice methods. Java arrays...
🌐
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 - arr = [1, 2, 3] arr.append(4) # [1, 2, 3, 4] Use extend() to add multiple elements. arr.extend([5, 6]) # [1, 2, 3, 4, 5, 6] Use insert() to add an element at a specific position. In [removed] Use push() to add elements at the end. let arr = ...
🌐
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 - As we’ve already seen, arrays are of fixed size. 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.
🌐
LabEx
labex.io › tutorials › java-add-elements-to-array-and-arraylist-117386
Java - Add Elements to Array and ArrayList
import java.util.Arrays; public class ArrayAppend { public static int[] appendToArray(int[] oldArr, int elementToAdd) { int[] newArr = Arrays.copyOf(oldArr, oldArr.length + 1); newArr[newArr.length - 1] = elementToAdd; return newArr; } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println("Initial Array: " + Arrays.toString(arr)); arr = appendToArray(arr, 6); arr = appendToArray(arr, 7); arr = appendToArray(arr, 8); System.out.println("After adding elements: " + Arrays.toString(arr)); } }
🌐
Programming.Guide
programming.guide › java › array-append.html
Java: Appending to an array | Programming.Guide
In Java arrays can't grow, so you need to create a new array, larger array, copy over the content, and insert the new element.
🌐
Edureka Community
edureka.co › home › community › categories › java › how can i add new elements to an array in java
How can I add new elements to an Array in Java | Edureka Community
April 19, 2018 - I want to append elements to an array String[] mm= {"5","4","3"}; mm.append("2"); This ... give me a way in which I can add elements to this array.
🌐
iO Flood
ioflood.com › blog › java-add-to-array
Adding Elements to an Array in Java: A How-To Guide
February 27, 2024 - When adding elements to an array in Java, keep the following tips in mind: Always check the array’s length before trying to add an element to avoid ArrayIndexOutOfBoundsException.
🌐
Mkyong
mkyong.com › home › java › java – append values into an object[] array
Java - Append values into an Object[] array - Mkyong.com
February 22, 2014 - Object[] obj = new Object[] { "a", "b", "c" }; ArrayList<Object> newObj = new ArrayList<Object>(Arrays.asList(obj)); newObj.add("new value"); newObj.add("new value 2"); ... package com.mkyong.test; import java.util.ArrayList; import ...
🌐
AlgoCademy
algocademy.com › link
Array Push in Java | AlgoCademy
The key concept here is the use of the .add() method to append elements to an ArrayList.
🌐
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
🌐
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.
🌐
JanBask Training
janbasktraining.com › community › java › how-to-append-something-to-an-array
How to append something to an array? | JanBask Training Community
April 18, 2025 - Appending something to an array means adding a new element to the end of that array. The method you use depends on the programming language you're working with. Here's a quick breakdown of how you can do this in some popular languages: ... Arrays in some languages like Java or C have fixed sizes, so you’ll need to use dynamic structures like ArrayList or manually create a new array with a larger size.