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
You can access an array element by referring to the index number.
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
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
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
🌐
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.

🌐
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
🌐
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)
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
🌐
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, ...
🌐
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 ...
🌐
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 first approach is that we can create a new array whose size is one element larger than the old size. Create a new array of size n+1, where n is the size of the original array. Add the n elements of the original array to this array.
🌐
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.
🌐
W3Schools
w3schools.com › java › ref_arraylist_add.asp
Java ArrayList add() Method
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
🌐
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).
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation. Each ArrayList instance has a capacity.
🌐
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 - ArrayList list = new ArrayList<>(); list.add(1); list.add(2); ... Arrays in some languages (like Python, JavaScript) are dynamic and can grow. In static languages (like Java, C), you often use resizable structures like ArrayList or Vector.
🌐
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 - However, the downside is that it’s not efficient for large arrays or frequent additions. Each time you add an element, a new array is created, which can be memory-intensive and slow. Additionally, arrays in Java are fixed in size, so you can’t add or remove elements from an existing array ...
🌐
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) ); } }