Using Java 1.8, I created the following implementation to concat
two int arrays:
public static int[] concat(int[] array1, int[] array2) {
return IntStream.concat(Arrays.stream(array1), Arrays.stream(array2)).toArray();
}
The unit test I created to test this looks like this:
int[] numbers1 = new int[] {1, 4, 3, 10, 2, 11, 15, 8};
int[] numbers2 = new int[] {5, 6, 7, 9, 12, 13, 14};
@Test
public void concat() {
int[] concatNumbers = ArrayUtils.concat(numbers1, numbers2);
for (int i = 0; i < numbers1.length; i++) {
assert(concatNumbers[i] == numbers1[i]);
}
for (int i = numbers2.length + 1; i < numbers2.length; i++) {
assert(concatNumbers[i] - 1 == numbers1[i]);
}
}
Questions:
- Although my unit test works correctly, is there a better way to unit test this method (e.g. in terms of style, cleaner code, etc.)?
How could I change the actual implementation if I were to use varargs?
public static int[] concat(int[] ... args)