I have a situation where I need to concatenate two two-dimensional arrays.
Object[][] getMergedResults() {
Object[][] a1 = getDataFromSource1();
Object[][] a2 = getDataFromSource2();
// I can guarantee that the second dimension of a1 and a2 are the same
// as I have some control over the two getDataFromSourceX() methods
// concat the two arrays
List<Object[]> result = new ArrayList<Object[]>();
for(Object[] entry: a1) {
result.add(entry);
}
for(Object[] entry: a2) {
result.add(entry);
}
Object[][] resultType = {};
return result.toArray(resultType);
}
You could try
Object[][] result = new Object[a1.length + a2.length][];
System.arraycopy(a1, 0, result, 0, a1.length);
System.arraycopy(a2, 0, result, a1.length, a2.length);