# How to concatenate two arrays in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 1, 2019. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-concatenate-arrays

> A short tutorial to learn how to concatenate multiple arrays into one using 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:

```java
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:

```bash
[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:

```java
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:

```bash
[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:

```java
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:

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

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

```java
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:

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

## Using Apache Commons Lang

If you are already using the [Apache Commons Lang](https://commons.apache.org/proper/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. 

```java
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:

```java
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:

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

For a [Gradle](https://attacomsian.com/blog/install-gradle-on-ubuntu) project, add the following dependency to your `build.gradle` file:

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