How to add an element to an Array in Java?

13 May, 2022
Given an array of size n, the task is to add an element x in this array in Java.
The size of the array cannot be changed dynamically in Java, as it is done in C/C++. Hence in order to add an element in the array, one of the following methods can be done:
By creating a new array:Create a new array of size n+1, where n is the size of the original array.Add the n elements of the original array in this array.Add the new element in the n+1 th position.Print the new array.Below is the implementation of the above approach:// Java Program to add an element in an Array  import java.io.*;import java.lang.*;import java.util.*;  class GFG {      // Function to add x in arr    public static int[] addX(int n, int arr[], int x)    {        int i;          // create a new array of size n+1        int newarr[] = new int[n + 1];          // insert the elements from        // the old array into the new array        // insert all elements till n        // then insert x at n+1        for (i = 0; i < n; i++)            newarr[i] = arr[i];          newarr[n] = x;          return newarr;    }      // Driver code    public static void main(String[] args)    {          int n = 10;        int i;          // initial array of size 10        int arr[]            = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };          // print the original array        System.out.println("Initial Array:\n"                           + Arrays.toString(arr));          // element to be added        int x = 50;          // call the method to add x in arr        arr = addX(n, arr, x);          // print the updated array        System.out.println("\nArray with " + x                           + " added:\n"                           + Arrays.toString(arr));    }}Output:Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array with 50 added:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]
By using ArrayList as intermediate storage:Create an ArrayList with the original array, using asList() method.Simply add the required element in the list using add() methodConvert the list to an array using toArray() method// Java Program to add an element in an Array  import java.io.*;import java.lang.*;import java.util.*;  class GFG {      // Function to add x in arr    public static Integer[] addX(int n, Integer arr[], int x)    {        int i;          // create a new ArrayList        List<Integer> arrlist            = new ArrayList<Integer>(                Arrays.asList(arr));          // Add the new element        arrlist.add(x);          // Convert the Arraylist to array        arr = arrlist.toArray(arr);          // return the array        return arr;    }      // Driver code    public static void main(String[] args)    {          int n = 10;        int i;          // initial array of size 10        Integer arr[]            = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };          // print the original array        System.out.println("Initial Array:\n"                           + Arrays.toString(arr));          // element to be added        int x = 50;          // call the method to add x in arr        arr = addX(n, arr, x);          // print the updated array        System.out.println("\nArray with " + x                           + " added:\n"                           + Arrays.toString(arr));    }}Output:Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array with 50 added:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]

By creating a new array:Create a new array of size n+1, where n is the size of the original array.Add the n elements of the original array in this array.Add the new element in the n+1 th position.Print the new array.Below is the implementation of the above approach:// Java Program to add an element in an Array  import java.io.*;import java.lang.*;import java.util.*;  class GFG {      // Function to add x in arr    public static int[] addX(int n, int arr[], int x)    {        int i;          // create a new array of size n+1        int newarr[] = new int[n + 1];          // insert the elements from        // the old array into the new array        // insert all elements till n        // then insert x at n+1        for (i = 0; i < n; i++)            newarr[i] = arr[i];          newarr[n] = x;          return newarr;    }      // Driver code    public static void main(String[] args)    {          int n = 10;        int i;          // initial array of size 10        int arr[]            = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };          // print the original array        System.out.println("Initial Array:\n"                           + Arrays.toString(arr));          // element to be added        int x = 50;          // call the method to add x in arr        arr = addX(n, arr, x);          // print the updated array        System.out.println("\nArray with " + x                           + " added:\n"                           + Arrays.toString(arr));    }}Output:Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array with 50 added:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]

Create a new array of size n+1, where n is the size of the original array.
Add the n elements of the original array in this array.
Add the new element in the n+1 th position.
Print the new array.
Below is the implementation of the above approach:
// Java Program to add an element in an Array  import java.io.*;import java.lang.*;import java.util.*;  class GFG {      // Function to add x in arr    public static int[] addX(int n, int arr[], int x)    {        int i;          // create a new array of size n+1        int newarr[] = new int[n + 1];          // insert the elements from        // the old array into the new array        // insert all elements till n        // then insert x at n+1        for (i = 0; i < n; i++)            newarr[i] = arr[i];          newarr[n] = x;          return newarr;    }      // Driver code    public static void main(String[] args)    {          int n = 10;        int i;          // initial array of size 10        int arr[]            = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };          // print the original array        System.out.println("Initial Array:\n"                           + Arrays.toString(arr));          // element to be added        int x = 50;          // call the method to add x in arr        arr = addX(n, arr, x);          // print the updated array        System.out.println("\nArray with " + x                           + " added:\n"                           + Arrays.toString(arr));    }}
Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array with 50 added:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]

By using ArrayList as intermediate storage:Create an ArrayList with the original array, using asList() method.Simply add the required element in the list using add() methodConvert the list to an array using toArray() method// Java Program to add an element in an Array  import java.io.*;import java.lang.*;import java.util.*;  class GFG {      // Function to add x in arr    public static Integer[] addX(int n, Integer arr[], int x)    {        int i;          // create a new ArrayList        List<Integer> arrlist            = new ArrayList<Integer>(                Arrays.asList(arr));          // Add the new element        arrlist.add(x);          // Convert the Arraylist to array        arr = arrlist.toArray(arr);          // return the array        return arr;    }      // Driver code    public static void main(String[] args)    {          int n = 10;        int i;          // initial array of size 10        Integer arr[]            = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };          // print the original array        System.out.println("Initial Array:\n"                           + Arrays.toString(arr));          // element to be added        int x = 50;          // call the method to add x in arr        arr = addX(n, arr, x);          // print the updated array        System.out.println("\nArray with " + x                           + " added:\n"                           + Arrays.toString(arr));    }}Output:Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array with 50 added:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]

Create an ArrayList with the original array, using asList() method.
Simply add the required element in the list using add() method
Convert the list to an array using toArray() method
// Java Program to add an element in an Array  import java.io.*;import java.lang.*;import java.util.*;  class GFG {      // Function to add x in arr    public static Integer[] addX(int n, Integer arr[], int x)    {        int i;          // create a new ArrayList        List<Integer> arrlist            = new ArrayList<Integer>(                Arrays.asList(arr));          // Add the new element        arrlist.add(x);          // Convert the Arraylist to array        arr = arrlist.toArray(arr);          // return the array        return arr;    }      // Driver code    public static void main(String[] args)    {          int n = 10;        int i;          // initial array of size 10        Integer arr[]            = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };          // print the original array        System.out.println("Initial Array:\n"                           + Arrays.toString(arr));          // element to be added        int x = 50;          // call the method to add x in arr        arr = addX(n, arr, x);          // print the updated array        System.out.println("\nArray with " + x                           + " added:\n"                           + Arrays.toString(arr));    }}
Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array with 50 added:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]

Arrays
Java-Array-Programs
Java
Arrays
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Multidimensional Arrays in Java
Collections in Java
Stream In Java
Set in Java
Initializing a List in Java