3

I have this little script that gets information from an excel file. After I've collected the information that i need i want to combine these two arrays into one. Is that possible?

public Object[][] createData1() throws Exception {
    Object[][] retObjArr1 = data.getTableArray("C:\\Users\\OAH\\Workspaces\\Chrome2\\Testdata2.xls", "Sheet1", "normalCustomer");
    Object[][] retObjArr2 = data.getTableArray("C:\\Users\\OAH\\Workspaces\\Chrome2\\Testdata2.xls", "Sheet2", "langLogin");
    return(retObjArrCombined); //I want to return one array with both arrays
}
Oleaha
  • 130
  • 1
  • 2
  • 10
  • Do you allow repeated elements in the union of arrays? – alepuzio Feb 01 '13 at 16:23
  • 1
    See this question: http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java – BlairHippo Feb 01 '13 at 16:24
  • 1
    Unless nobody ever thought about merging two arrays before you, I'm quite sure that googling `java merge arrays` will give you the answer you're looking for. – sp00m Feb 01 '13 at 16:25

6 Answers6

4

You can use the System.arraycopy method (yes, all lowercase). Javadoc. You can do more stuff with arrays using the java.util.Arrays class.

djechlin
  • 59,258
  • 35
  • 162
  • 290
systemboot
  • 860
  • 2
  • 8
  • 22
2

How about this?

Link to Another Stack Question!!!!!!

    public static int[][] append(int[][] a, int[][] b) {
    int[][] result = new int[a.length + b.length][];
    System.arraycopy(a, 0, result, 0, a.length);
    System.arraycopy(b, 0, result, a.length, b.length);
    return result;
}
Community
  • 1
  • 1
Michael W
  • 3,515
  • 8
  • 39
  • 62
1

Here is simple solution:

private static Object[] concatenate(Object[] a, Object[] b) {
    Collection<Object> result = new ArrayList<Object>(a.length + b.length);
    for (Object val : a) {
        result.add(val);
    }
    for (Object val : b) {
        result.add(val);            
    }
    return result.toArray();
} 
Dmitry Trifonov
  • 1,079
  • 11
  • 13
0

Another way:

Object[][] one = {{1, 2, 3}, {4, 5}, {6}};
Object[][] two = {{7, 8}, {9}};
List<Object[]> holder = new ArrayList<>();

Collections.addAll(holder, one);
Collections.addAll(holder, two);

Object[][] result = holder.toArray(new Object[holder.size()][]);
isvforall
  • 8,768
  • 6
  • 35
  • 50
0

The following code achieves your task :

Object[][] retObjArr1 = { { "a00", "a01" }, { "a10", "a11" },
        { "a20", "a21" } };
Object[][] retObjArr2 = { { "b00", "b01" }, { "b10", "b11" },
        { "b20", "b21" } };

List<Object[][]> list = new ArrayList<Object[][]>();
list.add(retObjArr1);
list.add(retObjArr2);

int totalRow = 0;
for (int all = 0; all < list.size(); all++) {
    totalRow += list.get(all).length;
}
Object[][] retObjArrCombined = new Object[totalRow][];
int rowCount = 0;
for (int all = 0; all < list.size(); all++) {
    Object[][] objects = list.get(all);
    for (int i = 0; i < objects.length; i++) {
        retObjArrCombined[rowCount] = objects[i];
        rowCount++;
    }
}
for (int i = 0; i < retObjArrCombined.length; i++) {
    for (int j = 0; j < retObjArrCombined[i].length; j++) {
        System.out.println("value at :(" + i + "," + j + ") is:"
                + retObjArrCombined[i][j]);
    }
}

In this code, the Object[][] retObjArrCombined contains all arrays copied from retObjArr1, retObjArr2 etc... And it prints the following output :

value at :(0,0) is:a00
value at :(0,1) is:a01
value at :(1,0) is:a10
value at :(1,1) is:a11
value at :(2,0) is:a20
value at :(2,1) is:a21
value at :(3,0) is:b00
value at :(3,1) is:b01
value at :(4,0) is:b10
value at :(4,1) is:b11
value at :(5,0) is:b20
value at :(5,1) is:b21
Visruth
  • 3,430
  • 35
  • 48
0

one-liner with streams without computation pull ups
and yes it will be a few ms slower

Object[][] combi = Stream.concat( Arrays.stream( retObjArr1 ), Arrays.stream( retObjArr2 ) ).toArray( Object[][]::new );

Kaplan
  • 2,572
  • 13
  • 14