Java Array Append
In Java, an array is a collection of fixed size. You cannot increase or decrease its size. But, you can always create a new one with specific size.
To append element(s) to array in Java, create a new array with required size, which is more than the original array. Now, add the original array elements and element(s) you would like to append to this new array.
Also, you can take help of Arrays class or ArrayList to append element(s) to array. But, these techniques require conversion from array to ArrayList, and vice versa.
In this tutorial, we will go through different approaches with the examples, to append one or more elements to the given array.
Append Element to Array
In this example, we will add an element to array.
We shall implement the following steps.
- Take original array
arr
. (We shall initialize an array with some elements.) - Take the
element
you would to append to arrayarr
. - Create new array
arrNew
with size greater than that ofarr
by one. - Assign elements of
arr
andelement
toarrNew
. We will use for loop to assign elements ofarr
toarrNew
.
Java Program
public class Example {
public static void main(String[] args) {
String arr[] = {"apple", "banana", "cherry"};
String element = "mango";
String arrNew[] = new String[arr.length + 1];
int i;
for(i = 0; i < arr.length; i++) {
arrNew[i] = arr[i];
}
arrNew[i] = element;
//print new array
for(String s: arrNew) {
System.out.println(s);
}
}
}
Output
apple
banana
cherry
mango
Append Array to Array
In this example, we will add an array to another array and create a new array.
We shall implement the following steps.
- Take two input arrays arr1 and arr2.
- Create new array arrNew with size equal to sum of lengths of arr1 and arr2.
- Assign elements of arr1 and arr2 to arrNew. Use for loop to assign elements of input arrays to new array.
Java Program
public class Example {
public static void main(String[] args) {
String arr1[] = {"apple", "banana", "cherry"};
String arr2[] = {"mango", "grape"};
String arrNew[] = new String[arr1.length + arr2.length];
int i;
for(i = 0; i < arr1.length; i++) {
arrNew[i] = arr1[i];
}
for(i = 0; i < arr2.length; i++) {
arrNew[arr1.length + i] = arr2[i];
}
//print new array
for(String s: arrNew) {
System.out.println(s);
}
}
}
Output
apple
banana
cherry
mango
grape
Append Element to Array using ArrayList
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.
Java Program
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);
}
}
Output
apple
banana
cherry
mango
Append Element to Array using java.util.Arrays
In this example, we will use java.util.Arrays class to append an element to array.
We shall implement the following steps.
- Take input array arr1.
- Create a new array using java.util.Arrays with the contents of arr1 and new size.
- Assign the element to the new array.
Java Program
import java.util.Arrays;
public class Example {
public static void main(String[] args) {
String arr[] = {"apple", "banana", "cherry"};
String element = "mango";
String arrNew[] = Arrays.copyOf(arr, arr.length + 1);
arrNew[arr.length]= element;
//print new array
for(String s: arrNew)
System.out.println(s);
}
}
Output
apple
banana
cherry
mango
Conclusion
In this Java Tutorial, we have learned how to append element(s) to array using different techniques with the help of example programs.