即使使用return语句,递归也永远不会结束

时间:2012-04-04 03:27:45

标签: java recursion

我可能不明白return语句的作用(我认为它只返回了一个变量并允许我退出循环)。我试图更好地理解递归,但这似乎永远不会退出。

import java.util.Arrays;
import java.util.List;

public class main {

    public static void main(String[] args) {
        System.out.println("Starting..");
        List<Integer> list_to_start = Arrays.asList(new Integer[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); 
        String[] name_of_list_to_start = new String[] {"grapes", "Strawberries", "Raspberries", "Blackberries", "Pineapples", "Oranges", "Prunes", "Pears", "cherries", "Peaches", "Apples"};       
        System.out.println(list_to_start.size());
        counter(list_to_start.size(), list_to_start, name_of_list_to_start);
    }

    private static void counter(int length, List<Integer> list_to_start, String[] name_of_list_to_start) {
        // TODO Auto-generated method stub
        if (length == 0) {
            System.out.println("List is empty now");
            for (int i = 0; i>=list_to_start.size(); i++) {
                System.out.println(name_of_list_to_start[i] + " = " + list_to_start.get(i));
            }
            return;
        }   
        Integer x_lenght = (Integer) list_to_start.get(length-1);
        for (int i = 0; i<=5; i++) {
            //System.out.println(i);
            if (length != 0 ) {
                list_to_start.set((length-1), i);
                counter((length-1), list_to_start, name_of_list_to_start);
                list_to_start.set((length-1), 0);
            }
        }
    }
}

基本上我正在尝试做的是,使用递归,找出10种不同水果的0-5的所有组合(这只是为了我学习,而不是家庭作业......不是学生)。

知道我做错了什么吗?为什么这个程序没有用return语句停止?

更新:如果有人遇到同样的问题,这里是上面代码的工作版本(保留破解的代码以便答案有意义):

import java.util.Arrays;
import java.util.List;

public class main {

    public static void main(String[] args) {
        System.out.println("Starting..");
        List<Integer> list_to_start = Arrays.asList(new Integer[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); 
        String[] name_of_list_to_start = new String[] {"grapes", "Strawberries", "Raspberries", "Blackberries", "Pineapples", "Oranges", "Prunes", "Pears", "cherries", "Peaches", "Apples"};       
        System.out.println(list_to_start.size());
        counter(list_to_start.size(), list_to_start, name_of_list_to_start);
    }

    private static void counter(int length, List<Integer> list_to_start, String[] name_of_list_to_start) {
        // TODO Auto-generated method stub
        if (length == 0) {
            //System.out.println("List is empty now");
            for (int i = 0; i<list_to_start.size(); i++) {
                //(name_of_list_to_start[i] + " = " + list_to_start.get(i));
                int k = i +2;
                int y = k -1;
            }
            //System.out.println("********");
            return;
        }   
        Integer x_lenght = (Integer) list_to_start.get(length-1);
        for (int i = 0; i<=5; i++) {
            //System.out.println(i);
            if (length != 0 ) {
                list_to_start.set((length-1), i);
                counter((length-1), list_to_start, name_of_list_to_start);
                list_to_start.set((length-1), 0);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

你确定这是一个无限循环,而不仅仅是一个非常长的序列吗?

在每个级别上,循环5次,每次分支到另一个递归。你有10个级别,所以你在底层总共有5 ^ 10个函数调用,或者9,765,625个System.out.println调用!

您的return语句不在循环中。 return语句退出当前函数调用...但是你在这里有超过1000万个函数调用,所以它必须返回很多次。

答案 1 :(得分:3)

当length = 0时,for循环中的条件应为

i<list_to_start.size()

正如莱利所说,你的递归需要稍微调整一下。