How to Add an Element to an Array in Java? Last Updated : 22 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array. In this article, we will discuss the NewDifferent Ways to Add an Element to an ArrayThere are two different approaches we can use to add an element to an Array. The approaches are listed below:Adding an Element Using a new ArrayUsing ArrayList as Intermediate Storage1. Adding an Element Using a New ArrayThe first approach is that we can create a new array whose size is one element larger than the old size.Approach: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 to this array.Add the new element in the n+1th position.Print the new array.Example: This example demonstrates how to add an element to an array by creating a new array. Java // Java Program to add an element // into a new array import java.io.*; import java.lang.*; import java.util.*; class Geeks { // Function to add x in arr public static int[] addX(int n, int arr[], int x) { 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 (int i = 0; i < n; i++) newarr[i] = arr[i]; newarr[n] = x; return newarr; } public static void main(String[] args) { int n = 5; int arr[] = { 10, 20, 30, 40, 50}; int x = 70; // call the method to add x in arr arr = addX(n, arr, x); System.out.println(Arrays.toString(arr)); } } Output[10, 20, 30, 40, 50, 70] 2. Using ArrayList as Intermediate StorageThe second approach is we can use ArrayList to add elements to an array because ArrayList handles dynamic resizing automatically. It eliminates the need to manually manage the array size.Approach: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 and return the new array.Example: This example demonstrates how to add element to an array using an ArrayList. Java // Java Program to add an element in an Array // with the help of ArrayList import java.io.*; import java.lang.*; import java.util.*; class Geeks { // Function to add x in arr public static Integer[] addX(int n, Integer arr[], int x) { 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 arr; } public static void main(String[] args) { int n = 10; Integer arr[] = { 10, 20, 30, 40, 50}; int x = 70; // call the method to add x in arr arr = addX(n, arr, x); System.out.println(Arrays.toString(arr)); } } Output[10, 20, 30, 40, 50, 70] Comment More infoAdvertise with us Next Article How to Add an Element to an Array in Java? C code_r Follow Improve Article Tags : Misc Java Arrays Java-Array-Programs Practice Tags : ArraysJavaMisc Similar Reads How to Declare an Array in Java? In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, dif 3 min read How to Insert an element at a specific position in an Array in Java An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in Java.Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos. Approach 1:Â Here's how to do it 4 min read How to Add All Items From a Collection to an ArrayList in Java? In Java, to add all items from a collection to an ArrayList we use the addAll() method. This method appends all elements from a specified collection to the end of the list.Approach:Get the Collection whose items are to be added to the ArrayList.Create an ArrayListAdd all the items of Collection into 1 min read ArrayDeque add() Method in Java The Java.util.ArrayDeque.add(Object element) method in Java is used to add a specific element at the end of the Deque. The function is similar to the addLast() method of ArrayDeque in Java. Syntax: Array_Deque.add(Object element) Parameters: The parameter element is of the type ArrayDeque and refers 2 min read ArrayDeque addAll() method in Java The addAll() method of ArrayDeque is used to insert all the elements of the collection passed as parameter at the end of this ArrayDeque. For adding elements of a collection to ArrayDeque we have to iterate through the collection and add each element in ArrayDeque by using addAll(E e) method. This m 4 min read List add(int index, E element) method in Java The add(int index, E ele) method of List interface in Java is used to insert the specified element at the given index in the current list. Implementation:Java// Java code to illustrate add(int index, E elements) import java.util.*; public class ArrayListDemo { public static void main(String[] args) 2 min read Insert Element at the End of an Array Given an array of integers, the task is to insert an element at the end of the array.Examples:Input: arr[] = [10, 20, 30, 40], ele = 50Output: [10, 20, 30, 40, 50]Input: arr[] = [], ele = 20Output: [20]Table of Content[Approach 1] Using Built-In Methods[Approach 2] Using Custom Method[Approach 1] Us 5 min read Conversion of Array To ArrayList in Java Following methods can be used for converting Array To ArrayList: Method 1: Using Arrays.asList() method Syntax: public static List asList(T... a) // Returns a fixed-size List as of size of given array. // Element Type of List is of same as type of array element type. // It returns an List containing 5 min read Insert Element at a Given Position in an Array Given an array of integers, the task is to insert an element at a given position in the array.Examples:Input: arr[] = [10, 20, 30, 40], pos = 2, ele = 50Output: [10, 50, 20, 30, 40]Input: arr[] = [], pos = 1, ele = 20Output: [20]Input: arr[] = [10, 20, 30, 40], pos = 5, ele = 50Output: [10, 20, 30, 7 min read How to Initialize an Array in Java? An array in Java is a linear data structure that is used to store multiple values of the same data type. In an array, each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java 5 min read Like