Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Join arrays
static <T> T[] joinArrays(T[] first,T[] second){
T[] joined = (T[]) Array.newInstance(second.getClass(),first.length+second.length);
for (int i = 0; i < first.length; i++) {
joined[i] = first[i];
}
for (int i = 0; i < second.length; i++) {
joined[i+first.length] = second[i];
}
return joined;
}
//join multiple arrays
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment