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
Use new with a size when you want to create an empty array and fill it later. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Discussions

Why is the add(index, element) time complexity not constant in Java for array lists?

For array lists, you would have to move all elements to adjacent positions when you insert at an index. So it takes linear time ( more the number of elements already in the list, more time it takes to move them all ).

Also Java has nothing to do with time complexities of a data structure. It is universal.

More on reddit.com
🌐 r/learnjava
8
2
June 1, 2020
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
Is there a way to add elements to an Array List all at once?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
6
1
November 2, 2021
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
🌐
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 › arrays-in-java
Arrays in Java - GeeksforGeeks
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
🌐
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 ...
🌐
Educative
educative.io › answers › how-to-append-to-an-array-in-java
How to append to an array in Java
The question is: how do you add things to your array without breaking it? That’s where this Answer comes in! ... 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:
Find elsewhere
🌐
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)
So you can add things to ArrayLists and other collections, but the Array will never grow, it will be of fixed size, determined at creation. RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
🌐
W3Schools
w3schools.com › java › ref_arraylist_add.asp
Java ArrayList add() Method
Arrays Loop Through an Array Real-Life Examples Multidimensional Arrays Code Challenge · Java Methods Java Method Challenge Java Method Parameters
🌐
Baeldung
baeldung.com › home › java › java array › concatenate two arrays in java
Concatenate Two Arrays in Java | Baeldung
April 8, 2026 - However, since Java 5, the Collections utility class has introduced an addAll(Collection<? super T> c, T… elements) method. We can create a List object, then call this method twice to add the two arrays to the list.
🌐
Mkyong
mkyong.com › home › java › java – append values into an object[] array
Java - Append values into an Object[] array - Mkyong.com
February 23, 2014 - package com.hostingcompass.test; import java.util.ArrayList; import java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class TestApp2 { public static void main(String[] args) { TestApp2 test = new TestApp2(); test.process(); } private void process() { int[] obj = new int[] { 1, 2, 3 }; System.out.println("Before int [] "); for (int temp : obj) { System.out.println(temp); } System.out.println("\nAfter Object [] "); int[] newObj = appendValue(obj, 99); for (int temp : newObj) { System.out.println(temp); } } private int[] appendValue(int[] obj, int newValue) { //convert int[] to
🌐
Study.com
study.com › business courses › java programming tutorial & training
Adding to Arrays in Java - Lesson | Study.com
January 13, 2019 - Remember that Java starts counting at zero! This rule also applies to our loop: the first time through we are at 0, so the value in the first bucket is 0. Here is the output of that code: To unlock this lesson you must be a Study.com member Create an account · Once you've established the array size, you can't add ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-arraylist-add-method-java
Java ArrayList add() Method with Examples - GeeksforGeeks
March 14, 2026 - ... Exception: Throws ... args) { // Creating an empty ArrayList ArrayList<Integer> al = new ArrayList<>(); // Use add() method to // add elements in the list al.add(10); al.add(20); al.add(30); al.add(40); ...
🌐
AlgoCademy
algocademy.com › link
Array Push in Java | AlgoCademy
We can add elements to the end of an ArrayList using the .add() method.
🌐
LabEx
labex.io › tutorials › java-add-elements-to-array-and-arraylist-117386
Java - Add Elements to Array and ArrayList
To run the code, open the terminal in the project folder, then compile and run the code with the following commands: ... import java.util.Arrays; public class ArrayInsert { public static int[] insertAtIndex(int[] arr, int elementToAdd, int index) { if (index > arr.length || index < 0) { throw new IndexOutOfBoundsException("Index is out of range!"); } else { int[] newArr = new int[arr.length + 1]; for (int i = 0, j = 0; i < arr.length; i++, j++) { if (i == index) { newArr[j++] = elementToAdd; } newArr[j] = arr[i]; } return newArr; } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println("Initial Array: " + Arrays.toString(arr)); arr = insertAtIndex(arr, 6, 2); System.out.println("After inserting at index 2: " + Arrays.toString(arr)); } }
🌐
Itmorelia
edistancia.itmorelia.edu.mx › 5-sneaky-ways-to-add-elements-to-an-array-in-java.html
5 Sneaky Ways To Add Elements To An Array In Java - Itmorelia
May 12, 2024 - As the world becomes increasingly digitized, the demand for skilled Java developers has skyrocketed. The ability to efficiently add elements to an array in Java has become a deciding factor in the success of many applications. The trend of 5 sneaky ways to add elements to an array in Java has been observed globally, with developers seeking innovative solutions to optimize their code.
🌐
Playwright
playwright.dev › locator
Locator | Playwright
Whether to bypass the actionability checks. Defaults to false. modifiers Array<"Alt" | "Control" | "ControlOrMeta" | "Meta" | "Shift"> (optional)#
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Spread_syntax
Spread syntax (...) - JavaScript | MDN
May 22, 2026 - The spread (...) syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected. In an object literal, the spread syntax enumerates the properties of an object and adds the key-value pairs to the object being created.
🌐
Silicon Cloud
silicloud.com › home › adding elements to java arrays
Adding Elements to Java Arrays - Blog - Silicon Cloud
August 6, 2025 - Learn how to add elements to Java arrays despite fixed size. Create new arrays, copy elements & extend dynamically.
🌐
iO Flood
ioflood.com › blog › java-add-to-array
Adding Elements to an Array in Java: A How-To Guide
February 27, 2024 - In this example, we create an array of integers with a length of 3 and then populate it with the numbers 1, 2, and 3. Note that the indices of the array start at 0 and go up to 2, which is the length of the array (3) minus one. The static nature of arrays in Java means that you can’t add or remove elements from an existing array without creating a new one.
🌐
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};