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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:16:04+00:00 2026-06-05T21:16:04+00:00

I have a List<List<String>> I need to get a List of all possible concatenation

  • 0

I have a List<List<String>>
I need to get a List of all possible concatenation on the first dimension

[ [1,2 ], [1] , [3,4] ]

should give:

[ 113, 114, 213, 214 ]

I’m trying with 2 loops, like it should be possible to.

This is what I have tried:

private static List<String> constructIndexes(List<List<String>> indexList){
    List<String> index = new ArrayList<String>();

    String v="";

    for (int i=0; i< indexList.size(); i++){
        List<String> l =  indexList.get(i);
        for (int j=0; j<l.size(); j++){
            if (index.size()>0){
                for (int k=0; k<index.size(); k++){
                    index.set(k, index.get(k)+l.get(j));
                    // System.out.println(">");
                }
            } else {
                index.add(l.get(j));
            }

        }
    }

    return index;
}

some init code:

List<List<String>> indexList = new ArrayList<List<String>>();

    List<String> l = new ArrayList<String>();
    l.add("1");
    l.add("2");
    indexList.add(l);
    l = new ArrayList<String>();
    l.add("1");
    indexList.add(l);
    l = new ArrayList<String>();
    l.add("3");
    l.add("4");
    indexList.add(l);

    System.out.println( constructIndexes(indexList));
  • 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-05T21:16:05+00:00Added an answer on June 5, 2026 at 9:16 pm

    Since the number of lists in the outer list (i.e. the list of lists) is not known until the runtime, you cannot know the number of the loops upfront. In situations like this, you need a recursive solution:

    public static void combine(List<List<String>> data, int[] pos, int n, List<String> res) {
        if (n == pos.length) {
            StringBuilder b = new StringBuilder();
            for (int i = 0 ; i != pos.length ; i++) {
                b.append(data.get(i).get(pos[i]));
            }
            res.add(b.toString());
        } else {
            for (pos[n] = 0 ; pos[n] != data.get(n).size() ; pos[n]++) {
                combine(data, pos, n+1, res);
            }
        }
    }
    

    Here is how to call it:

    List<List<String>> indexList = new ArrayList<List<String>>();
    List<String> a = new ArrayList<String>();
    a.add("1");
    a.add("2");
    List<String> b = new ArrayList<String>();
    b.add("1");
    List<String> c = new ArrayList<String>();
    c.add("3");
    c.add("4");
    indexList.add(a);
    indexList.add(b);
    indexList.add(c);
    List<String> res = new ArrayList<String>();
    combine(indexList, new int[indexList.size()], 0, res);
    for (String s : res) {
        System.out.println(s);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comma delimited list of record id's: string = 2207,117,90,2168,120,118,113,112,17 I need
I would like to get a list of all possible keyword arguments a string
Possible Duplicate: Generate list of all possible permutations of a string I need to
I have List[(String,String)] and I need to sort them by the 2nd value and
Possible Duplicate: PHP preg_match_all: Extract comma seperated list I have a string that looks
I have a list of multiple string and I need to do operation on
I have a generic list of string[] arrays, and I need to build a
I have a list of strings that I need to pass to a process
I have a list of integers or of strings and need to pass it
I have a need to store an ordered list of strings and am trying

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.