【问题标题】:How do I make a recursive method which takes type List where the output is a different List?如何制作一个采用 List 类型的递归方法,其中输出是不同的 List?
【发布时间】:2019-01-26 16:38:12
【问题描述】:

我正在尝试创建一个递归方法,它采用 IntegerList 并返回一个新的 List,只有原始 List 的正数。

这就是我所做的

static List<Integer> positives(List<Integer> a) {
    if (a.getTail().isEmpty()) 
        return new List<Integer>(a.getHead(), new List<Integer>());

    if (a.getHead() - a.getHead() == 0) 
        new List<Integer>(a.getHead(), new List<Integer>());
        return positives(a.getTail());

    else
       return positives(a.getTail());

所以当List 的头部为正时,它应该将它添加到一个新列表中,然后方法循环。如果头部不是积极的,它只是循环。当到达List 的末尾时,它应该返回包含正数的列表。

我可以用我目前的代码告诉isEmpty() 语句返回一个与肯定检查语句完全不同的列表,这是我出错的地方。所以我不知道当循环到达List的末尾时如何从第二个if语句返回列表@

【问题讨论】:

  • 为什么需要递归?
  • 看来你需要一个辅助方法。
  • 你使用的 List 实现是什么?
  • 除非这是一个学习递归的课堂项目,否则这是一个糟糕的技术。正如@Flo 暗示的那样,这绝不需要递归。

标签: java list recursion


【解决方案1】:

虽然不是递归的,但最短的方法是使用流

List<Integer> posList = a.stream().filter(x -> x > 0).collect(Collectors.toList());

【讨论】:

    【解决方案2】:

    假设你没有疯,这要么是一个课堂项目 或者是合法的东西的明显简化版本 确实“需要”递归, 这是一个答案。

    提示:客户端调用的方法不必是递归方法。

    一些类似代码的东西:

    public class BlammyHoot
    {
      public List<Integer> letsPretendRecursionIsRequired(final List<Integer> inputList)
      {
        final List<Integer> returnValue = new LinkedList<>();
    
        ... perform input validiation before entering the recursive method.
    
        theRecursiveMethod(inputList, returnValue);
    
        return returnValue;
      }
    
      private void theRecursiveMethod(
        final List<Integer> sourceList,
        final List<Integer> destinationList)
      {
        ... do your recursive thing here.
      }
    }
    

    【讨论】:

      【解决方案3】:

      我发现此声明存在三个潜在问题:

      if (a.getTail().isEmpty()) 
          return new List<Integer>(a.getHead(), new List<Integer>());
      

      首先,我们返回a.getHead(),而从未测试过它是阳性还是阴性。其次,我们无缘无故地创建了一个额外的List。第三,我们是否试图实例化一个接口而不是一个类?在这个ifstatement:

      if (a.getHead() - a.getHead() == 0) 
          new List<Integer>(a.getHead(), new List<Integer>());
          return positives(a.getTail());
      

      我们无缘无故地使a.getHead() &gt;= 0 测试复杂化;我们再次创建了一个额外的List;当我们应该添加到positives() 返回的现有结构时,我们正在创建新结构。

      (缺少.getHead() and .getTail())我可能会这样做:

      static List<Integer> positives(List<Integer> a) {
          List<Integer> non_negatives, tail = a.subList(1, a.size()); // tail = a.getTail()
      
          if (tail.isEmpty()) 
              non_negatives = new ArrayList<Integer>();  // need to create List to return
          else {
              non_negatives = positives(tail);
          }
      
          Integer head = a.get(0);  // head = a.getHead()
      
          if (head >= 0) 
                  non_negatives.add(0, head);
      
          return non_negatives;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 2018-10-19
        • 1970-01-01
        • 1970-01-01
        • 2012-06-27
        • 2021-07-17
        • 1970-01-01
        相关资源
        最近更新 更多