Skip to content

Instantly share code, notes, and snippets.

@pantos27
Created February 5, 2017 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pantos27/59c5f748b1c79f9cd3cf985300c2766d to your computer and use it in GitHub Desktop.
Save pantos27/59c5f748b1c79f9cd3cf985300c2766d to your computer and use it in GitHub Desktop.
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