Concatenate Two Arrays in Java

Exercise:

Write a Java Program to concatenate two arrays.

1.Concatenate using arraycopy

Click Here to View the Solution!
import java.util.Arrays;
 
public class ConcateArrays {
 
    public static void main(String[] args) {
        int[] array1 = {1,2,3};
        int[] array2 = {4, 5, 6,7,8,9};
 
        int aLen = array1.length;
        int bLen = array2.length;
        int[] result = new int[aLen + bLen];
 
        System.arraycopy(array1, 0, result, 0, aLen);
        System.arraycopy(array2, 0, result, aLen, bLen);
        System.out.println(Arrays.toString(result));
    }
} 
Click Here to View the Output!
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Click Here to View the Explanation!
  • This program is used to concatenate two arrays by using a Java function arraycopy().
  • Initially, two arrays array1 and array2 are defined in the program. Each is given 3 integer elements.
  • For the two arrays to combine, first the program will find out the length of each array using the length method and store in variables aLen and bLen.
  • Secondly, both the lengths are added and stored in a single variable result as follows: result = aLen + bLen.
  • The parameters assigned to the arraycopy method for array1 as arraycopy(array1, 0 , result, 0, aLen) intimates the program to copy all the elements of the array1 starting from index 0 and place them into the result, starting from index 0 till aLen.
  • In a similar way, the parameters assigned to the arraycopy method for array2 as arraycopy(array2, 0 , result, aLen, bLen) intimates the program to copy all the elements of the array2 starting from index 0 and place them into the result, starting from index aLen till bLen.
  • Finally, the result printed as a single array will be [1, 2, 3, 4, 5, 6,7,8,9].

2.Concatenate without using arraycopy

Click Here to View the Solution!
import java.util.Arrays;
public class ConcateArrays {
 
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3,4,5};
        int[] array2 = {6,7,8};
        int length = array1.length + array2.length;
        int[] result = new int[length];
        int pos = 0;

        for (int element : array1) {
            result[pos] = element;
            pos++;
        }
        for (int element : array2) {
            result[pos] = element;
            pos++;
        }
       System.out.println(Arrays.toString(result));
    }
} 
Click Here to View the Output!
[1, 2, 3, 4, 5, 6, 7, 8]
Click Here to View the Explanation!
  • This program is used to concatenate two arrays by manually copying the elements to a single array via for-each loop.
  • A new integer variable length will hold the length of each array by adding array1.length and array2.length and storing the sum in length.
  • After the summing the lengths, a new array result is created of the length.
  • Two for-each loops are used in the program. A position ‘pos’ integer variable will help the program to iterate between the two arrays.
  • The first for-each loop for(int element : array1) will iterate between the elements of the array1 and copy its elements into the result array. The value of pos will increase after each iteration and the loop will continue until the elements end in the array1.
  • Similarly, the elements of the array2 will be copied to the result array starting from the position where the array1 elements end.
  • Finally, the result array will be printed using the .toString method.
%d bloggers like this: