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
Arrays Loop Through an Array Real-Life Examples Multidimensional Arrays Code Challenge · Java Methods Java Method Challenge Java Method Parameters · Parameters Return Values Code Challenge Java Method Overloading Java Scope Java Recursion · Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Class Challenge Java Constructors Java this Keyword Java Modifiers · Access Modifiers Non-Access Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java super Keyword Java Inner Classes Java Abstraction Java Interface Java Anonymous Java Enum
Discussions

Maintain order in array append
Hi, I am new to couchbase. I am appending an element to an array inside a json document. What I notice is that the order in which the appends are issued is not the order in which the elements occur in the array. For example, if the json document is {x:1, arrayx: [a, b, c]}, the append for c ... More on couchbase.com
🌐 couchbase.com
7
0
August 15, 2019
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: Printing an ArrayList in one line with foreach - removing last comma
But I'd like to use a foreach loop why? there's not an easy way afaik you could use a string builder by appending each item to the builder followed by a comma, then pop the trailing comma off and print the built string. not 100% aware of how System.out.print buffers its output, but my using a string builder you could ensure that it's just a singular write to the console edit: or just use String.join More on reddit.com
🌐 r/learnprogramming
12
2
February 4, 2021
Can't append to an array of Strings inside my view
You are using ForEach in your function which returns a View. You should use the for in loop instead and it should fix it. More on reddit.com
🌐 r/SwiftUI
5
2
November 21, 2023
🌐
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 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 Geeks { // Function to add x in arr public static int[] addX(int n, int arr[], int x) { int newarr[] = new int[n + 1]; // insert the elements from // the old array into the new array // insert all elements till n // then insert x at n+1 for (int i = 0; i < n; i++) newarr[i] = arr[i]; newarr[n] = x; return newarr; } public static void main(String[] args) { int n = 5; int arr[] = { 10, 20, 30, 40, 50}; int x = 70; // call the method to add x in arr arr = addX(n, arr, x); ...
🌐
Educative
educative.io › answers › how-to-append-to-an-array-in-java
How to append to an array in Java
No, arrays in Java have a fixed size once they are created, so we cannot append elements directly.
🌐
Mkyong
mkyong.com › home › java › java – append values into an object[] array
Java - Append values into an Object[] array - Mkyong.com
February 22, 2014 - Object[] obj = new Object[] { "a", "b", "c" }; ArrayList<Object> newObj = new ArrayList<Object>(Arrays.asList(obj)); newObj.add("new value"); newObj.add("new value 2"); ... package com.mkyong.test; import java.util.ArrayList; import ...
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)
You would declare it something like this: ArrayList< ArrayList<Double> > doubleArrayOfDoubles = new ArrayList< >(); for each row you would need to say: ArrayList<Double> row = new ArrayList<Double>(); doubleArrayOfDoubles.add( row ); and then add the elements along the other dimension either ...
🌐
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.
🌐
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 - This above code will create an array list by the name “grades” and will add the existing grades to it. Moreover, it will append the new grade of a new student by using the feature of “add()” in the array list.
🌐
Quora
quora.com › How-can-I-append-a-number-into-a-java-array
How to append a number into a java array - Quora
Java arrays have fixed length; you cannot directly "append" to an existing array. Common approaches: ... Create a new array with length old.length + 1, copy elements, set last element. ... Use java.util.ArrayListwzxhzdk:1 (or IntStream/primitive-specialized libraries) for dynamic resizing, ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-an-element-to-an-array-in-java
How to add an element to an Array in Java?
July 20, 2023 - You can add an element at a particular position in an array using the add() method of this class. import java.util.Scanner; import org.apache.commons.lang3.ArrayUtils; public class InsertingElements { public static void main(String args[]) { ...
🌐
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 - If you want to learn easy ways to add an element to Java arrays, this is a full guide on how to add to array a new element
🌐
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 append() to add a single element. ... Use extend() to add multiple elements. ... Use insert() to add an element at a specific position. ... Use push() to add elements at the end.
🌐
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 - So, to append an element, first, we need to declare a new array that is larger than the old array and copy the elements from the old array to the newly created array. After that, we can append the new element to this newly created array.
🌐
JanBask Training
janbasktraining.com › community › java › how-to-append-something-to-an-array
How to append something to an array? | JanBask Training Community
April 18, 2025 - Appending something to an array means adding a new element to the end of that array. The method you use depends on the programming language you're working with. Here's a quick breakdown of how you can do this in some popular languages: ... Arrays ...
🌐
Couchbase
couchbase.com › java sdk
Maintain order in array append - Java SDK - Couchbase Forums
August 15, 2019 - Hi, I am new to couchbase. I am appending an element to an array inside a json document. What I notice is that the order in which the appends are issued is not the order in which the elements occur in the array. For example, if the json document is {x:1, arrayx: [a, b, c]}, the append for c may have occurred before the append for b, but c occurs after b in the array.
🌐
Processing
processing.org › reference › append_.html
append() / Reference / Processing.org
String[] sa1 = { "OH", "NY", "CA"}; String[] sa2 = append(sa1, "MA"); println(sa2); // Prints updated array contents to the console: // [0] "OH" // [1] "NY" // [2] "CA" // [3] "MA" ...
🌐
AlgoCademy
algocademy.com › link
Array Push in Java | AlgoCademy
In this lesson, we covered the basics of appending items to an ArrayList in Java. We explored the .add() method, discussed common pitfalls and best practices, and provided examples and advanced techniques.
🌐
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.