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 ... of declaring separate variables for each value. To declare an array, define the variable type with square brackets [ ] :...
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
Discussions

How can I append something to an array in Java Script?
Answer: push() method.View 1 other answers by ✔ Expert Tutors on UrbanPro.com More on urbanpro.com
🌐 urbanpro.com
2
0
February 21, 2025
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
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
[Java] How to pass int[] array in an object constructor

It does if you add "new" before passing the array:

Number newArray = new Number(new int[] {1,2,3})
More on reddit.com
🌐 r/learnprogramming
4
11
March 4, 2017
🌐
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 - 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 ...
🌐
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 › ref_arraylist_add.asp
Java ArrayList add() Method
Access Modifiers Non-Access Modifiers ... Java Interface Java Anonymous Java Enum ... 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 Two Numbers ...
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!)
🌐
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
🌐
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.
🌐
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
🌐
JanBask Training
janbasktraining.com › community › java › how-can-i-append-an-array-in-java-programming-language
How can I append an array in Java programming language? | JanBask Training Community
January 10, 2024 - Here is the example given of how you can append the element of an Array list in the context of Java programming language:- ... Public class GradeManager { Public static void main(String[] args) { // Suppose you have an ArrayList to store grades ArrayList grades = new ArrayList<>(); // Add existing grades Grades.add(85); Grades.add(90); Grades.add(78); // New student’s grade Int newGrade = 92; // Append the new grade Grades.add(newGrade); // Now, ‘grades’ ArrayList contains the new grade System.out.println(“Updated grades: “ + grades); }}
🌐
arrayThis
arraythis.com
Online array converter; convert your text list to array | arrayThis
Is your list ready? In the need to optimize your list (remove duplicates, empty lines, sort, prefix and suffix, etc.) use KitTxt before. ... Why this? Because I needed a quick tool for passing portions of data (data excel columns, text lists, etc.) to array.
🌐
Study.com
study.com › business courses › java programming tutorial & training
Adding to Arrays in Java - Lesson | Study.com
January 13, 2019 - The following code adds another variable j, which helps us add values to the array. Each time we go through the for loop, we add the value of j to the bucket in the array. Next, we increase the value of j so that all buckets don't have the same value; then we print the value of each bucket. import java.util.*; public class Main { public static void main(String[] args) { //new array int[] buckets = new int[5]; System.out.println(buckets.length); int j = 0; //for loop for (int i = 0; i < buckets.length; i++) { buckets[i] = j; j += 5; System.out.println(buckets[i]); } } } }
🌐
Know Program
knowprogram.com › home › how to append an array in java
How To Append An Array In Java - Know Program
August 11, 2022 - In the addElement() method we have taken a temporary array having length = original array length + 1. Then we copied each element of the original array to the temporary array and inserted the given element at the end of the array.
🌐
UrbanPro
urbanpro.com › java › learn java
How can I append something to an array in Java Script? - UrbanPro
February 21, 2025 - Answer: push() method.View 1 other answers by ✔ Expert Tutors on UrbanPro.com
🌐
freeCodeCamp
freecodecamp.org › news › how-to-create-an-array-in-java
How to Create an Array in Java – Array Declaration Example
March 16, 2023 - Array declaration with default values is useful when you need to create an array with a fixed size, but you don't yet have specific values to initialize it with. You can later assign values to the elements of the array using indexing. A multi-dimensional array is an array that contains other arrays. In Java, you can create multi-dimensional arrays with two or more dimensions.
🌐
LeetCode
leetcode.com › problems › add-to-array-form-of-integer
Add to Array-Form of Integer - LeetCode
Can you solve this real interview question? Add to Array-Form of Integer - The array-form of an integer num is an array representing its digits in left to right order. * For example, for num = 1321, the array form is [1,3,2,1]. Given num, the array-form of an integer, and an integer k, return ...
🌐
Playwright
playwright.dev › locator
Locator | Playwright
In a nutshell, locators represent a way to find element(s) on the page at any moment. A locator can be created with the page.locator() method. Learn more about locators. ... When the locator points to a list of elements, this returns an array of locators, pointing to their respective elements.
🌐
Guvi
ftp.guvi.in › hub › java-examples-tutorial › add-elements-to-array-and-arraylist
Add Elements to Array and ArrayList
Let's learn how to append elements to an array and ArrayList. Append means to add to the end. We will first create a new array of larger size. Next, we will transfer the elements to this new array.
🌐
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.