简体   繁体   English

如何在Java中拼接二维arrays

[英]How to concatenate two-dimensional arrays in Java

I have a situation where I need to concatenate two two-dimensional arrays.我有一种情况需要连接两个二维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);
}

I have looked at the solutions for the concatenation of 1-dimensional arrays in this post but have been unable to make it work for my two-dimensional arrays.我在这篇文章中查看了连接一维 arrays 的解决方案,但无法使其适用于我的二维 arrays。

So far, the solution I have come up with is to iterate over both arrays and adding each member to a ArrayList and then returning toArray() of that array list.到目前为止,我提出的解决方案是遍历 arrays 并将每个成员添加到 ArrayList,然后返回该数组列表的 toArray()。 I'm sure there must be an easier solution, but have so far been unable to come with one.我确信一定有一个更简单的解决方案,但到目前为止还无法提供一个。

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

You could use Apache Commons Library - ArrayUtils . 您可以使用Apache Commons Library - ArrayUtils Change only the index for second dimension and merge the whole lines. 仅更改第二维的索引并合并整行。

Here is the method I use for 2D array concatenation. 这是我用于2D数组连接的方法。 It partly uses Sergio Nakanishi's answer, but adds the ability to concatenate in both directions. 它部分使用了Sergio Nakanishi的答案,但增加了两个方向连接的能力。

/*
 * Define directions for array concatenation
 */
public static final byte ARRAY_CONCAT_HORIZ = 0, ARRAY_CONCAT_VERT = 1;

/*
 * Concatenates 2 2D arrays
 */
public static Object[][] arrayConcat(Object[][] a, Object[][] b, byte concatDirection)
{
    if(concatDirection == ARRAY_CONCAT_HORIZ && a[0].length == b[0].length)
    {
        return Arrays.stream(arrayConcat(a, b)).map(Object[].class::cast)
                    .toArray(Object[][]::new);
    }
    else if(concatDirection == ARRAY_CONCAT_VERT && a.length == b.length)
    {
        Object[][] arr = new Object[a.length][a[0].length + b[0].length];
        for(int i=0; i<a.length; i++)
        {
            arr[i] = arrayConcat(a[i], b[i]);
        }
        return arr;
    }
    else
        throw new RuntimeException("Attempted to concatenate arrays of incompatible sizes.");
}

/*
 * Concatenates 2 1D arrays
 */
public static Object[] arrayConcat(Object[] a, Object[] b)
{
    Object[] arr = new Object[a.length + b.length];
    System.arraycopy(a, 0, arr, 0, a.length);
    System.arraycopy(b, 0, arr, a.length, b.length);
    return arr;
}

Logical Way-逻辑方式-

    for(int j = 0; j < a.length+b.length; j++){
        if(j < a.length){
            ans[j] = a[j];
        }else{
            ans[j] = b[j-(a.length)];
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM