Grails Cookbook - A collection of tutorials and examples

How To Add element to String Array in Java

Working with arrays in Java is common because this data structure is simple to use and very efficient. Most programming language supports arrays, so many developers may have idea on how to use it without reading much documentation. Specially for programmers coming from C or C++ background, the way on how to use it is very similar. But there is one big limitation when working with Arrays in Java, the length or size of Arrays are fixed. Meaning we can not increase or decrease the size without programming tricks. Below are some examples on how we can add element to a String Array in Java.

Add Element to a String Array by Recreating the Data Structure

There is actually no other way to add elements to an existing array without recreating the array. For example, if we have an array of 10 String, we can not add 11th String to it. Arrays in general do not support resizing. Here is a sample code on how we can add one more item by actually recreating the instance of the array.
String[] myArray = {"Car", "Van", "Bike" };    
String newItem = "SUV";
int currentSize = myArray.length;
int newSize = currentSize + 1;
String[] tempArray = new String[ newSize ];
for (int i=0; i < currentSize; i++)
{
    tempArray[i] = myArray [i];
}
tempArray[newSize- 1] = newItem;
myArray = tempArray;   
for (String element:myArray ) {
    System.out.println( element );
}
What we did above is create a new array tempArray, and copy the contents from the original array into it. Then we add the new value to the last index of the new array. The output will be:
Car
Van
Bike
SUV
We can take out the code above and refactor it to make a function. This will make our solution reusable. Here is the sample code:
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;
    return tempArray;   
}
public static void main( String[] args )
{
    String[] thisIsAStringArray = {"Car", "Van", "Bike"};
    thisIsAStringArray = add(thisIsAStringArray, "SUV");
    for (String element:thisIsAStringArray) {
        System.out.println( element );
    }
}
This will have the same output as the previous example.

Add Element to a String Array by Pre-Allocation

Another approach we can take if we do not wish to recreate our array every time we need to put a new element, is to actually create a big array and maintain the number of items on a separate counter. Here is a simple example given below:
String[] myArray = new String[10];
// Set initial contents
myArray[0] = "Car";
myArray[1] = "Van";
myArray[2] = "Bike";
int numberOfItems = 3;
// try to add new value to the end
String newItem = "SUV";
myArray[numberOfItems++] = newItem;
for (int i=0; i<numberOfItems; i++ ) {
    System.out.println( myArray[i] );
}