I may not understand what the return statement does(I thought it just returned a variable and allowed me to exit the loop). I'm trying to get a better understanding of recursions but this never seems to exit.
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);
}
}
}
}
Basically what I'm trying to do is, using a recursion, figure out all the combinations of 0-5 for 10 different fruits(this is just for me to learn, not homework..not a student).
Any idea what I'm doing wrong? Why is this program not stopping with the return statement?
update: in case anyone ever has the same problem, here's the working version of the above code(keeping the broken code so the answers make sense):
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);
}
}
}
}
Are you sure this is an infinite loop, and not just a really long sequence?
On each level you loop 5 times, branching to another recursion each time. You have 10 levels, so you'll have a total of 5^10 function calls at the bottom level, or 9,765,625 System.out.println calls!
Your return statement is not in a loop. The return statement exits the current function call... but you have over 10 million function calls here, so it has to return a lot of times.
Your condition in the for loop when length = 0 should be
i<list_to_start.size()
and as Riley said, your recursion needs to be tweaked somewhat.
will cause exceptions when listtostart.size() == - Riley Lark 2012-04-04 03:37