Java Code Example: join two arrays

Join two Arrays using arraycopy function of system class

This code demonstrates the use of System.arraycopy() method to join two arrays into a single array. The method System.arraycopy() is used to copy elements from one array to another array. The method takes five parameters: the source array, the source starting position, the destination array, the destination starting position, and the number of elements to be copied.

In this code, two arrays firstArray and secondArray are joined into a single array joinedArray. The elements of firstArray are copied to joinedArray using System.arraycopy() method, starting from index 0 to index firstArray.length - 1. Similarly, the elements of secondArray are copied to joinedArray starting from index firstArray.length to index firstArray.length + secondArray.length - 1. Finally, the contents of all three arrays are printed to the console using the Arrays.toString() method.

import java.util.Arrays;

class Cube {
    public static void main(String[] args) {
        int[] firstArray = { 1, 2, 3, 4, 5 };
        int[] secondArray = { 6, 7, 8, 9, 10 };
        int[] joinedArray = new int[firstArray.length + secondArray.length];

        /* Copies an array from the specified source array, beginning at the specified position, 
        to the specified position of the destination array. A subsequence of array components 
        are copied from the source array referenced by src to the destination array referenced by dest. 
        The number of components copied is equal to the length argument. The components at positions srcPos 
        through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, 
        respectively, of the destination array. */
        System.arraycopy(firstArray, 0, joinedArray, 0, firstArray.length);
        System.arraycopy(secondArray, 0, joinedArray, firstArray.length, secondArray.length);

        System.out.println("First array: ");
        System.out.println(Arrays.toString(firstArray));
        System.out.println("Second array: ");
        System.out.println(Arrays.toString(secondArray));
        System.out.println("Joined array: ");
        System.out.println(Arrays.toString(joinedArray));
    }
}
Output
First array: 
[1, 2, 3, 4, 5]
Second array: 
[6, 7, 8, 9, 10]
Joined array: 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Join two Arrays using streams

In the main method, two arrays are defined: firstArray and secondArray. Then, the concat method from the Stream class is used to combine the elements of these two arrays into a single stream, and this stream is then converted into an array using the toArray method. The resulting array is assigned to the joinedArray variable. Finally, the toString method from the Arrays class is used to display the contents of joinedArray as a string representation of an array.

import java.util.stream.*;
import java.util.Arrays;

class Cube {
    public static void main(String[] args) {
        String[] firstArray = { "John", "Mike", "Sarah" };
        String[] secondArray = { "Lion", "Ari" };

        String[] joinedArray = Stream
                .concat(Arrays.stream(firstArray), Arrays.stream(secondArray))
                .toArray(String[]::new);

        System.out.println(Arrays.toString(joinedArray));
    }
}
Output
[John, Mike, Sarah, Lion, Ari]