Java

How to Add Elements to an Array in Java

Array is a group of same data type elements and is considered a fixed-size data structure. In Java, you cannot directly add elements to an array because the location next to the last element of the array is available in memory or not is not known. However, there are some other ways for adding elements to an array.

This blog will explain how to add an element to an array in Java. So let’s get started!

Adding elements to a Java array

In Java, you can add elements to an array:

  • By creating a new array
  • By using ArrayList

Now, let’s check out the stated method one by one.

Method 1: Adding Elements to array by creating a new Java array

To add elements to an array in Java, first create an array then copy the existing array elements in the newly created array. After doing so, you can add new elements to it.

Example
In this example, firstly, we will create an integer array named numArray[ ] with the following values:

int numArray[] = {11,28,13,46,25,46};

In the next step, we will create a new integer type array named newNumArray[ ] with a greater size of the existing array:

int newNumArray[] = new int[numArray.length + 1];

The element 77 is stored in the variable named appendValue, which we want to add:

int appendValue = 77;

For printing the array numArray[ ], use the System.out.println() method:

System.out.println("numArray:" + Arrays.toString(numArray));

Now, copy the elements of array numArray[ ] in a newly created array newNumArray[ ] by using a for loop:

for(int i = 0; i<numArray.length; i++) {  
    newNumArray[i] = numArray[i];  
}

Then, insert the value that is stored in appendValue variable in the newNumArray[ ]:

newNumArray[numArray.length] = appendValue;

Lastly, print the newNumArray[] elements:

System.out.println("newNumArray:" + Arrays.toString(newNumArray));

The given output indicates that 77 is successfully added in the newNumArray[ ]:

Now, let’s check out the other method for adding elements to an array in Java.

Method 2: Adding Elements to an array in Java by using ArrayList

You can also utilize Java ArrayList to add elements to an array. It is considered ideal as ArrayList is a re-sizable array.

Example
First of all, we will create an integer type array named numArray[ ] with the following values:

Integer numArray[] = {11,28,13,46,25,46};

Print array by using the System.out.println() method:

System.out.println("numArray: "+ Arrays.toString(numArray));

Create an ArrayList named newNumArrayList and pass the array in it by using the aslist() method:

ArrayList<Integer> newNumArrayList = new ArrayList<Integer>(Arrays.asList(numArray));

Add the required element in the created ArrayList with the help of the add() method:

newNumArrayList.add(77);

Now, we will convert this ArrayList into an array by using the toArray() method:

numArray = newNumArrayList.toArray(numArray);

Finally, print the array with the appended element:

System.out.println("ArrayList: "+ Arrays.toString(numArray));

Output

We have provided all of the necessary information related to adding elements to an array in Java.

Conclusion

In Java, elements can be added to an array by using Array List or creating a new array. The best and most efficient method is utilizing the ArrayList for the mentioned purpose. To do so, convert the existing array into an ArrayList, add required elements, and then convert it to a normal array. ArrayList also takes less memory space. This blog discussed the methods of adding elements to an array in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.