How to merge two arrays in Java? Finding an efficient way to concatenate
Hello everyone! I'm a beginner in Java (studying for about a month) and I've encountered a task that seems simple, but I can't find an elegant solution.
I need to merge two integer arrays into one. For example, I have arrays `int[] a = {1, 2, 3}` and `int[] b = {4, 5, 6}`. I want to get a third array `c = {1, 2, 3, 4, 5, 6}`.
I wrote this code:
```java
int[] a = {1, 2, 3};
int[] b = {4, 5, 6};
int[] c = new int[a.length + b.length];
for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for (int i = 0; i < b.length; i++) {
c[a.length + i] = b[i];
}
```
It works, but looks cumbersome. I feel like Java should have something built-in for such a common operation. I tried using `Arrays.concat()` or `c = a + b`, but the compiler complains.
Please advise