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 Arrayimportjava.io.*;importjava.lang.*;importjava.util.*;classGFG {// Function to add x in arrpublicstaticint[] addX(intn,intarr[],intx){inti;// create a new array of size n+1intnewarr[] =newint[n +1];// insert the elements from// the old array into the new array// insert all elements till n// then insert x at n+1for(i =0; i < n; i++)newarr[i] = arr[i];newarr[n] = x;returnnewarr;}// Driver codepublicstaticvoidmain(String[] args){intn =10;inti;// initial array of size 10intarr[]= {1,2,3,4,5,6,7,8,9,10};// print the original arraySystem.out.println("Initial Array:\n"+ Arrays.toString(arr));// element to be addedintx =50;// call the method to add x in arrarr = addX(n, arr, x);// print the updated arraySystem.out.println("\nArray with "+ x+" added:\n"+ Arrays.toString(arr));}}chevron_rightfilter_noneOutput: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() method
- Convert the list to an array using toArray() method
// Java Program to add an element in an Arrayimportjava.io.*;importjava.lang.*;importjava.util.*;classGFG {// Function to add x in arrpublicstaticInteger[] addX(intn, Integer arr[],intx){inti;// create a new ArrayListList<Integer> arrlist=newArrayList<Integer>(Arrays.asList(arr));// Add the new elementarrlist.add(x);// Convert the Arraylist to arrayarr = arrlist.toArray(arr);// return the arrayreturnarr;}// Driver codepublicstaticvoidmain(String[] args){intn =10;inti;// initial array of size 10Integer arr[]= {1,2,3,4,5,6,7,8,9,10};// print the original arraySystem.out.println("Initial Array:\n"+ Arrays.toString(arr));// element to be addedintx =50;// call the method to add x in arrarr = addX(n, arr, x);// print the updated arraySystem.out.println("\nArray with "+ x+" added:\n"+ Arrays.toString(arr));}}chevron_rightfilter_noneOutput: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]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to add element at first and last position of linked list in Java?
- List add(int index, E element) method in Java
- AbstractList add(int index, E element) method in Java with Examples
- Java.util.ArrayList.add() Method in Java
- DelayQueue add() method in Java with Examples
- ArrayBlockingQueue add() method in Java
- LinkedList add() Method in Java
- How to add TextSwitcher with animation in Android using Java
- HashSet add() Method in Java
- TreeSet add() Method in Java
- PriorityQueue add() Method in Java
- ArrayDeque add() Method in Java
- LinkedBlockingDeque add() method in Java
- GregorianCalendar add() Method in Java
- Vector add() Method in Java
- Stream.Builder add() method in Java
- IntStream.Builder add() method in Java
- Java AbstractSequentialList | add()
- DoubleStream.Builder add(double t) in Java with Examples
- LongStream.Builder add(long t) in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

