【问题标题】:How to create a new List from merging 3 ArrayLists in round robin style?如何以循环方式合并 3 个 ArrayLists 创建一个新列表?
【发布时间】:2021-04-18 02:48:20
【问题描述】:

我有 3 个数组。我想合并 3 个数组并创建一个显示所有合并整数的新数组。我希望这个新数组采用循环方式。

示例输入:

array = {arr1 = 1, 2, 3, arr2 = 4, 5, 6, arr3 = 7, 8, 9}

示例输出:

arr4 = 1, 4, 7, 2, 5, 8, 3, 6, 9

我应该使用此功能,但我不明白如何使用listOfLists

String roundRobin(List<List<Integer>>listOfLists) {

【问题讨论】:

    标签: java arrays list arraylist round-robin


    【解决方案1】:

    您可以更简单地使用 ArrayLists 或 Arrays。

    使用 数组,您可以创建 3 个 int[] 和 1 个 int[][]。 这转化为“3 个ints 数组和1 个ints 数组数组”。这就是#4:你的其他数组的数组。在代码中是:

    int[]
        arr1 = {1, 2, 3},
        arr2 = {4, 5, 6},
        arr3 = {7, 8, 9};
    int[][] arr4 = {arr1, arr2, arr3};
    

    或者,您可以使用 ArrayLists,它与数组的不同之处在于您可以添加或删除元素以及许多其他操作。在这种情况下,逻辑是相同的,只是语法不同。您创建了 3 个ArrayList&lt;Integer&gt; 和 1 个ArrayList&lt;ArrayList&lt;Integer&gt;&gt;,转换为“Integers 的 3 个ArrayLists 和 ArrayLists 的ArrayLists 的 1 个ArrayList。”在代码中是:

    ArrayList<Integer>
        list1 = new ArrayList<>(Arrays.asList(1, 2, 3)),
        list2 = new ArrayList<>(Arrays.asList(4, 5, 6)),
        list3 = new ArrayList<>(Arrays.asList(7, 8, 9));
    
    List<ArrayList<Integer>> list4 = new ArrayList<>
            (Arrays.asList(list1, list2, list3));
    

    最后,您可以打印两种方法的输出:

    System.out.println("Arrays - int[][]: " + Arrays.deepToString(arr4)
            + "\nLists - List<ArrayList<Integer>>: " + list4);
    

    你会得到:

    Arrays - int[][]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    Lists - List<List<Integer>>: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    

    这有帮助吗?

    【讨论】:

      【解决方案2】:

      您可以使用两个嵌套的 for 循环:

      List<List<Integer>> lists = List.of(
              List.of(1, 2),
              List.of(3, 4, 5, 6),
              List.of(7, 8, 9));
      
      List<Integer> listRobin = new ArrayList<>();
      
      // infinite loop through the columns
      for (int i = 0; ; i++) {
          // if the maximum number
          // of columns is reached
          boolean max = true;
          // loop through the rows, aka inner lists
          for (List<Integer> row : lists) {
              // if this column is present
              if (i < row.size()) {
                  // column is present
                  max = false;
                  // take value from the column
                  listRobin.add(row.get(i));
              }
          }
          // while the columns are still present
          if (max) break;
      }
      
      // output
      System.out.println(listRobin);
      // [1, 3, 7, 2, 4, 8, 5, 9, 6]
      

      另见:Filling a jagged 2d array first by columns

      【讨论】:

        【解决方案3】:

        我认为循环输入并获取元素并将元素添加到数组列表中很容易理解。

        public static List<Integer> roundRobin(List<List<Integer>> listOfLists) {
            if (listOfLists == null || listOfLists.isEmpty()) {
                return new ArrayList<>();
            }
        
            int maxLength = -1;
            for (List<Integer> list : listOfLists) {
                if (list.size() > maxLength) {
                    maxLength = list.size();
                }
            }
        
            List<Integer> result = new ArrayList<>(maxLength * listOfLists.size());
            for (int i = 0; i < maxLength; i++) {
                for (List<Integer> list : listOfLists) {
                    if (i < list.size()) {
                        result.add(list.get(i));
                    }
                }
            }
        
            return result;
        
        }
        

        【讨论】:

          【解决方案4】:

          要从二维列表的列填充一维列表,您可以使用两个嵌套流:首先按列,然后按行。内部列表的长度无关紧要,在外部流中,您可以列仍然存在时遍历。

          List<List<Integer>> listOfLists = List.of(
                  List.of(1, 2, 3, 4),
                  List.of(5, 6),
                  List.of(7, 8, 9));
          
          List<Integer> listRobin = IntStream
                  // infinite Stream through
                  // the columns of a 2d list
                  .iterate(0, i -> i + 1)
                  // take the values from the column
                  // Stream<List<Integer>>
                  .mapToObj(i -> listOfLists
                          // iterate over the inner lists
                          .stream()
                          // take those lists where
                          // this column is present
                          .filter(list -> list.size() > i)
                          // take value from the column
                          .map(list -> list.get(i))
                          // return a new list
                          .collect(Collectors.toList()))
                  // while the columns are still present
                  .takeWhile(list -> list.size() > 0)
                  // flatten to a single stream
                  // Stream<Integer>
                  .flatMap(List::stream)
                  // return a new list
                  .collect(Collectors.toList());
          
          // output
          System.out.print(listRobin); // [1, 5, 7, 2, 6, 8, 3, 9, 4]
          

          另见:Efficient way to choose data from several Lists with round robin algorithm

          【讨论】:

            猜你喜欢
            • 2019-08-12
            • 1970-01-01
            • 2017-07-26
            • 2021-08-11
            • 2022-07-12
            • 1970-01-01
            • 1970-01-01
            • 2014-11-17
            • 1970-01-01
            相关资源
            最近更新 更多