【问题标题】:How to split ArrayList by occurence of something?如何通过发生某些事情来拆分 ArrayList?
【发布时间】:2017-08-28 10:31:13
【问题描述】:

我有以下 ArrayList

List<Integer> l = new ArrayList<Integer>(Arrays.asList(1,2,3,4,1,2,3,1,2,3,4,5,6));

那就是

[1,2,3,4,1,2,3,1,2,3,4,5,6]

现在我想通过下一次出现 1 将列表拆分为子列表 这样输出将是

[[1,2,3,4],[1,2,3],[1,2,3,4,5,6]]

如何做到这一点? List之类的列表

List<List<Integer>> ll = new ArrayList<List<Integer>>();

我的尝试是

ListIterator<Integer> li = steps.listIterator();
List<List<Integer>> fl = new ArrayList<List<Integer>>();
List<Integer> l = new ArrayList<Integer>();

while (li.hasNext()) {

    if (li.next() == 1) {
        l.add(li.next());
    }

    fl.add(l);
}

【问题讨论】:

  • 到目前为止你尝试过什么?
  • 是的,它可以做到,你需要编写一些代码来做到这一点。
  • List&lt;List&lt;Integer&gt;&gt; fl = new ArrayList&lt;List&lt;Integer&gt;&gt;(); List&lt;Integer&gt; l = new ArrayList&lt;Integer&gt;(); while (li.hasNext()) { if (li.next() == 1) { y++; } l.add(li.next()); fl.add(l); }

标签: java list arraylist collections


【解决方案1】:

当您调用iterator.next() 时,迭代器中的光标将移动到下一个位置。您可以在不使用 iterator 的情况下重写您的算法,只需使用 foreach 循环以避免光标移动时出错。

例如:

    List<Integer> originalList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 1, 1, 2, 3, 1, 2, 3, 4, 5, 6));
    List<List<Integer>> finalList = new ArrayList<>();
    List<Integer> currentList = new ArrayList<>();
    for (Integer current : originalList) {
        if (current == 1) {
            if (!currentList.isEmpty()) {
                finalList.add(currentList);
            }
            currentList = new ArrayList<>();
        }
        currentList.add(current);
    }
    if (!currentList.isEmpty()) {
         finalList.add(currentList);
    }

或者,如果你有 java 8,使用StreamEx Library 你可以让它变得更简单:

   List<List<Integer>> streamExList = StreamEx.of(originalList)
            .groupRuns((first, second) -> second != 1)
            .collect(Collectors.toList());

【讨论】:

    【解决方案2】:

    我正在使用 ArrayList 的 subList 方法来创建一个新的 ArrayList

    工作代码如下:

      List<Integer> steps = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4, 5, 6));
                ListIterator<Integer> li = steps.listIterator();
                List<List<Integer>> fl = new ArrayList<List<Integer>>();
                List<Integer> indexes = new ArrayList<>();
                while (li.hasNext()) {
                      if (li.next() == 1) {
                            indexes.add(li.nextIndex() - 1);
                      }
                }
                for (int i = 0; i < indexes.size(); i++) {
                      if (indexes.size() - 1 != i) {
                            fl.add(steps.subList(indexes.get(i), indexes.get(i + 1)));
                      } else {
                            fl.add(steps.subList(indexes.get(i), steps.size()));
                      }
                }
                System.out.println(fl);
    

    是的,我在这里使用了一个名为 indexes 的新列表,添加查找位置的索引很有用。在这里,当然我在新列表(索引)中分配的索引需要-1,所以我编辑了。

    注意:

    您在循环中使用以下代码:

    if (li.next() == 1) {
            l.add(li.next());
        }
    

    这有两次li.next(),最好将它分配给某个变量。这确实意味着如果您在循环中使用多次 li.next() ,指针将移动到下一个位置,并且您不会得到想要的结果,并且您可能面临的运行时异常是NoSuchElementException。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-13
      • 2020-03-25
      • 2016-02-06
      • 2018-04-04
      • 2019-12-22
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多