How to concatenate two arrays in Java

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 me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.