You cannot resize an array in java.

Once the size of array is declared, it remains fixed.

Instead you can use ArrayList that has dynamic size, meaning you don't need to worry about its size. If your array list is not big enough to accommodate new values then it will be resized automatically.

ArrayList<String> ar = new ArrayList<String>();
String s1 ="Test1";
String s2 ="Test2";
String s3 ="Test3";
ar.add(s1);
ar.add(s2);
ar.add(s3);

String s4 ="Test4";
ar.add(s4);
Answer from Abubakkar 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
Discussions

java - How to dynamically add elements to String array? - Stack Overflow
I want to add dynamic number of elements to a string array from inside a for loop. How to create an string array of an undefined length? More on stackoverflow.com
🌐 stackoverflow.com
Adding a value of a string to a string array ?
Thiago H is having issues with: How can I add an additional value to a array, getting the data from a string? I know how to modify/update a data in a array like so:... More on teamtreehouse.com
🌐 teamtreehouse.com
1
March 7, 2014
how to add elements into java string array? - Oracle Forums
For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you More on forums.oracle.com
🌐 forums.oracle.com
November 13, 2007
java - How can I add new item to the String array? - Stack Overflow
Possible Duplicate: how to add new elements to a String[] array? How can I add new item to the String array ? I am trying to add item to the initially empty String. Example : String a []; ... More on stackoverflow.com
🌐 stackoverflow.com
January 25, 2013
🌐
W3Docs
w3docs.com › java
add string to String array | W3Docs
Alternatively, you can use the ArrayList class to add a string to an array. ArrayList provides methods for adding and removing elements, so you can use it to create a dynamic collection that can grow and shrink as needed.
🌐
Grails
grails.asia › how-to-add-element-to-string-array-in-java
How To Add element to String Array in Java - Grails Cookbook
July 29, 2017 - public static String[] add(String[] originalArray, String newItem) { int currentSize = originalArray.length; int newSize = currentSize + 1; String[] tempArray = new String[ newSize ]; for (int i=0; i < currentSize; i++) { tempArray[i] = originalArray [i]; } tempArray[newSize- 1] = newItem; ...
🌐
Team Treehouse
teamtreehouse.com › community › adding-a-value-of-a-string-to-a-string-array
Adding a value of a string to a string array ? (Example) | Treehouse Community
March 7, 2014 - This is how to add an element into array list · ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add(testAddToArray); This will add your data to the array list. Hope this helps · Posting to the forum is only allowed for members ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string array- tutorial with code examples
Java String Array- Tutorial With Code Examples
April 1, 2025 - In this section, let’s see the various methods to add an element to the string array. In this method, you create an array having a larger size. For Example, if you have to store 5 elements in the array, then you will create an array with size 10. This way you can easily add new elements at the end of the array. An example that implements the addition of an element to the Pre-allocated array is shown below. import java.util.*; public class Main { public static void main(String[] args) { String[] colorsArray = new String[5]; // initial array values colorsArray[0] = "Red"; colorsArray[1] = "Gre
Find elsewhere
🌐
Oracle
forums.oracle.com › ords › apexds › post › how-to-add-elements-into-java-string-array-1044
how to add elements into java string array? - Oracle Forums
November 13, 2007 - I open a file and want to put the contents in a string array. I tried as below. String[] names; s = new Scanner(new BufferedReader(new FileReader("outfile.txt"))); while (s.hasNext()) { ...
🌐
Silicon Cloud
silicloud.com › home › add elements to java string array
Add Elements to Java String Array - Blog - Silicon Cloud
August 5, 2025 - Place the element to be added at the last position of the new array. Assign the new array to the original array. String[] originalArray = {"元素1", "元素2"}; String[] newArray = new String[originalArray.length + 1]; System.arraycopy(originalArray, 0, newArray, 0, originalArray.length); ...
🌐
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 ...
🌐
Javatpoint
javatpoint.com › add-elements-to-array-in-java
Add elements to Array in Java - Javatpoint
Add elements to Array in Java - Add elements to Array in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
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.
Top answer
1 of 10
120

You already have built-in method for that: -

List<String> species = Arrays.asList(speciesArr);

NOTE: - You should use List<String> species not ArrayList<String> species.

Arrays.asList returns a different ArrayList -> java.util.Arrays.ArrayList which cannot be typecasted to java.util.ArrayList.

Then you would have to use addAll method, which is not so good. So just use List<String>

NOTE: - The list returned by Arrays.asList is a fixed size list. If you want to add something to the list, you would need to create another list, and use addAll to add elements to it. So, then you would better go with the 2nd way as below: -

    String[] arr = new String[1];
    arr[0] = "rohit";
    List<String> newList = Arrays.asList(arr);

    // Will throw `UnsupportedOperationException
    // newList.add("jain"); // Can't do this.

    ArrayList<String> updatableList = new ArrayList<String>();

    updatableList.addAll(newList); 

    updatableList.add("jain"); // OK this is fine. 

    System.out.println(newList);       // Prints [rohit]
    System.out.println(updatableList); //Prints [rohit, jain]
2 of 10
18

I prefer this,

List<String> temp = Arrays.asList(speciesArr);
species.addAll(temp);

The reason is Arrays.asList() method will create a fixed sized List. So if you directly store it into species then you will not be able to add any more element, still its not read-only. You can surely edit your items. So take it into temporary list.

Alternative for this is,

Collections.addAll(species, speciesArr);

In this case, you can add, edit, remove your items.

🌐
Baeldung
baeldung.com › home › java › java array › how to add string arrays to arraylist in java
How to Add String Arrays to ArrayList in Java | Baeldung
March 7, 2025 - In this article, we’ve explored various ways to add elements from multiple String arrays to an ArrayList in Java. We can use the Arrays.asList() method or Collections.addAll() to solve the problem.
🌐
Scaler
scaler.com › home › topics › string array in java
String Array in Java - Scaler Topics
April 28, 2024 - We can add the elements in a string array in three ways: pre-allocation of the array in which we set the size of the array bigger to add the values afterwards, the second is the creation of a new array, and the other one is to use of the ArrayList ...
🌐
Javatpoint
javatpoint.com › string-array-in-java
String Array in Java - javatpoint
String Array in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
CodeGym
codegym.cc › java blog › java arrays › string array in java
String Array in Java
May 11, 2023 - If you’ve got an array of strings and need to add a new string to the end of your array, use the Arrays.copyOf method. This method creates a new array with one extra element, and then adds the new element to the end of the new array.
🌐
Hero Vired
herovired.com › learning-hub › blogs › string-array-in-java
String Array in Java: How to Declare, Example, Initializing
In Java, an array's index starts at 0, so to access the first element of an array, we would use myArray\$\$$0$, and for the second myArray\$\$$1$, etc. For example: "`Java String myElement = myArray$1$; // Retrieves "two" from the array ``` Can I add or remove elements from a string array dynamically?
🌐
TutorialKart
tutorialkart.com › java › java-array › java-append-to-array
Java - Append to Array
November 23, 2020 - import java.util.ArrayList; public class Example { public static void main(String[] args) { String arr[] = {"apple", "banana", "cherry"}; String element = "mango"; ArrayList<String> arrayList = new ArrayList<String>(); for(String s: arr) arrayList.add(s); arrayList.add(element); String arrNew[] = new String[arr.length + 1]; for(int i = 0; i < arrNew.length; i++) arrNew[i] = arrayList.get(i); //print new array for(String s: arrNew) System.out.println(s); } }