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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:32:13+00:00 2026-05-29T07:32:13+00:00

I cannot wrap my head around the following, say I have a List, each

  • 0

I cannot wrap my head around the following, say I have a List, each list contains a ‘would be’ for loop. each succession should be within one another.

So if I have a list with 3 objects, I want

class Focus {
    String focus;
    List<String> values;

    public Focus(String focus, String... values) {
        this.focus = focus;
        this.values = Lists.newArrayList(values);
    }
}

List<Focus> focuses = new ArrayList<Focus>();
focuses.add(new Focus("Focus 1", "09", "14", "13", "12"));
focuses.add(new Focus("Focus 2", "94", "92"));
focuses.add(new Focus("Focus 3", "A", "B"));

String my_string = "";
for (Focus obj1 : list_obj_x) {
    for (Focus obj2 : list_obj_xx) {
        for (Focus obj3 : list_obj_xxx) {
            my_string += obj1 + " " + obj2 + " " + obj3;
        }
    }
}

obviously with a list the for-loop structure can grow, and the above is not possible.
i need a dynamic structure to cater for the my_string need. i.e:

94    09    A
94    14    A
94    13    A
94    12    A
94    09    B
94    14    B
94    13    B
94    12    B
92    09    A
92    14    A
92    13    A
92    12    A
92    09    B
92    14    B
92    13    B
92    12    B 

the output should be like the above.
this is what I have so far:

int focusCount = focuses.size();
for (int i = (focusCount - 1); i >= 0; i--) {
    Focus currentFocus = focuses.get(i);
    List<String> currentFocusValues = currentFocus.values;

    for (int cfv = 0; cfv < currentFocusValues.size(); cfv++) {
        String currentFocusValue = currentFocusValues.get(cfv);

        for (int j = (i - 1); j >= 0; j--) {
            Focus previousFocus = focuses.get(j);
            List<String> previousFocusValues = previousFocus.values;

            for (int pfv = 0; pfv < previousFocusValues.size(); pfv++) {
                String previousFocusValue = previousFocusValues.get(pfv);
                System.out.println(currentFocusValue + " " + previousFocusValue);
            }
        }
    }
}

it caters for all combinations of the list values,
but not in the structure I want.

Can someone please help me?

  • 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-05-29T07:32:14+00:00Added an answer on May 29, 2026 at 7:32 am

    The most straightforward approach would probably be recursion. In each step of the recursion, you “pin down” the value of the n-th list one by one, then recurse down the “list of lists” until you reach the end.

    String[] values = new String[focuses.size()];
    CreateCombinations(focuses, 0, values);
    

    With the recursive method

    private void CreateCombinations(List<Focus> focuses, int index, string[] values) {
        Focus focus = focuses.get(index);
        for (string v : focus.values) {
            values[index] = v;
            if (index < focuses.size() - 1) {
                // there is at least one other focus
                CreateCombinations(focuses, index+1, values);
            } else {
                // all values pinned down
                StringBuilder sb = new StringBuilder(values[0]);
                for (int i = 1; i < values.length; ++i) {
                    sb.append(" ").append(values[i]);
                }
                // now do whatever you like to do with sb.toString()...
            }
        }
    }
    

    Of course, this can be refined further, but maybe it suffices as a starting point for you.

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

Sidebar

Related Questions

I have a rails question that I cannot seem to wrap my head around.
I have a small problem that I cannot seem to wrap my head around
I have just come across delegation in python and cannot wrap my head around
I have a problem that I cannot wrap my head around. I'm using Apache
I have a database issue that i currently cannot wrap my head around with
I cannot wrap my head around this...I have an array that looks like: Array
I'm trying to wrap my head around linq -> nhib I have a simple
I'm trying to wrap my head around the proper syntax to achieve the following--or
I simply cannot wrap my head around how to solve this problem and after
I've searched the web high and low and just cannot wrap my head around

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.