Pure Java API example, supports both primitive and generic types.
JoinArray.java
package com.mkyong.example.array;
import java.lang.reflect.Array;
import java.util.Arrays;
public class JoinArray {
public static void main(String[] args) {
String[] s1 = new String[]{"a", "b", "c"};
String[] s2 = new String[]{"d", "e", "f"};
String[] s3 = new String[]{"g", "h", "i"};
String[] result = joinArrayGeneric(s1, s2, s3);
System.out.println(Arrays.toString(result));
int[] int1 = new int[]{1, 2, 3};
int[] int2 = new int[]{4, 5, 6};
int[] int3 = new int[]{7, 8, 9};
int[] result2 = joinArray(int1, int2, int3);
System.out.println(Arrays.toString(result2));
}
static <T> T[] joinArrayGeneric(T[]... arrays) {
int length = 0;
for (T[] array : arrays) {
length += array.length;
}
//T[] result = new T[length];
final T[] result = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), length);
int offset = 0;
for (T[] array : arrays) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
return result;
}
static int[] joinArray(int[]... arrays) {
int length = 0;
for (int[] array : arrays) {
length += array.length;
}
final int[] result = new int[length];
int offset = 0;
for (int[] array : arrays) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
return result;
}
//create other overloaded primitive type - long, double...
//static long[] joinArray(long[]... arrays)
}
Output
[a, b, c, d, e, f, g, h, i] [1, 2, 3, 4, 5, 6, 7, 8, 9]
0 Comments
If you have any doubts,please let me know