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
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
Top answer
1 of 16
479

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
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 › 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
In Java, arrays cannot grow once created, so adding elements requires workarounds.
🌐
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!)
Find elsewhere
🌐
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: 2 weeks ago
🌐
Baeldung
baeldung.com › home › java › java array › concatenate two arrays in java
Concatenate Two Arrays in Java | Baeldung
September 2, 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.
🌐
W3Schools
w3schools.com › java › ref_arraylist_add.asp
Java ArrayList add() Method
Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms · Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add ...
🌐
Mkyong
mkyong.com › home › java › java – append values into an object[] array
Java – Append values into an Object[] array | mkyong.com
February 22, 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 - Trying to access or add a bucket outside that range results in an out of bounds error, meaning you tried to add an element with an index outside the size of the array. Java does not support dynamic arrays, and so you can't add to the end of the array with code.
🌐
Playwright
playwright.dev › locator
Locator | Playwright
The snapshot can be used to assert the state of the element in the test, or to compare it to state in the future. The ARIA snapshot is represented using YAML markup language: The keys of the objects are the roles and optional accessible names of the elements. The values are either text content or an array of child elements.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Spread_syntax
Spread syntax (...) - JavaScript | MDN
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.
🌐
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};
🌐
Substack
alexanderobregon.substack.com › alexander obregon's substack › kosaraju vs. tarjan in java
Kosaraju vs. Tarjan in Java - Alexander Obregon's Substack
April 3, 2026 - That number is assigned from a running counter and never changes later. Most Java versions store those values in an int[] index array, and unseen vertices start at -1. Tarjan also sets low[v] to that same index at first contact, because the vertex can always reach itself before any outgoing edge is checked.
🌐
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.
🌐
Quora
quora.com › How-do-we-add-two-arrays-in-java
How do we add two arrays in java? - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
Spring
spring.io › guides › gs › spring-boot
Getting Started | Building an Application with Spring Boot
Spring Boot offers a fast way to build applications. It looks at the Spring Boot modules you’ve added and at the beans you have configured, makes reasonable assumptions about what you are missing, and adds those items.
🌐
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.