【问题标题】:Split a list of values based on a value into a list using Java使用Java将基于值的值列表拆分为列表
【发布时间】:2020-09-04 13:01:46
【问题描述】:

考虑一个长值列表[1,2,3,4,2,3,6,3,7,3]。我需要实现 java 程序来根据值拆分列表。假设value = 3。每当我找到value = 3。我需要以[[1,2] ,[3],[4,2],[3],[6],[3],[7],[3]] 的方式拆分列表。

我写了下面的代码来实现问题陈述:

    public class Solution {
    public static void main(String args[]) {
      List<Integer> list = new ArrayList<Integer>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(2);
      list.add(3);
      list.add(6);
      list.add(3);
      list.add(7);
      System.out.println(list);

      int val = 3;
      List<List<Integer>> finalList = new ArrayList<>();
      List<Integer> tempList = new ArrayList<>();

      for (int i : list) {
          if (i != val) {
              tempList.add(i);
          } else {
              finalList.add(new ArrayList<>(tempList));
              finalList.add(Arrays.asList(i));
              tempList.clear();
          }
      }

        System.out.println(finalList);
    }
}

输出: [[1, 2], [3], [4, 2], [3], [6], [3]]

预期输出: [[1,2] ,[3],[4,2],[3],[6],[3],[7]]

最后一个数字 [7] 未添加到 finalList。任何建议!

【问题讨论】:

  • 首先,您没有将这些值[1,2,3,4,2,3,6,3,7,3] 添加到列表中。请保持您的输入与代码中的输入一致。
  • 在你的 for 循环之后,临时列表包含 7。所以你也必须在这里添加到最终列表。

标签: java list algorithm arraylist


【解决方案1】:

问题是您将整个起始列表添加到数组中,

相反,您需要记住其他数组,以便在遇到 3 时可以推动这个数组。

List<Integer> list_i_want_to_push = new ArrayList<>();
for (int i =0; i < list.size(); i++){
              list_i_want_to_push.add(list.get(i));

              if(val == list.get(i)){
                  list_i_want_to_push.remove(i);
                  finalList.add(list_i_want_to_push);
                  finalList.add(Arrays.asList(val));
                  list_i_want_to_push = []; //empty your array
              }

             }
             System.out.println(finalList);

【讨论】:

  • 您的代码无法编译。它是List&lt;Integer&gt;,而不是List&lt;integer&gt;。旁注:list_i_want_to_push 这不是 java 风格。 Arrays.asList(val) 返回一个固定列表,如果需要,以后不能对其进行修改。
  • @HarshalParekh,该死的!不知道你评论的第二部分!!谢谢队友,固定类型!
【解决方案2】:

我相信这可以满足您的要求,但我必须做出一些假设。

  • 如果出现相邻的拆分编号,只需将每个拆分编号放入自己的列表中并添加到最终列表中。
  • 假设列表可以以一个或多个拆分数字开始和/或结束。
import java.util.ArrayList;
import java.util.List;

public class SplitListOnValue {
    public static void main(String args[]) {
        List<Integer> list = new ArrayList<>(List.of(3,3,2,3,4,2,3,3,3,6,6,6,3,7,3,3));
        System.out.println(list);

        int splitVal = 3;
        List<Integer> temp = new ArrayList<>();
        List<List<Integer>> finalList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            int item = list.get(i);
            if (item == splitVal) {
                // add both list and singleton list if temp not empty
                // other wise just add singletone list
                if (!temp.isEmpty()) {
                    finalList.add(new ArrayList<>(temp));
                }
                finalList.add(new ArrayList<>(List.of(splitVal)));
                // clear temp list and restart process
                temp.clear();
                continue;
            } 
            // still accumulating
            temp.add(item);
        }
        System.out.println(finalList);
    }
}

打印

[[3], [3], [2], [3], [4, 2], [3], [3], [3], [6, 6, 6], [3], [7], [3], [3]]

【讨论】:

    【解决方案3】:

    试试这样:

    public static void main(String[] args) {
        // The number you want to split by
        int delimiter = 3;
        List<Integer> input = Arrays.asList(5, 3, 10, 4, 3, 5, 6, 3, 4);
    
        List<List<Integer>> result = new ArrayList<>();
        result.add(new ArrayList<>()); // Add a list so that we have something to work with
    
        for (int i = 0; i < input.size(); i++) {
            // Get the next value in the list
            int val = input.get(i);
    
            // If the current value equals the one we want to split by...
            if (val == delimiter) {
                //... add a new list containing only the delimiter...
                result.add(Arrays.asList(delimiter));
    
                //... and add a new list so the other values will be added correctly
                result.add(new ArrayList<>());
            } else // if this is just a random number, add it to the last list
                result.get(result.size() - 1).add(val);
        }
    
        // If the last number in your input is equal to the delimiter, there will be an empty
        // list at the end, which can be prevented this way
        if (result.get(result.size() - 1).size() == 0)
            result.remove(result.size() - 1);
    
    }
    

    【讨论】:

    • 请在您的答案周围添加一些上下文,不要只发布代码。
    • 附带说明,如果您花时间写答案,也请花一些时间发布完整的解决方案;不要离开// your list 并在那里添加示例列表。
    • 另外,保持你的变量名与问题中的变量名一致。这只能是 OP 代码的一部分。
    • result 中有一个空的[]
    • @HarshalParekh 你说得对,我添加了一些 cmets 并将修复那个空的 []。但是,我将使用我最熟悉的变量名,因为如果我要在此处发布 OP 的解决方案,他们至少可以更改变量名。
    猜你喜欢
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 2018-10-07
    相关资源
    最近更新 更多