如何在 Java 中组合两个对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14650563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:58:06  来源:igfitidea点击:

How to combine two object arrays in Java

javaarraystestng

提问by Oleaha

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?

我有这个从 excel 文件中获取信息的小脚本。在收集了我需要的信息后,我想将这两个数组合并为一个。那可能吗?

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
}

回答by systemboot

You can use the System.arraycopymethod (yes, all lowercase). Javadoc. You can do more stuff with arrays using the java.util.Arraysclass.

您可以使用该System.arraycopy方法(是的,全部小写)。文档。您可以使用java.util.Arrays该类对数组执行更多操作。

回答by Michael W

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;
}

回答by Dmitry Trifonov

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();
} 

回答by Kaplan

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 );

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

回答by isvforall

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()][]);

回答by Visruth

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[][] retObjArrCombinedcontains all arrays copied from retObjArr1, retObjArr2etc... And it prints the following output :

在这段代码中,Object[][] retObjArrCombined包含了从复制的所有阵列retObjArr1retObjArr2等...它输出以下:

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