Java Array add elements

Filed Under: Java

How to add elements to an array in java? We know that java array size is fixed, so we can’t add elements to an Array. We have to provide size of the array when we initialize array in java.

Java Array add elements

java array add elements, java add to array

There is no shortcut method to add elements to an array in java. But as a programmer, we can write one. Here I am providing a utility method that we can use to add elements to an array. We can also use it for java copy arrays.

In the utility method, I will create a temporary array, whose size will be the addition of the length of array and number of elements to add in the array. Then I will copy the input array to the temporary array and add the elements and then return it.

Let’s see this in action.

package com.journaldev.util;

import java.util.Arrays;

public class AddToArray {

    public static void main(String[] args) {
        Object[] objArr1 = {"1","2","3"};
        Object[] objArr2 = {"4","5","6"};
        //adding an element to array
        Object[] objArr = add(objArr1, "4");
        //adding two arrays
        objArr = add(objArr1, objArr2);
     * This method will add elements to an array and return the resulting array
     * @param arr
     * @param elements
     * @return
    public static Object[] add(Object[] arr, Object... elements){
        Object[] tempArr = new Object[arr.length+elements.length];
        System.arraycopy(arr, 0, tempArr, 0, arr.length);
        for(int i=0; i < elements.length; i++)
            tempArr[arr.length+i] = elements[i];
        return tempArr;

I am using variable arguments in add() so that we can pass any number of objects to be added to the array. Note that the array type should be Object else it will throw ClassCastException.
Also, this will work only for Object array and not for primitive data types array.

Output of the above program is:

[1, 2, 3, 4]
[1, 2, 3, 4, 5, 6]

This is just an alternative way to add Objects to an array in java but we should use ArrayList in this scenario where the number of elements can change.

You can checkout more Array examples from our GitHub Repository.

Reference: Arrays


  1. Anusha A.G says:

    Hi Sir,

    I need solution for below question.
    Q)Find all the divisors of a given integer number and return in an array.

    ->By using collection i can able get solution,but in need in simple array usage.
    ->Need solution in such a way that,return array from method/class and to display elements in tester(main) class.

  2. Shashank says:

    AFAIK Its not the alternative way to copy an array but its most effective way because system.arraycopy(arg..) is native method and use memcpy internally , which loads whole memory chunks in one time instead of one by one in case of for loop and that makes it efficient , If you have any thought in this please let me know.

    A Reader

  3. Kanwaljit says:

    For copying the second arry over, why did you not use the System.arraycopy again ?

    System.arraycopy(arr, 0, tempArr, 0, arr.length); //for first copy (like you have done already)
    System.arraycopy(elements, 0 , tempArr, arr.length +1, elements.length); // for second copy

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content