How to Add an Element to an Array in Java? Last Updated : 16 Oct, 2025 Comments Improve Suggest changes 34 Likes 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. There are two different approaches we can use to add an element to an Array. The approaches are listed below:1. 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.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. 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.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. 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 C code_r Follow 34 Improve C code_r Follow 34 Improve Article Tags : Misc Java Arrays Java-Array-Programs Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like