图片好像不能上传,附上代码,同样的方式调用方法,同样的方式递归,为什么打印的值不相同?public class Recursion { public static void main(String[] args) {
Recursion recursion = new Recursion();
List<Long> list = new ArrayList<Long>();
Long num = null;
recursion.recursionFunction_1(5L, list);
recursion.recursionFunction_2(5L, num);
System.out.println("recursionFunction_1:" + list.get(0));
System.out.println("recursionFunction_2:" + num);
} private void recursionFunction_1(Long i, List<Long> list) {
i--; if (i == 0) {
list.add(i);
} else {
recursionFunction_1(i, list);
}
} private void recursionFunction_2(Long i, Long num) {
i--; if (i == 0) {
num = i;
} else {
recursionFunction_2(i, num);
}
}
}
添加回答
举报
0/150
提交
取消