Oct 13
The following utility method will allow you to merge any number of arrays.
public static <T> T[] arrayMerge(T[]... arrays)
{
// Determine required size of new array
int count = 0;
for (T[] array : arrays)
{
count += array.length;
}
// create new array of required class
T[] mergedArray = (T[]) Array.newInstance(
arrays[0][0].getClass(),count);
// Merge each array into new array
int start = 0;
for (T[] array : arrays)
{
System.arraycopy(array, 0,
mergedArray, start, array.length);
start += array.length;
}
return (T[]) mergedArray;
}
Leave a Reply
You must be logged in to post a comment.