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
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
🌐
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. ... Coding fundamentals as a game. Bite-sized lessons and challenges. ... Ready to start your journey? Your streak is waiting. ... 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

Simplest way to add elements to an array?
If you had wanted a C-like API, you should have programmed in C. In Java we can allocate a known number of elements in an array. Upon allocation, each element is null. We initialize each either in a loop, or, if your batch is small, by direct index addressing. But there are easier ways. You may want to consider using the ArrayList generic class. That collection class does expose add and remove methods. More on reddit.com
🌐 r/learnjava
8
4
April 12, 2019
How can I insert an element into a specific position of an array?
Guys please. Read the rules. Do not ask for or reply with solutions or keys to solutions, rather comment explanations and guides. Comments with solutions will be removed and commenters will automatically be banned for a week. This thread looks like a graveyard in mod view. Stop posting full solutions for this problem. More on reddit.com
🌐 r/javahelp
12
5
November 10, 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
Adding Items to an array in Java - Stack Overflow
I wanted to create an array that has a length specified by the user, and also wanted to have it filled by a loop command, and then it should be copied to another array by another loop command, so I... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
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 ...
🌐
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)
This will need to do until Java catches up. Kind regards, Glyn ... No, there is no need to do that. I used to read the same file twice with a Scanner for that very reason, but you can get a Stream to do the counting for you. You end up with something like this:-In line 7 you have a Stream processing every line in the file; toArray() in line 8 needs to know the size for the array. That method counts the Stream's elements and uses XYZ::new where the count sets the length for a new array.
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-list-add-addall-methods
How To Use add() and addAll() Methods for Java List | DigitalOcean
Learn how to use Java List add() and addAll() methods with examples. Understand syntax, performance, and best practices for managing collections.
🌐
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 - In other words, adding n elements to an ArrayList requires O(n) time. An array can contain primitive as well as non-primitive data types, depending on the definition of the array. However, an ArrayList can only contain non-primitive data types. When we insert elements with primitive data types into an ArrayList, the Java compiler automatically converts the primitive data type into its corresponding object wrapper class.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
This Java program demonstrates how to pass an array to a method. 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
🌐
Codemia
codemia.io › home › knowledge hub › java arraylist how to add elements at the beginning
Java ArrayList how to add elements at the beginning | Codemia
January 27, 2025 - Standard Java arrays are of a fixed length, which means that once a Java array is created, it cannot grow or shrink, which makes the ArrayList very useful when dealing with data whose quantity is unknown at runtime. One common task that may be somewhat less straightforward with an ArrayList compared to a traditional array is inserting elements at the beginning, or at any arbitrary index. This is because an ArrayList is designed to provide fast access to its elements and to add elements primarily at the end.
🌐
Reddit
reddit.com › r/javahelp › how can i insert an element into a specific position of an array?
r/javahelp on Reddit: How can I insert an element into a specific position of an array?
November 10, 2020 -

Hello,

I just started programming Java and am wondering how to insert an element into any given position of an array. I want the array to become {1, 2, 3, 6, 4, 5} from {1, 2, 3, 4, 5}. As a beginner, I don't understand this very well and would appreciate feedback.

import java.util.Arrays;

public class InsertArrays {
    public static int[] insertArray(int x, int array[], int z) {

        int newArray[] = new int[x + 1];
        for (int i = 0; i < x - 2; i++)
        newArray[i] = array[i];
        newArray[x] = z;
        return newArray;

        }

        public static void main(String[] args) {
            int x = 5;
            int array[] = {1, 2, 3, 4, 5};
            System.out.println("Original Array: " + Arrays.toString(array));

            int z = 6;
            int newArray[] = insertArray(x, array, z);
            System.out.println("New Array: "+ Arrays.toString(newArray));

        }
    }

This is what I have so far. The problem with this is that the compiler adds the value 6 to the end of the array, not in front of the 4 like I want.

🌐
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
🌐
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.

🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
🌐
W3Schools
w3schools.com › java › ref_arraylist_add.asp
Java ArrayList add() Method
import java.util.ArrayList; public ... cars.add("Ford"); cars.add("Mazda"); System.out.println(cars); } } ... The add() method adds an item to the list....
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
July 21, 2026 - NullPointerException - if the specified array is null ... Returns the element at the specified position in this list. ... Replaces the element at the specified position in this list with the specified element. ... Appends the specified element to the end of this list. ... Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
🌐
Tutorjoes
tutorjoes.in › java_programming_tutorial › Insert_element_array_in_java
Insert an element (specific position) into an array in Java
After shifting the elements, the program sets the element at the specified index to the new value. Finally, the program prints the updated array using · Arrays.toString(a) and stores the result in a string. import java.util.Arrays; public class Insert_element_array { public static void main(String args[]) { //Program to insert a element in specific index of an array int[] a = {10,20,30,40,50,60,70,80,90,100}; int index = 2; int value = 55; System.out.println("Before Insert "+Arrays.toString(a) ); for(int i=a.length-1;i>index;i--) { a[i]=a[i-1]; } a[index]=value; System.out.println("After Insert "+Arrays.toString(a) ); } }
🌐
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 ...