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
🌐
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 - Simply add the required element in the list using add() method. Convert the list to an array using toArray() method and return the new array. ... // Java Program to add an element in an Array // with the help of ArrayList import java.io.*; import ...
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
🌐
W3Schools
w3schools.com › java › ref_arraylist_add.asp
Java ArrayList add() Method
The add() method adds an item to the list. If an index is provided then the new item will be placed at the specified index, pushing all of the following elements in the list ahead by one.
🌐
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 - Q #3) How do you add an ArrayList to an Array in Java? Answer: Create a list of n items. Then use the toArray method of the list to convert it to the array.
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
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 Fac
🌐
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 - The insert() method returns a new array containing a larger number of elements, with the new element at the specified index and all remaining elements shifted one position to the right.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-list-add-addall-methods
How To Use add() and addAll() Methods for Java List | DigitalOcean
September 10, 2025 - Memory efficiency: Reduces method call overhead and potential array resizing · Optimized implementations: ArrayList can pre-allocate space for bulk additions ... package com.journaldev.examples; import java.util.*; import java.util.stream.Collectors; public class ListAddAllExamples { public static void main(String[] args) { // Example 1: Basic addAll() operations List<Integer> primeNumbers = new ArrayList<>(); primeNumbers.addAll(Arrays.asList(2, 7, 11)); System.out.println("After adding [2, 7, 11]: " + primeNumbers); primeNumbers.addAll(1, Arrays.asList(3, 5)); System.out.println("After inse
Find elsewhere
🌐
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 - Once you no longer need to edit an array, convert back to the original data type with the help of toArray() method. With all the methods and conversions, this can seem confusing at first. Let’s take a look at the example of using asList() to clear things up. // Code for adding Java arrays to a program import java.lang.*; import java.util.*; class ArrayDemo{ //Let’s add a new element to an array public static Integer[] addX(Integer myArray[], int x) { int i; //turn array into ArrayList using asList() method List
🌐
Educative
educative.io › answers › how-to-append-to-an-array-in-java
How to append to an array in Java
Use ArrayList.add() If we need a dynamic data structure that can grow and shrink automatically, ArrayList is the best option. Kickstart your programming journey with "Learn Java." Master essential concepts like input/output methods, user-defined methods, and basic data types.
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-an-element-to-an-array-in-java
How to add an element to an Array in Java?
Moving further add a new element to the ArrayList using a built-in method ?add()'. Display the result and exit. In the following example, we will add an element to the given array by using the ArrayList. import java.util.*; public class Increment { public static void main(String[] args) { Integer aray[] = {25, 30, 35, 40, 45}; int sz = aray.length; System.out.print("The given array: "); for(int i = 0; i < sz; i++) { System.out.print(aray[i] + " "); } System.out.println(); // creating an ArrayList with old array ArrayList<Integer> arayList = new ArrayList<Integer>(Arrays.asList(aray)); arayList.add(50); // adding new element System.out.print("The new array after appending the element: " + arayList); } }
🌐
iO Flood
ioflood.com › blog › java-add-to-array
Adding Elements to an Array in Java: A How-To Guide
February 27, 2024 - Always check the array’s length before trying to add an element to avoid ArrayIndexOutOfBoundsException. Consider using ArrayList or other Java collections for more flexible and efficient array manipulation. When performance is a concern, consider using System.arraycopy() or similar methods for faster array copying.
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
An ArrayList keeps elements in the same order you add them, so the first item you add will be at index 0, the next at index 1, and so on. To access an element in the ArrayList, use the get() method and refer to the index number:
🌐
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.
🌐
Javatpoint
javatpoint.com › add-elements-to-array-in-java
Add elements to Array in Java - Javatpoint
Add elements to Array in Java - Add elements to Array in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-arraylist-add-method-java
Java ArrayList add() Method with Examples - GeeksforGeeks
December 10, 2024 - This method inserts the specified element at a given position in the ArrayList. It shifts the current element at that position and subsequent elements to the right. ... Exception: Throws IndexOutOfBoundsException if the specified index is out ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable · Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods ...
🌐
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 find yourself frequently needing to add a new element to array in Java, consider using an ArrayList instead of an array. ArrayList is a resizable array implementation of the List interface, which allows you to add and remove elements dynamically. In this example, we create an ArrayList called numbers and add elements using the add() method.
🌐
W3Docs
w3docs.com › java
add an element to int [] array in java
If you want to modify the original array, you can use the first method and assign the result back to the original array: int[] original = {1, 2, 3}; int[] result = new int[original.length + 1]; System.arraycopy(original, 0, result, 0, original.length); result[result.length - 1] = 4; original = result; ... int[] original = {1, 2, 3}; List<Integer> list = IntStream.of(original).boxed().collect(Collectors.toList()); list.add(4); IntStream.range(0, list.size()).forEach(i -> original[i] = list.get(i));