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 8041841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:21:40+00:00 2026-06-05T04:21:40+00:00

I would like to have a clean recursive function to generate the same result

  • 0

I would like to have a clean recursive function to generate the same result than

static List<String> l, out;

l=new ArrayList<String>();
l.add("A");l.add("B");l.add("C");l.add("D");l.add("E");l.add("F");
for (int i = 0; i < l.size(); i++) {
    for (int j = i+1; j < l.size(); j++) {
        for (int k = j+1; k < l.size(); k++) {
            for (int t = k+1; t < l.size(); t++) {
                StringBuffer buffer = new StringBuffer(50);
                buffer.append(l.get(i));
                buffer.append(l.get(j));
                buffer.append(l.get(k));
                buffer.append(l.get(t));
                out.add(buffer.toString());
            }
        }
    }
}

here there are 4 levels for example

I gave one try there http://pastebin.com/auSxQMHt

but it’s not working (see the output, I don’t get as many results)

thx

  • 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-05T04:21:42+00:00Added an answer on June 5, 2026 at 4:21 am

    Try this out, your index is off by one:

    private static final void loops(int i, StringBuffer buffer, int level, int k, int size) {
            if  (level>=k){
                out.add(buffer.toString());
            } else {
                for (int j = i; j < size; j++) {
                    StringBuffer buf = new StringBuffer(buffer);
                    buf.append(l.get(j));
                    loops(j+1, buf, level+1, k, size);
                }
            }
    }
    
    ... main() ...
        int size = l.size();
        loops(0, new StringBuffer(50), 0, 4, size);
    ... main() ...
    

    Here are the results:

    p1: 194580 duration:334 ms
    p2: 194580 duration:370 ms
    

    Instead of using “int j = i + 1” use “int j = i” and pass “loops(j+1, buf, level+1, k);”

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

Sidebar

Related Questions

I would like to have a clean formatted output, but the result is not
I have a clean Eclipse Java SDK. I would like to make it suitable
I would like to have a nice clean LINQ code that can get an
I would like to have the same Makefile for building on Linux and on
I would like to have a clean C# class that authenticates from Active Directory.
I would like to have Ctrl-l for clear in my Bash. How can you
I would like have always only float value with dot (not comma) in my
So in my project i would like have a nice treeview that has images.
I learn Symfony 2. I would like have example with SonataAdminBundle. I found: http://sonata-project.org/bundles/news/2-0/doc/reference/installation.html
I would like to have an arrayList that holds reference to object inside the

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.