我可能不明白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);
            }
        }
    }
}

最佳答案

您确定这是一个无限循环,而不仅仅是一个很长的序列?

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

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

关于java - 即使使用return语句,递归也永远不会结束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10004540/

10-10 06:03