VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-add-an-element-to-an-array-in-java/

⇱ How to Add an Element to an Array in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Add an Element to an Array in Java?

Last Updated : 16 Oct, 2025

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 Array

The 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.

Output
[10, 20, 30, 40, 50, 70]


2. Using ArrayList as Intermediate Storage

The 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.

Output
[10, 20, 30, 40, 50, 70]
Comment
Article Tags: