简体   繁体   English

合并Java中的两个多维字符串数组

[英]Merge two multidimensional string arrays in Java

I have (at least) two multidimensional String arrays (with UNequal dimensions), that I am trying to put side by side. 我(至少)有两个多维String数组(具有不相等的维数),我试图将它们放在一起。 That is, 那是,

String[][] arrayOne contains the following: String[][] arrayOne包含以下内容:

aaaaa
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa

...and String[][] arrayTwo contains the following: ...和String[][] arrayTwo包含以下内容:

bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb

...and String[][] arrayThree contains the following: ...和String[][] arrayThree包含以下内容:

ccccccc
ccccccc
ccccccc
ccccccc
ccccccc
ccccccc
ccccccc
ccccccc

What I have been unsuccessfully trying to do is create a method which returns the following (preferrably as a multidimensional String array, but as a String works too): 我一直未能成功完成的工作是创建一个返回以下内容的方法(最好是多维String数组,但是String也可以):

String[][] result contains the following: String[][] result包含以下内容:

               ccccccc
               ccccccc
aaaaa          ccccccc
aaaaa          ccccccc
aaaaa          ccccccc
aaaaa bbbbbbbb ccccccc
aaaaa bbbbbbbb ccccccc
aaaaa bbbbbbbb ccccccc
aaaaa bbbbbbbb ccccccc

So basically, the method would put the arrays side-by-side, align them all to the bottom, and combine them into one array. 因此,基本上,该方法将并排放置数组,将它们全部对齐到底部,然后将它们组合成一个数组。

So far, my complex attempts to solve this problem have only created more errors, and trying to debug with println statements and the like has only confused me more. 到目前为止,我为解决该问题所做的复杂尝试仅产生了更多错误,而尝试使用println语句等进行调试只会使我更加困惑。 And I figured someone mush known a fairly easy way to accomplish this task. 而且我认为有人必须知道一种相当容易的方法来完成此任务。

If anyone knows, your help would be greatly appreciated!!! 如果有人知道,将非常感谢您的帮助!!!

* EDIT - * Example #2: * 编辑-*示例2:

aaaa            bbb            ccccc                       ccccc
aaaa   'plus'   bbb   'plus'   ccccc   'yields'   aaaa     ccccc
aaaa                           ccccc              aaaa bbb ccccc
                               ccccc              aaaa bbb ccccc

An (untested) general solution to your problem: 一种(未试用的)通用解决方案:

EDIT: Thanks you @Paul Bellora for testing and confirming. 编辑: 谢谢@Paul Bellora的测试和确认。

public static <T> T[][] mergeArrays(Class<T> clazz, T[][]... arrays) {
    // determine length of 1st dimension.
    int dim1 = 0;
    for (T[][] arr : arrays) {
        dim1 += arr.length;
    }
    // Create new 2Dim Array
    T[][] result = (T[][]) Array.newInstance(clazz, dim1, 0);
    // Fill the new array with all 'old' arrays
    int index = 0;
    for (T[][] arr : arrays) {
        for (T[] array : arr) {
            // changes within your old arrays will reflect to merged one
            result[index++] = array;
        }
    }
    return result;
}

This is how you call it: 这就是你的称呼:

    String[][] one = // your 1st array here;
    String[][] two = // your 2nd array here;
    String[][] three = // your 3rd array here;
    String[][] four = // your 4th array here;
    // This works with an arbitrary amount of arrays.
    String[][] merged1 = mergeArrays(String.class, one, two, three);
    String[][] merged2 = mergeArrays(String.class, one, two, three, four);

As requested, just a String version, still untested: 根据要求,只是一个String版本,尚未经过测试:

public static String[][] mergeStringArrays(String[][]... arrays) {
    // determine length of 1st dimension.
    int dim1 = 0;
    for (String[][] arr : arrays) {
        dim1 += arr.length;
    }
    // Create new 2Dim Array
    String [][] result = new String[dim1][];
    // Fill the new array with all 'old' arrays
    int index = 0;
    for (String[][] arr : arrays) {
        for (String[] array : arr) {
            // changes within your old arrays will reflect to merged one
            result[index++] = array;
        }
    }
    return result;
}

Look at my first answer to see how to call this method. 查看我的第一个答案,看看如何调用此方法。

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

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