[英]Different ways of using recursion in Java
I'm thinking of several elegant ways of writing a simple Lisp-like recursive function in Java that does, let's say, a simple summation. 我在考虑几种用Java编写类似于Lisp的简单递归函数的优雅方法,可以说是一个简单的求和。
In Common Lisp it would be like this: 在Common Lisp中,它将是这样的:
(defun summation(l)
(if l
(+ (car l) (summation (cdr l)))
0))
(summation '(1 2 3 4 5)) ==> 15
In Java the one of many possible solutions would be: 在Java中,许多可能的解决方案之一是:
public int summation(int[] array, int n) {
return (n == 0)
? array[0]
: array[n] + summation(array, n - 1);
}
CALL:
summation(new int[]{1,2,3,4,5}, 4); //15
1) Is there any possible way NOT to use the index n? 1)有没有可能不使用索引n的方法?
2) Or leave your solution (non-iterational) which you see as interesting. 2)或留下您认为有趣的解决方案(非迭代)。
Thanks. 谢谢。
Using Java Collections - something like this should give you an idea of how to eliminate n
and recurse in terms of the list size instead: 使用Java集合-这样的事情应该使您了解如何根据列表大小消除n
并递归:
public int summation( List<Integer> list ) {
return list.isEmpty()
? 0
: list.get( list.size - 1 ) + summation( list.subList( 0 , list.size() - 1 ) );
}
Cheers, 干杯,
Usually, I solve this kind of recursion with a public API that does not require the index parameter and a private API with any signature I#d like it to be. 通常,我使用不需要index参数的公共API和具有任何签名I#d的私有API来解决这种递归问题。 For this I would separate it this way: 为此,我将以这种方式将其分开:
public int summation(int[] numbers) {
return summation(numbers, numbers.length - 1);
}
private int summation(int[] numbers, int till) {
return (till < 0) ? 0 : numbers[till] + summation(numbers, till - 1);
}
Note that you must check till < 0
as this handles an empty array correctly. 请注意,您必须检查till < 0
因为它可以正确处理空数组。
Another way would be to not use an array, but any Iterable<Integer>
: 另一种方法是不使用数组,而是使用任何Iterable<Integer>
:
public int summation(Iterable<Integer> numbers) {
return summation(numbers.iterator());
}
private int summation(Iterator<Integer> numbers) {
return (numbers.hasNext()) ? numbers.next() + summation(numbers) : 0;
}
Hint: The order of calls in numbers.next() + summation(numbers)
is important, as the next()
call must be done first. 提示:数字中的调用顺序很重要numbers.next() + summation(numbers)
很重要,因为必须先进行next()
调用。
If you use List.subList method, it may perform iteration, underneath. 如果使用List.subList方法,则它可能在下面执行迭代。 You can use Queue instead, to avoid iteration. 您可以改用Queue以避免迭代。 For example: 例如:
public int sum(Queue queue) {
return queue.isEmpty() ? 0 : (queue.poll() + sum(queue));
}
public class HelloWorld{
static int sum=0;
static int c;
public static void main(String []args){
int[] y={1,2,3,4,5};
c=y.length;
System.out.println( summation(y)); //15
}
public static int summation(int[] array) {
c--;
if(c<0){
return sum;
}
else{
sum+=array[c];
return summation(array);
}
}
}
Here's a simple method that seems pretty close to what's being asked for.Basically, we are taking a recursive approach to performing summation ascontrasted with brute force from the bottom up. 这是一个简单的方法,似乎很接近所要求的方法。基本上,我们采用递归方法来执行求和,而这种求和是自下而上进行的。
public static int sumToN(int n) {
if( n == 0 ){
return 0;
}
return n + sumToN(n - 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.