I am looking for a method which will merge two 2D arrays into one 2D array.
Arrays may store any types of data.
For example I have:
Object[][] dataProvider = new Object[][]{
{12, 6.0}, {6, 3.0}
};
Object[][] impl = new Object[][]{
{1}, {2}, {3}
};
As a result should be array below:
Object[][] result = new Object[][]{
{1, 12, 6.0}
{1, 6, 3.0}
{2, 12, 6.0}
{2, 6, 3.0}
{3, 12, 6.0}
{3, 6, 3.0}
};
here is my code:
public Object[][] merge(Object[][] impl, Object[][] dataProvider) {
int n = dataProvider.length;
int m = impl.length;
Object[][] merged = new Object[m * n][];
int countLines = 0;
for (Object[] objects : impl) {
for (Object[] value : dataProvider) {
int g = objects.length + value.length;
List<Object> list = new ArrayList<>();
for (int j = 0; j < objects.length; j++) {
list.add(j, impl[j]);
}
for (int k = 0; k < value.length; k++) {
list.add(list.size(), value);
}
merged[countLines] = list.toArray();
countLines++;
list.clear();
}
}
return merged;
}
The output for merge[0] is [1], [12, 6.0], [12, 6.0] The output should be [1, 12, 6.0]
Could you please help me to find a mistake in the method? Thanks ahead!