Java数组递归
本作业的目的是学习递归方法。对于这个特定问题,我需要打印 list
的值,每行一个。我需要完成的方法的框架无法更改,如下所示:
public void list (String[] list) {
}
说明说辅助方法可能会使编写更容易。这是我的辅助方法:
public void print(String[] list, int index) {
if (index < list.length) {
System.out.println(list[index]);
print(list, index+1);
}
}
我只想通过列表方法调用打印方法进行测试。我知道评分是通过脚本完成的,所以我再次无法更改列表方法的返回类型或参数。请原谅我的格式错误,并提前感谢您的帮助!
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);
}
}
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有很多Java 中的递归的好例子。阅读这些内容您将会受益匪浅。首先阅读你的教科书,然后继续这些例子
There are lots of good examples of recursion in Java. You would benefit greatly from reading these. First read your textbook, then continue with these examples
如果我这样做,我将使用名称为
print()
或类似名称的辅助方法。在考虑程序逻辑方面,请考虑您需要做什么:
有一个辅助方法
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:
Have a helper method
print()
that:What do you think that second function could be? Hint: It's recursive...
我不会直接给你答案,因为你不会从中学到任何东西。您可以创建变量来存储当前深度。创建 getter 和 setter 辅助方法以增加递归深度。您的方法列表将使用相同的数组调用自身,但前提是您检查深度不大于数组的大小。不要忘记增加每次调用的递归深度。
I won't give you direct answer, since you won't learn anything from it. You can create the variable to store current depth. Create getter and setter helper methods to increase the depth of recursion. Your method list, will call itself, with the same array, but only after you check that the depth is not bigger then the size of array. Don't forget to increase the depth of the recursion on every call.
想像这样的递归解决方案:给定一堆东西(比如列表),对其中一个东西(比如列表的第一个元素)做一些事情,然后将其余的东西传递给相同的解决方案处理其中一件事情
提示:您需要考虑您的解决方案在没有给出任何内容的情况下会做什么。
提示 2:不必在每次递归时“切碎”数组,只需记下您在数组中的位置即可。
Think of a recursive solution like this: given a bunch of things (like a list) do something with one of those things (like the first element of the list), then pass the rest of the things to the same solution that processed one of the things
Hint: you need to think about what your solution does in the case when it's given nothing.
Hint 2: Instead of "chopping up" the array each time you recurse, just note your place in the array.
当您进行递归时,有时写出如何使用循环执行相同的任务会很有帮助:
现在,假设我们想要更接近递归解决方案。第一步可能是去掉 for 循环的内部部分:
好的,现在我们已经非常接近递归解决方案了。我们只需要执行循环正在处理的最后两个任务,递增
index
并检查index
index
index
index
index
index
。这些看起来可能是减少步骤和基本案例的绝佳候选者。index
。列表.长度When your doing recursion, it can sometimes be helpful to write out how you would perform the same task using a loop:
Now, say we wanted to get closer to a recursive solution. The first step might be to get rid of the interior part of the
for
loop:Okay, now we are really close to the recursive solution. We just need to take the last two tasks that the loop is handling, incrementing
index
and checking ifindex < list.length
. These look like they might be great candidates for a reduction step and a base case.让我们从头开始:
您可能已经知道递归函数是调用自身的函数。如果函数每次运行时都会调用自身,那么您将陷入无限循环。因此,始终确保您的函数也知道何时停止调用自身非常重要。
我们怎样才能做到这一点?函数体在调用之间不会改变,因为它是在编译时设置的。然而,将会改变的是函数的输入。每当编写递归函数时,都需要包含在满足特定条件时避免递归调用的逻辑。
在您的示例中,函数的输入是字符串数组。知道何时停止进行递归调用的一种方法是每次向函数传递一个较小的数组,并在有空数组时停止。这在像 C 这样的语言中效果更好,其中数组作为指针公开,但在 Java 中效率很低,因为每次都必须创建一个新数组。
更好的解决方案是维护一个指向列表中当前位置的索引(这就是为什么您需要一个辅助函数 - 来添加额外的参数)。在每次调用时,您只需打印当前索引处的字符串并递增索引即可。当索引不再位于数组内时,就完成了!
如果你真的想很好地理解递归,我建议学习一门函数式语言,比如 Lisp、Haskell 或 ML。函数式语言避免可变状态(更改变量的值),因此它们不使用 for 循环之类的东西(因为需要在每次迭代时更新循环计数器)。所以他们使用递归来实现循环!例如,在 Java 中,我们可能会这样:
而在 OCaml 中,我们会编写以下内容:
这里的重要区别在于,我们不是更改 count 的值(对于上述 OCaml 代码,实际上这是不可能的),而是将一个新值传递到每次
循环
函数。Let's start from the beginning:
You probably already know that a recursive function is one that calls itself. If every time a function runs it calls itself, you are going to end up in an infinite loop. Therefore, it is important to always make sure your function knows when to stop calling itself as well.
How can we do this? The body of the function is not going to change between calls, since it is set at compile time. What will change, however, is the input to the function. Whenever you write a recursive function, you need to include logic that will avoid the recursive call when a certain condition is met.
In your example, the input to your function is an array of Strings. One way to know when to stop making the recursive call could be to pass a smaller array to the function each time, and stop when you have an empty array. This would work better in a language like C where arrays are exposed as pointers, but in Java it would be inefficient as you would have to create a new array each time.
A nicer solution would be to maintain an index pointing to your current position in the list (this is why you need a helper function - to add the extra parameter). At each call, you can simply print the String at the current index and increment the index. When the index is no longer inside the array, you're done!
If you really want to get a good understanding of recursion, I recommend learning a functional language such as Lisp, Haskell, or ML. Functional languages avoid mutable state (changing the values of variables), and as such they don't use things like for loops (because of the need to update a loop counter at every iteration). So they implement loops using recursion instead! For example, in Java we might have:
whereas in OCaml we would write the following:
The important difference here is that, rather than changing the value of count (actually not possible given the above OCaml code), we pass a new value into the
loop
function each time.我认为这不是一个很好的方法,尽管它会有所帮助。
I think this is not the much good way even though it will be helpful.