【问题标题】:How to determine if numbers in an array list if divisible by the numbers in another array list?如何确定数组列表中的数字是否可以被另一个数组列表中的数字整除?
【发布时间】:2016-12-05 06:51:16
【问题描述】:

我正在尝试确定数组列表中的数字是否可以被另一个数组列表中的所有数字整除。下面的代码输出listdiv 列表中的所有数字,这些数字可以被listdivisor 中的any 除数禁用。我想输出可以被列表中所有除数整除的值,而不是它们中的任何一个。例如,如果我有 listdiv = [1,2,3,,5,8] 和 listdivisor = [2,4]。预期输出应为 8 ,但此代码输出 2 和 8 。

谢谢!您的努力将不胜感激!

for (int i = 0; i < listdiv.size(); i++) {
            for (int c = 0; c < listdivisor.size(); c++) {
                if (listdiv.get(i) % listdivisor.get(c) == 0) {
                System.out.println("final results are :  " + listdiv.get(i));
                }
            }
        }

【问题讨论】:

    标签: java arraylist


    【解决方案1】:

    Java 8 的其他方式:

    List<Integer> collect = listdiv.stream().filter(p ->
            listdivisor.stream().allMatch(d -> p % d == 0)
    ).collect(Collectors.toList());
    

    或者使用parallelStream()获得更好的性能:

    List<Integer> collect = listdiv.parallelStream().filter(p ->
            listdivisor.parallelStream().allMatch(d -> p % d == 0)
    ).collect(Collectors.toList());
    

    【讨论】:

    • 当输入列表这么小时,Parallel 实际上会更慢。尽管如此,还是要为 Java 8 流解决方案投票。
    【解决方案2】:

    您的错误似乎是由于您在达到 第一个匹配除数时将除数计算为可被 所有除数整除。相反,您需要添加逻辑来跟踪 所有 除数并确保每个除数都适用于给定的除数。

    System.out.println("final results are: ");
    
    for (int i=0; i < listdiv.size(); i++) {
        boolean isDivisible = true;
        for (int c=0; c < listdivisor.size(); c++) {
            if (listdiv.get(i) % listdivisor.get(c) != 0) {
                isDivisible = false;
                break;
            }
        }
    
        if (isDivisible) {
            System.out.println(listdiv.get(i));
        }
    }
    

    【讨论】:

      【解决方案3】:

      您的条件检查是否可以被至少一个除数整除的值,这不是您想要的。

      您需要在生成输出之前检查所有除数:

      for (int i = 0; i < listdiv.size(); i++) {
          boolean divisible = true;
          for (int c = 0; c < listdivisor.size() && divisible; c++) {
              if (listdiv.get(i) % listdivisor.get(c) != 0) {
                  divisible = false;
              }
          }
          if (divisible) {
              System.out.println("final results are :  " + listdiv.get(i));
          }
      }
      

      【讨论】:

      • 谢谢您,您的代码也可以,但我只能勾选一个!
      【解决方案4】:

      在下面的解决方案中,当第一个列表中的元素不能被第二个列表中的任何元素整除时,我将继续外部 for 循环。如果没有发生这种情况, println 将被调用。

      outer: 
      for (int i = 0; i < listdiv.size(); i++) {
          for (int c = 0; c < listdivisor.size(); c++) {
              if (listdiv.get(i) % listdivisor.get(c) != 0) {
                  continue outer;
              }
          }
          System.out.println("Found :  " + listdiv.get(i));
      }
      

      此解决方案不需要任何额外的变量(例如保持可分状态的布尔值)并使用鲜为人知的loop labels

      【讨论】:

      • 谢谢您,您的代码也可以,但我只能勾选一个!
      【解决方案5】:

      到目前为止提出的所有算法都是 O(N*M)。首先找到第一个列表的LCM,然后用它过滤第二个列表,可以得到O(N+M)。

      int thelcm = 1
      for(int i : listdivisor) {
        thelcm = lcm(thelcm, i);
      }
      
      ArrayList<Integer> result = new ArrayList<>();
      for(int j : listdiv) {
        if( j % thelcm == 0 ) {
          result.put(j);
        }
      }
      

      【讨论】:

        【解决方案6】:

        我们可以使用 Set 来获得预期的输出,我已经修改了你的代码,

        Set<Integer> firstIteration = new HashSet<>();
                Set<Integer> nextIteration = new HashSet<>();
                for (int c = 0; c < divisors.size(); c++) {
                    for (int i = 0; i < numbers.size(); i++) {
                        if (numbers.get(i) % divisors.get(c) == 0) {
                            if (c == 0) {
                                firstIteration.add(numbers.get(i));
                            } else {
                                nextIteration.add(numbers.get(i));
                            }
                        }
        
                    }
                    if (c != 0) {
                        // We will perform Set intersection here for each iteration
                        firstIteration.retainAll(nextIteration);
                    }
                }
                System.out.println(firstIteration);
        

        希望这能解决您的问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-18
          • 1970-01-01
          • 2022-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多