# How to concatenate two arrays ?

Java's array concatenation is always as simple as in PHP with array_merge method. In Java we need to use System.arraycopy() or org.apache.commons.lang.ArrayUtils.addAll() method. There are the code samples with both of them:

```public class ArrayMerge {

private int [] firstArray = new int[] {0, 5, 10};
private int [] secondArray = new int[] {11, 15, 16};
private int [] expectedArray = new int[] {0, 5, 10, 11, 15, 16};

@Test
public void testArrayCopy() {
int[] sourceArray = new int[firstArray.length+secondArray.length];
System.arraycopy(firstArray, 0, sourceArray, 0, firstArray.length);
System.arraycopy(secondArray, 0, sourceArray, firstArray.length, secondArray.length);
compare(sourceArray);
}

@Test
public void testApacheCommons() {
}

private void compare(int[] sourceArray) {
for (int i = 0; i < sourceArray.length; i++) {
assertTrue("Generated ("+sourceArray[i]+") value should be the same as expected ("+expectedArray[i]+")", sourceArray[i]==expectedArray[i]);
}
}

}
```

The test pass well. In the reality, they are no the difference between both of them because Apache's addAll method uses internally System.arraycopy calls as well:

``` /**
* <p>Adds all the elements of the given arrays into a new array.</p>
* <p>The new array contains all of the element of {@code array1} followed
* by all of the elements {@code array2}. When an array is returned, it is always
* a new array.</p>
*
* <pre>
* ArrayUtils.addAll(array1, null) = cloned copy of array1
* ArrayUtils.addAll(null, array2) = cloned copy of array2
* </pre>
*
* @param array1 the first array whose elements are added to the new array.
* @param array2 the second array whose elements are added to the new array.
* @return The new int[] array.
* @since 2.1
*/
public static int[] addAll(final int[] array1, final int... array2) {
if (array1 == null) {
return clone(array2);
} else if (array2 == null) {
return clone(array1);
}
final int[] joinedArray = new int[array1.length + array2.length];
System.arraycopy(array1, 0, joinedArray, 0, array1.length);
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
return joinedArray;
}
```

System.arraycopy method parameters are, respectively:
- array to copy
- the first position to copy
- source array
- source array first position receiving the copied value
- the quantity of values to copy from one array to another
{"ft_lang_label":"__label__en","ft_lang_prob":0.54859746,"math_prob":0.7896197,"size":2424,"snap":"2020-34-2020-40","text_gpt3_token_len":612,"char_repetition_ratio":0.16652893,"word_repetition_ratio":0.04011461,"special_character_ratio":0.2858911,"punctuation_ratio":0.21123596,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9515597,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-20T17:00:46Z\",\"WARC-Record-ID\":\"<urn:uuid:27678ee4-f533-4e33-9b97-c0045e08009c>\",\"Content-Length\":\"14074\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3774349e-71c5-46ba-9f46-8ca862b4610c>\",\"WARC-Concurrent-To\":\"<urn:uuid:b430367f-d864-4764-82a1-c978200ad9b8>\",\"WARC-IP-Address\":\"213.186.33.18\",\"WARC-Target-URI\":\"https://www.waitingforcode.com/tips/java/how-to-concatenate-two-arrays\",\"WARC-Payload-Digest\":\"sha1:PGKOC527A2MUJXUZMPTEJJHPFDF3RYDT\",\"WARC-Block-Digest\":\"sha1:2CVVLRZCMXDGLSVEJYXYHOCQ7FMZ53V4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400198287.23_warc_CC-MAIN-20200920161009-20200920191009-00422.warc.gz\"}"}