【问题标题】:Java how to append two or more arrays in to new array like in PythonJava如何将两个或多个数组附加到新数组中,就像在Python中一样
【发布时间】:2026-02-23 21:10:02
【问题描述】:

我写这个问题是因为我似乎找不到任何适合我需要的答案。

我想要实现的目标:我想创建新数组,例如array3 在 java 中将包含来自不同数组的两个或多个数组,例如array3 = [[array1], [array2], [arrayN]].

在 Python 中,我知道如何将 2 个列表附加到第 3 个列表,例如:

list1 = [1, 2, 3]
list2 = [11, 22, 33]
list3 = []

for i in range(len(list1)):
    list3.append([list1[i], list2[i]])

print(list3)

结果将是:[[1, 11], [2, 22], [3, 33]]

而且我找不到正确的答案如何在 Java 中执行此操作。有没有办法在 Java 中实现这个目标?

添加了我之前做过的事情:

        String[] list1 = integer1.split(";");
        String[] list2 = integer2.split(";");
        String[] list3 = integer3.split(";");
        String[] list4 = integer4.split(";");

        int lenOfList1 = list1.length;
        int lenOfList2 = list2.length;
        int lenOfList3 = list3.length;
        int lenOfList4 = list4.length;

        int result1 = Integer.compare(lenOfList1, lenOfList2);
        int result2 = Integer.compare(lenOfList3, lenOfList4);
        int result3 = Integer.compare(result1, result2);

        ArrayList<String> ar = new ArrayList<String>();

        if (result3 == 0) {
            System.out.println("This is result: " + result3 + ", and we are good to go now!");
            for(int i=0; i < lenOfList1; i++){
                ar.add(list1[i]);
                ar.add(list2[i]);
                ar.add(list3[i]);
                ar.add(list4[i]);
            }
        } else {
            System.out.println("This is result: " + result3 + ", and this is the end!");
        }

【问题讨论】:

  • 你有Java代码要展示吗?你开始了吗?
  • 我只从不同的数组中实现了这个[1,2,3,4]。
  • 不妨看看this
  • @rolandas simkus 你的意思是数组[数组]??
  • @RolandasŠimkus 两个数组将始终包含相同的大小,或者它们可能不匹配?

标签: java arrays


【解决方案1】:

我会简单地创建一种方法来重新组合二维列表中的列表。

使用简单的可变参数方法,例如:

 public static <T> List<List<T>> groupArrays(T[]... arrays){

这样,您可以将任意数量的数组传递给该方法。实现看起来像

public static <T> List<List<T>> groupArrays(T[]... arrays){
    if(arrays.length == 0){
        throw new IllegalArgumentException("No arrays to concat");
    }

    //Find the longuest array to know how many inner list to create
    int maxLength = arrays[0].length;
    for(int i = 1; i < arrays.length; ++i){
        maxLength = Math.max(maxLength, arrays[1].length);
    }

    //creating those now reduce the complexity in the next loop.
    List<List<T>> lists = new ArrayList<>();
    for(int i = 0; i < maxLength; ++i){
        lists.add(new ArrayList<>());
    }

    for(T[] array : arrays){
        for(int i = 0; i < array.length; ++i){
            lists.get(i).add(array[i]);
        }
    }

    return lists;
}

然后,就这样称呼它:

Integer[] a1 = {1, 2};
Integer[] a2 = {11, 22, 33, 44};
Integer[] a3 = {111, 222, 333};

List<List<Integer>> result = groupArrays(a1, a2, a3);
System.out.println(result);

[[1, 11, 111], [2, 22, 222], [33, 333], [44]]

【讨论】:

    【解决方案2】:

    您可以尝试使用List&lt;List&lt;dataType&gt;&gt;

    为您服务:

    List<Integer> list1 = new ArrayList<>();
    list1.add(1);
    list1.add(2);
    list1.add(3);
    
    List<Integer> list2 = new ArrayList<>();
    list2.add(11);
    list2.add(22);
    list2.add(33);
    
    List<List<Integer>> list3 = new ArrayList<>();
    
    for (int i = 0; i <list1.size(); i++) {
    
        List<Integer> tempList = new ArrayList<>();
        tempList.add(list1.get(i));
        tempList.add(list2.get(i));
        list3.add(tempList);
    }
    System.out.println(list3);
    

    输出:

    [[1, 11], [2, 22], [3, 33]] 
    

    【讨论】:

    • 谢谢,这就是我要找的。谢谢!