How To Append An Array In Java

How To Append An Array In Java | In this blog we will discuss how to add an array to the array or how to add elements to the array in Java. In simple words how to merge two arrays or add elements to the array. Now we will see the different possibilities for achieving our task. See the examples for appending an array in Java:-

Example-1:
Array: [11, 2, 3, 4, 56, 90]
Element to be added:- 78
Array after adding the element:- [11, 2, 3, 4, 56, 90, 78]

Example-2:
Array1: [58, 45, 1, 23, 69, 85, 14, 7]
Array2: [65, 21, 74, 32, 56, 98, 14]
After appending the array2 to array1 at the end:
[58, 45, 1, 23, 69, 85, 14, 7, 65, 21, 74, 32, 56, 98, 14]

How To Append An Element To An Array In Java

To append just one element to an array we have developed our own custom addElement() method that enables the user to add a single element to the end of the array. Now let us see the demonstration of the addElement() method.

import java.util.Arrays;

public class Main {
   public static int[] addElement(int[] array, int element) {
      int temp[] = new int[array.length + 1];
      for (int i = 0; i < array.length; i++) {
         temp[i] = array[i];
      }
      temp[array.length] = element;
      return temp;
   }

   public static void main(String[] args) {
      int array[] = { 56, 45, 78, 02, 10, 98 };
      int element = 999;
      System.out.println("Original array: " + Arrays.toString(array));
      array = addElement(array, element);
      System.out.println("New array: " + Arrays.toString(array));
   }
}

Output:-

Original array: [56, 45, 78, 2, 10, 98]
New array: [56, 45, 78, 2, 10, 98, 999]

In the addElement() method we have taken a temporary array having length = original array length + 1. Then we copied each element of the original array to the temporary array and inserted the given element at the end of the array. The temporary array is returned back to the caller method.

How To Append To An Array In Java

Now we will see how to append an array to another array. To do this we need to use for loop and iterate over all the elements to copy to the new array. 

To append an array to another existing array, we have to create an array with a size equal to the sum of those arrays. Later we have to copy the first array, and then the second array to the new array. After this process, we will new array containing both array elements.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      int array1[] = { 56, 78, 14, 96, 52, 10 };
      int array2[] = { 0, 4, 8, 9, 3, 5, 1, 25 };
      int array[] = new int[array1.length + array2.length];

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

      for (int i = 0, j = array1.length; 
            j < (array1.length + array2.length); j++, i++) {
         array[j] = array2[i];
      }

      System.out.println("Array1 = " + Arrays.toString(array1));
      System.out.println("Array2 = " + Arrays.toString(array2));
      System.out.println("Merged Array = " + Arrays.toString(array));
   }
}

Output:-

Array1 = [56, 78, 14, 96, 52, 10]
Array2 = [0, 4, 8, 9, 3, 5, 1, 25]
Merged Array = [56, 78, 14, 96, 52, 10, 0, 4, 8, 9, 3, 5, 1, 25]

How To Append A String To An Array In Java

We can append a string either to the String array or Object type array. The below programs show how to append a string to the string array.

import java.util.Arrays;

public class Main {
   public static String[] addElement(String[] array, String element) {
      String temp[] = new String[array.length + 1];
      for (int i = 0; i < array.length; i++) {
         temp[i] = array[i];
      }
      temp[array.length] = element;
      return temp;
   }

   public static void main(String[] args) {
      String array[] = { "Java", "JavaScript", "Python" };
      String string = "Kotlin";
      System.out.println("Original string array: " + Arrays.toString(array));
      array = addElement(array, string);
      System.out.println("New string array: " + Arrays.toString(array));
   }
}

Output:-

Original string array: [Java, JavaScript, Python]
New string array: [Java, JavaScript, Python, Kotlin]

An object array can contain multiple types of values and we can also insert string-type elements. Program to append string to an Object array.

import java.util.Arrays;

public class Main {

   public static Object[] addElement(Object[] array, String element) {
      Object temp[] = new Object[array.length + 1];
      for (int i = 0; i < array.length; i++) {
         temp[i] = array[i];
      }
      temp[array.length] = element;
      return temp;
   }

   public static void main(String[] args) {
      Object array1[] = { 100, "Hello", 'C', 200.52, 50L, true };
      String string1 = "HTML";
      System.out.println("Original object array: " + Arrays.toString(array1));
      array1 = addElement(array1, string1);
      System.out.println("New object array: " + Arrays.toString(array1));
   }
}

Output:-

Original object array: [100, Hello, C, 200.52, 50, true]
New object array: [100, Hello, C, 200.52, 50, true, HTML]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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