Java add to array

In this post, we will see how to add elements to the array.


Using Apache’s common lang library

You can use varargs add method to add elements to array dynamically.

Here are the few add overloaded methods provided by ArrayUtils class

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

ArrayUtils's add methods

If you want to add more than one element, you can use ArrayUtils’s addAll methods.

ArrayUtils's addAll methods

Here is quick example using ArrayUtil’s add method

When you run above program, you will get below output.

1 2 3 4 5

By writing your own utility method

As Array is fixed in length, there is no direct method to add elements to the array, you can write your own utility method.
We have used varargs here to support n numbers of elements that can be added to array.

When you run above program, you will get below output:

1 2 3 4 5

As you can see, we are using object[] here. If you want to write a method specific to any data type, you can write that as well.
If you have frequent scenarios to add elements to array dynamically, I would recommend to use varargs instead of Array.

That’s all about Java add to array.

Related Posts

  • Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers
    07 April

    Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers

    In this article, we look at a problem : Given an Array of Positive Integers, Find the Maximum Difference between Two Adjacent Numbers. For each pair of elements we need to compute their difference and find the Maximum value of all the differences in array. Let us look at an example, Consider this array: 💻 […]

  • 11 November

    How to print array in java

    Learn about how to print array in java in multiple ways.

  • 09 October

    Initialize 2D array in Java

    In this article, we will learn to initialize 2D array in Java. The array is a data structure that is used to collect a similar type of data into contiguous memory space. An array can be a single-dimensional or multidimensional. An array that has 2 dimensions is called 2D or two-dimensional array. There are several […]

  • 10 November

    Java Convert Array to List

    In this post, we will see how to convert array to list in java. There are may ways to convert array to list: 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ By using Arrays.asList(): This is an easy method […]

  • 10 November

    Java convert List to Array

    In this post, we will see how to convert List to Array in java. There are many ways to convert List to Array in java Using toArray() method We can use toArray() method to convert List to String. We can either pass a array of size as List’s length or we can pass empty array. […]

  • 22 September

    Java program to Remove element from array

    In this post, we will see how to remove an element from array in java. Unlike Arraylist,Java Arrays class does not provide any direct method to add or delete element. As Array is fixed size in nature, you can not shrink or grow it dynamically. You need to create new array and copy all elements […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.