I wrote a data model, into one class I need to merge 3 arrays. These arrays has a fixed size (of course) and initialized to null. I use this code :
public static <T> T[] merge(T[]... arrays) {
int size = 0;
for (T[] array : arrays) {
size += array.length;
}
T[] merged = (T[]) Array.newInstance(arrays[0][0].getClass(), size);
int start = 0;
for (T[] array : arrays) {
System.arraycopy(array, 0,
merged, start, array.length);
start += array.length;
}
return (T[]) merged;
}
This line is not correct :
T[] merged = (T[]) Array.newInstance(arrays[0][0].getClass(), size);
I need to precise which class I use, but I get a null pointer !
I would leverage the Collections library
Test program: