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
🌐
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. Build sequential, selective, and iterative programs through engaging hands-on projects. Haven’t found what you were looking for? Contact Us · We can append two arrays using Arrays.copyOf() in Java:
🌐
TutorialKart
tutorialkart.com › java › java-array › java-append-to-array
Java - Append to Array
November 23, 2020 - In this example, we will take help of ArrayList to append an element to array. We shall implement the following steps. Take input array arr1. Create an ArrayList with elements of arr1. Append the element to the ArrayList. Convert ArrayList to array.
Discussions

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
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
How to append multiple values to an array without spamming ".append( )".
You could put the values into a new array, and then call append_array on the array you want them in, with the new array as the argument. More on reddit.com
🌐 r/godot
10
4
February 13, 2023
Is there a way to insert an array at the end of a 2d array?
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
9
5
November 3, 2022
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
🌐
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};
🌐
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.

🌐
Know Program
knowprogram.com › home › how to append an array in java
How To Append An Array In Java - Know Program
August 11, 2022 - To append an array to another existing array, we have to create an array with a size equal to the sum of those arrays. Later we have to copy the first array, and then the second array to the new array.
🌐
Programming.Guide
programming.guide › java › array-append.html
Java: Appending to an array | Programming.Guide
In Java arrays can't grow, so you need to create a new array, larger array, copy over the content, and insert the new element. Example: Append 40 to the end of arr ·
Find elsewhere
🌐
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 - 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 ...
🌐
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.
🌐
Mkyong
mkyong.com › home › java › java – append values into an object[] array
Java - Append values into an Object[] array - Mkyong.com
February 23, 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 ...
🌐
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 in some languages like Java or C have fixed sizes, so you’ll need to use dynamic structures like ArrayList or manually create a new array with a larger size.
🌐
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
🌐
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 - The second approach is we can use ArrayList to add elements to an array because ArrayList handles dynamic resizing automatically.
🌐
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.
🌐
Linux Hint
linuxhint.com › append-to-an-array-in-java
Linux Hint – Linux Hint
February 28, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
LabEx
labex.io › tutorials › java-add-elements-to-array-and-arraylist-117386
Java - Add Elements to Array and ArrayList
import java.util.Arrays; public class ArrayAppend { public static int[] appendToArray(int[] oldArr, int elementToAdd) { int[] newArr = Arrays.copyOf(oldArr, oldArr.length + 1); newArr[newArr.length - 1] = elementToAdd; return newArr; } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println("Initial Array: " + Arrays.toString(arr)); arr = appendToArray(arr, 6); arr = appendToArray(arr, 7); arr = appendToArray(arr, 8); System.out.println("After adding elements: " + Arrays.toString(arr)); } }
🌐
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.
🌐
JavaBeat
javabeat.net › home › how to add an element to an array in java
How to Add an Element to an Array in Java
November 30, 2023 - Create a new integer array using the “Integer” class having the size “1” greater than the formerly defined array. Apply the “for” loop such that at the specified index, the value “25” becomes appended to the new array and the ...
🌐
Quora
quora.com › How-can-I-append-a-number-into-a-java-array
How to append a number into a java array - Quora
Use java.util.ArrayListwzxhzdk:1 (or IntStream/primitive-specialized libraries) for dynamic resizing, then convert to int[] if needed. ... For many appends with primitive ints avoid boxing: use libraries such as fastutil (IntArrayList), Trove (TIntArrayList), or Eclipse Collections.