In this short article, you will learn about different ways to concatenate two arrays into one in Java.

Using A Loop

The simplest way to concatenate two arrays in Java is by using the for loop:

int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};

// create a new array
int[] result = new int[arr1.length + arr2.length];

// add elements to new array
int index = 0;
for (int elem : arr1) {
    result[index] = elem;
    index++;
}

for (int elem : arr2) {
    result[index] = elem;
    index++;
}

System.out.println(Arrays.toString(result));

The above code snippet will output the following on the console:

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

Using System.arraycopy() Method

Another way of concatenating two arrays in Java is by using the System.arraycopy() method (works for both primitive and generic types), as shown below:

int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};

// create a new array
int[] result = new int[arr1.length + arr2.length];

// concatenate arrays
System.arraycopy(arr1, 0, result, 0, arr1.length);
System.arraycopy(arr2, 0, result, arr1.length, arr2.length);

System.out.println(Arrays.toString(result));

Here is the output of the above code:

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

Using Java 8 Stream

If you can using Java 8 or higher, it is also possible to use the Stream API to merge two arrays into one. Here is an example:

String[] arr1 = {"a", "b", "c", "d"};
String[] arr2 = {"e", "f", "g", "h"};

// concatenate arrays
String[] result = Stream.of(arr1, arr2).flatMap(Stream::of).toArray(String[]::new);

System.out.println(Arrays.toString(result));

Here is the output:

[a, b, c, d, e, f, g, h]

Streams can also be used for non-primitive arrays like int[]:

int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};

// concatenate arrays
int[] result = IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray();

System.out.println(Arrays.toString(result));

Here is the output of the above code:

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

Using Apache Commons Lang

If you are already using the Apache Commons Lang library, just use the ArrayUtils. addAll() method to concatenate two arrays into one. This method works for both primitive as well as generic type arrays.

String[] arr1 = {"a", "b", "c", "d"};
String[] arr2 = {"e", "f", "g", "h"};

// concatenate arrays
String[] result = ArrayUtils.addAll(arr1, arr2);

System.out.println(Arrays.toString(result));

// [a, b, c, d, e, f, g, h]

Let us have another example of primitive arrays:

int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};

// concatenate arrays
int[] result = ArrayUtils.addAll(arr1, arr2);

System.out.println(Arrays.toString(result));

// [1, 2, 3, 4, 5, 6, 7, 8]

Don't forget to include the following dependency to your pom.xml file:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.9</version>
</dependency>

For a Gradle project, add the following dependency to your build.gradle file:

implementation 'org.apache.commons:commons-lang3:3.9'

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.