You can concatenate two arrays in Java using the following methods:
Method 1: Using Arrays. copy Of and System. array copy
import java.util.Arrays;
Method 2: Using Java 8 Streams
import java.util.Arrays;
import java.util.stream.IntStream;
Public class Main {public static void main (String [] args) {int[] array1 = {1, 2, 3}
int[] array2 = {4, 5, 6}; int[] result = IntStream.concat (Arrays.stream(array1), Arrays.stream(array2)).toArray();System.out.println (Arrays.toString(result))
Method 3: Using Apache Commons Lang
If you’re using Apache Commons Lang, you can use the ArrayUtils.addAll method: import org.apache.commons.lang3.ArrayUtils;
Public class Main {public static void main(String[] args) {int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};int[] result = ArrayUtils.addAll(array1, array2); System.out.println(Arrays.toString(result));
These methods will all produce the same result.