【问题标题】:Comparing List<Integer> by index按索引比较 List<Integer>
【发布时间】:2020-04-15 12:36:47
【问题描述】:
    List<Integer> listOne= new ArrayList<>();
    listOne.add(10);
    listOne.add(2);
    listOne.add(3);

    //Second Array

    List<Integer> listTwo= new ArrayList<>();
    listTwo.add(3);
    listTwo.add(7);
    listTwo.add(1);

我想比较两个 List 并根据哪个值更大指出listOnelistTwo

[10, 2, 3] compare to [3, 7, 1]
if listOne.get(0) > listTwo.get(0) //add one point to listOne

if listOne.get(0) < listTwo.get(0) //add one point to listTwo

这是我测试过的代码

    static List<Integer> compareList(List<Integer> a, List<Integer> b) {
        ArrayList<Integer> output = new ArrayList<>();
        output.add(0, 0);
        output.add(1, 0);
        int out = output.get(0);
        int out2 = output.get(1);
        for (int i = 0; i < a.size(); i++) {
            if (a.get(i) > b.get(i)) {
                out = out + 1;
            } 
            if (a.get(i) < b.get(i)) {
                out2 = out2 + 1;
            } 
        }

        output.add(0, out);
        output.add(1, out2);
        return output;

    }

它可以工作,但它没有输出预期的答案

预期输出:2 1 输出得到:2 1 0 0

【问题讨论】:

    标签: java loops for-loop arraylist compare


    【解决方案1】:

    您将两个0s 添加到List,然后在List 的开头添加两个数字,除了0s。

    要么删除这些语句:

    output.add(0, 0);
    output.add(1, 0);
    

    并保持这些语句不变:

    output.add(0, out);
    output.add(1, out2);
    

    或者只是更改这些语句:

    output.add(0, out);
    output.add(1, out2);
    

    到:

    output.set(0, out);
    output.set(1, out2);
    

    【讨论】:

      【解决方案2】:

      当你这样做时

      output.add(0, out);
      output.add(1, out2);
      

      您将项目添加到索引 0 和 1,这会将先前输入的零推送到索引 2 和 3。output 现在是 [2, 1, 0, 0]

      删除

      output.add(0, 0);
      output.add(1, 0);
      

      并将outout2初始化改为

      int out = 0;
      int out2 = 0;
      

      现在你也不需要按索引添加结果了

      output.add(out);
      output.add(out2);
      

      【讨论】:

        【解决方案3】:

        如果你删除:

        output.add(0, 0);
        output.add(1, 0);
        

        作品:

        static List<Integer> compareList(List<Integer> a, List<Integer> b) {
            ArrayList<Integer> output = new ArrayList<>();
            int out = 0;
            int out2 = 0;
            for (int i = 0; i < a.size(); i++) {
                if (a.get(i) > b.get(i)) {
                    out = out + 1;
                }
                if (a.get(i) < b.get(i)) {
                    out2 = out2 + 1;
                }
            }
        
            output.add(0, out);
            output.add(1, out2);
            return output;
        }
        

        【讨论】:

          【解决方案4】:

          ArrayList 文档说:

          在此列表中的指定位置插入指定元素。 移动当前在该位置的元素(如果有)和任何 右侧的后续元素(将其索引加一)。

          因此,使用声明 output.add(0, out)output.add(1, out2) 您只需在列表中的 0 和 1 位置添加项目,向下推列表中已有的内容(即值 0 和 0 移动到第三和第四位置) .

          另一种方法是使用 HashMap,如下所示:

          static List<Integer> compareList(List<Integer> a, List<Integer> b) {
              HashMap<Integer, Integer> output = new HashMap<>();
              output.put(0, 0);
              output.put(1, 0);
              int out = output.get(0);
              int out2 = output.get(1);
              for (int i = 0; i < a.size(); i++) {
                  if (a.get(i) > b.get(i)) {
                      out = out + 1;
                  }
                  if (a.get(i) < b.get(i)) {
                      out2 = out2 + 1;
                  }
              }
          
              output.put(0, out);
              output.put(1, out2);
              return new ArrayList(output.values());
          
          }
          

          【讨论】:

            猜你喜欢
            • 2021-01-08
            • 1970-01-01
            • 2015-12-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-05
            相关资源
            最近更新 更多