Java数组递归 [英] Java Array Recursion

查看:59
本文介绍了Java数组递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此作业的目的是学习递归方法.对于此特定问题,我需要打印 list 的值,每行打印一个.我需要完成的方法的框架无法更改,如下所示:

The purpose of this assignment is to learn recursive methods. For this particular problem, I need to print the values of list, one per line. The skeleton of the method I need to complete cannot be changed and is as follows:

public void list (String[] list) {

}

说明说辅助方法可能使编写起来更容易.这是我的助手方法所拥有的:

The instructions say that helper methods may make this easier to write. Here is what I have for my helper method:

public void print(String[] list, int index) {    
      if (index < list.length) {
          System.out.println(list[index]);
          print(list, index+1);
      }
}  

我只想通过list方法调用print方法进行测试.我知道评分是通过脚本完成的,因此我也无法更改list方法的返回类型或参数.请原谅我的格式错误,并在此先感谢您的帮助!

I would just like to call the print method through the list method for testing. I know the grading is done through a script, so again I cannot change the return type or parameters of the list method. Please excuse my formatting errors, and thank you in advance for your help!

推荐答案

如果这样做,我将使用名称为 print()或类似名称的辅助方法.

If I were doing this I would use a helper method with the name of print() or something similar.

在考虑程序的逻辑方面,请考虑您需要做的事情:

In terms of thinking about the logic of the program, think about what you would need to do:

具有一个辅助方法 print():

  • 在新行上打印列表中的当前项目
  • 然后调用一个函数以打印列表中的下一个和后续项目,每个项目都在新行上显示

您认为第二个功能可能是什么?提示:这是递归的...

What do you think that second function could be? Hint: It's recursive...

这篇关于Java数组递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆