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
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
Discussions

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
Adding Items to an array in Java - Stack Overflow
I wanted to create an array that has a length specified by the user, and also wanted to have it filled by a loop command, and then it should be copied to another array by another loop command, so I... More on stackoverflow.com
🌐 stackoverflow.com
List vs Array vs ArrayList
These are three different things: Array - a very basic data structure. It has a fixed, predetermined size that must be known at the time of creating (instantiating) the array. Think of it as a sorting box with several slots. Here, you have .length - without parentheses as it is a property, not a method List - an interface (not a class) that defines certain behavior. It cannot be instantiated on its own. ArrayList - a concrete class that implements the List interface. When you see a declaration like List data = new ArrayList<>(); there are several things going on: Listprogramming against the interface - here, the interface List is used as data type - this is good practice as it allows you to at any time change the internal representation of the List - e.g. from ArrayList to LinkedList if you figure out that the other better suits your needs. this is a data type identifier. It limits the type of elements that can be stored in the list. Here, only String objects are allowed. If you were to omit this, the list would store Object (the ancestor of all Java classes) and descendant instances (i.e. Object and any subclass of it). data - just the variable name new - the keyword to create a new Object Instance ArrayList<>() - the constructor call. Here, the constructor without parameters of the ArrayList class is called. The diamond operator <> is used to infer (take) the data type for the ArrayList from the variable type (here String). So, the whole is: we create a new variable of type List that can only accept String elements and ist internally represented as ArrayList. More on reddit.com
🌐 r/javahelp
15
7
September 12, 2023
Adding objects to array list - Last entry overwrites all elements

You are only creating one instance of the class Recipe. And you are adding it multiple times to the ArrayList. This will modify the object everytime.

What you need to do instead is to create a new Recipe before setting the name, and then add it to the ArrayList.

More on reddit.com
🌐 r/javahelp
7
10
May 31, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
This Java program demonstrates how to pass an array to a method. An integer array arr is declared and initialized in the main method. The sum() method is called with arr as an argument. Inside the sum() method, all array elements are added using a for loop.
Published   May 8, 2026
🌐
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.

🌐
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 this tutorial, we’ll briefly look at the similarities and dissimilarities in memory allocation between Java arrays and the standard ArrayList. Furthermore, we’ll see how to append and insert elements in an array and ArrayList.
🌐
Coderanch
coderanch.com › t › 745116 › java › add-elements-array-arrays
How to add elements to an array of arrays (Java in General forum at Coderanch)
August 26, 2021 - This will need to do until Java catches up. Kind regards, Glyn ... No, there is no need to do that. I used to read the same file twice with a Scanner for that very reason, but you can get a Stream to do the counting for you. You end up with something like this:-In line 7 you have a Stream processing every line in the file; toArray() in line 8 needs to know the size for the array. That method counts the Stream's elements and uses XYZ::new where the count sets the length for a new array.
Find elsewhere
🌐
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 ...
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
🌐
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 n elements of the original array to this array. 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 ...
🌐
W3Schools
w3schools.com › java › ref_arraylist_add.asp
Java ArrayList add() Method
import java.util.ArrayList; public ... cars.add("Ford"); cars.add("Mazda"); System.out.println(cars); } } ... The add() method adds an item to the list....
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - NullPointerException - if the specified array is null ... Returns the element at the specified position in this list. ... Replaces the element at the specified position in this list with the specified element. ... Appends the specified element to the end of this list. ... Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
🌐
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 - Use unshift() to add elements at the beginning. Use splice() to insert elements at any position. ... Arrays in Java have fixed sizes, so you can’t directly add elements.
🌐
Tutorjoes
tutorjoes.in › java_programming_tutorial › Insert_element_array_in_java
Insert an element (specific position) into an array in Java
After shifting the elements, the program sets the element at the specified index to the new value. Finally, the program prints the updated array using · Arrays.toString(a) and stores the result in a string. import java.util.Arrays; public class Insert_element_array { public static void main(String args[]) { //Program to insert a element in specific index of an array int[] a = {10,20,30,40,50,60,70,80,90,100}; int index = 2; int value = 55; System.out.println("Before Insert "+Arrays.toString(a) ); for(int i=a.length-1;i>index;i--) { a[i]=a[i-1]; } a[index]=value; System.out.println("After Insert "+Arrays.toString(a) ); } }
🌐
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 - But think of Java arrays as a train that can add more carriages, accommodating more passengers, or in this case, elements. This guide will walk you through the process of adding elements to an array in Java, from the basics to more advanced techniques.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-list-add-addall-methods
How To Use add() and addAll() Methods for Java List | DigitalOcean
September 10, 2025 - Use add(int index, E element) for single elements or addAll(int index, Collection<? extends E> c) for multiple elements: list.add(2, "middle"); list.addAll(1, Arrays.asList("A", "B")); Mastering Java’s add() and addAll() methods is essential for efficient programming.
🌐
Study.com
study.com › business courses › java programming tutorial & training
Adding to Arrays in Java - Lesson | Study.com
January 13, 2019 - Some programming languages support dynamic arrays, which are arrays that let you add to the end. Java does not. The following code tries to add a sixteenth element to the array. Don't forget that Java starts counting at zero! We've set the size to 15, so when we're adding at the index of 15, ...
🌐
CodeGym
codegym.cc › java blog › java arrays › how to add a new element to an array in java
How To Add an 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