Created
February 5, 2017 11:42
-
-
Save pantos27/59c5f748b1c79f9cd3cf985300c2766d to your computer and use it in GitHub Desktop.
Join arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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