Add Element to Array in Java

In this post, we will see how to add an element to the array in Java. Adding an element to the array means inserting an element at the end of the array. Also see:- How to Insert element to array at a specific position in Java.

We can solve this problem in two ways,
a) By creating a new array.
b) By taking the help of ArrayList.

Add Element to Array in Java by creating a new array

In this approach,
a) Create a new array of n+1 size, where n is the size of the original array where an element should be added.
b) Copy all elements of the original array to the new array.
c) Insert element to the end.
d) Return the new array, i.e. now the array variable which was pointing to the original array will point to the new array.

Let us demonstrate it through an example. Java program to add an element to array in Java by creating a new array,

import java.util.Arrays;
public class ArrayTest {

   // method to add element to array and return new array
   public static int[] addElement(int[] arr, int element) {

      // create new array of size n+1
      int temp[] = new int[arr.length+1];
      
      // copy all existing element 
      for (int i = 0; i < arr.length; i++) {
         temp[i] = arr[i];
      }
      
      // insert last element
      temp[arr.length] = element;
      
      // return new array
      return temp;
   }

   public static void main(String[] args) {
      // original array
      int arr[] = { 30, 50, 20, 40, 10};
      
      // new element to be added
      int element = 99;
      
      // display old array
      System.out.println("Original array: " + Arrays.toString(arr));
      
      // add element 
      arr = addElement(arr, element);
      
      // display new array
      System.out.println("New array: " + Arrays.toString(arr));
   }
}

Output:-

Original array: [30, 50, 20, 40, 10]
New array: [30, 50, 20, 40, 10, 99]

In this method, to copy the original array to a new array we have used normal for loop, but we can also use the System.arraycopy() method which is a native method and uses a direct memory copy outside of Java land. The native method is implemented in platform-dependent code, typically written in another programming language such as C, C++, FORTRAN, or assembly language. The arraycopy() is likely the fastest way to copy an array, and it gives better performance compared to normal array copy using loops. Learn more about it:- System.arraycopy() method in Java

The same method by using System.arraycopy() method can be written as,

// method to add element to array and return new array
public static int[] addElement(int[] arr, int element) {
    // create new array of size n+1
    int temp[] = new int[arr.length+1];
        
    // copy all existing element 
    System.arraycopy(arr, 0, temp, 0, arr.length);
        
    // insert last element
    temp[arr.length] = element;
        
    // return new array
    return temp;
}

With the help of ArrayList

In this method, we will use ArrayList as a helper. Here the operations will be performed as,

a) Create a new ArrayList with the original array, with the help of Array.asList() method.
b) Add new element to list using add() method.
c) Convert the list to an array using the toArray() method.

Note:- In this approach, the array should be of wrapper type (Integer, Double, Float, and e.t.c.), not of primitive types.

Let us demonstrate it through an example,

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayTest {
   // method to add element to array and return new array
   public static Integer[] addElement(Integer[] arr, int element) {
      
      // create ArrayList with original array
      List<Integer> list = 
            new ArrayList<Integer>(Arrays.asList(arr));
      
      // add new element to arrayList
      list.add(element);
      
      // return array (after converting list to array)
      return list.toArray(arr);
   }

   public static void main(String[] args) {
      // original array
      Integer arr[] = { 30, 50, 20, 40, 10};
      
      // new element to be added
      int element = 99;
      
      // display old array
      System.out.println("Original array: " + Arrays.toString(arr));
      
      // add element 
      arr = addElement(arr, element);
      
      // display new array
      System.out.println("New array: " + Arrays.toString(arr));
   }
}

Output:-

Original array: [30, 50, 20, 40, 10]
New array: [30, 50, 20, 40, 10, 99]

In these examples, to display the array we have used the Arrays.toString() method. The Arrays.toString() method returns a string representation of the contents of the specified array. The string representation consists of a list of the array’s elements, enclosed in square brackets “[]” and the adjacent elements are separated by the characters “, ” (a comma followed by a space). It Returns “null” if the passed array is null. Learn more:- Arrays.toString() method in Java

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “Add Element to Array in Java”

Leave a Comment

Your email address will not be published. Required fields are marked *