Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:59:39+00:00 2026-06-01T07:59:39+00:00

I may not understand what the return statement does(I thought it just returned a

  • 0

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);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:59:40+00:00Added an answer on June 1, 2026 at 7:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I may not understand correctly how array_splice is supposed to work. My
I've looked at these and they do not answer my question: variable-sized object may
One may not always know the Type of an object at compile-time, but may
It may not be best practice but are there ways of removing unsused classes
This may not be the correct way to use controllers, but I did notice
It may not be a pure programming question but I'm looking for information about
This may not really sound hard to do but it is currently killing me.
This may not be a programming question, but I don't know where to ask
This may not be the kind of question one should ask on StackOverflow, but
It may not be so obvious how respond_to? works in ruby. Consider that: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.