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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:31:38+00:00 2026-06-17T20:31:38+00:00

I have an arraylist of Strings that want to have all possible combinations stored

  • 0

I have an arraylist of Strings that want to have all possible combinations stored into another collection.

For example:

[air,bus,car]
->
[air]
[bus]
[car]
[air,bus]
[air,car]
[bus,air]
[bus,car]
[car,air]
[car,bus]
[air,bus,car]
[air,car,bus]
...
[car,bus,air]

Repetitions are not important. The code right now I have is:

public ArrayList<String> comb(ArrayList<String> wrds, ArrayList<String> str, int size)
{
    ArrayList<String> s = new ArrayList<String>();
    s.addAll(str);
    if(size != a1.size())
    {
        Iterator e = a1.iterator();
        while(e.hasNext())
        {
            s.add((String)e.next());
        }
        size++;
    }
}

I am trying to get it to recursively call itself so it can store the combinations. Can I get any help as to where or which part I am missing in my code?

  • 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-17T20:31:39+00:00Added an answer on June 17, 2026 at 8:31 pm

    Seeing as this is homework, I’ll try to give you background to the answer.

    The key to solving this is to use recursion.

    First imagine you have two items in your array. You’d could remove the first item to give you your first combination. Adding the remaining item to the first item gives you the second combination. Removing the second item give you the third combination. Adding the remaining item gives you the forth combination. If you had ["air", "bus"] it’d be something like:

    ["air"]
    ["air", "bus"]
    ["bus"]
    ["bus", "air"]
    

    A method that returns that might look like:

    String[][] combinations(String[] strings)
    

    The important things to note are the an array containing a single string can be passed to this method and it can return an array containing an array with a single string in it.

    The problem is complicated a little because you have to keep a tally of the string combinations, so before we get to solving that, it’s important that you understand recursion.

    Imagine you wanted to write a multiplication method that takes two numbers and multiplies them but you only have addition and subtraction at your disposal. You could write a recursive function that adds one of the numbers to itself until the other number reaches an exit condition, something like:

    public int multiply(int value1, int value2) 
    {
      if (value1 > 1) 
      {
        int remaining = value1 - 1;
        return value2 + multiply(remaining, value2);
      }
      else 
      {
        return value2;
      }
    }
    

    You can do just the same thing with an array, only instead to exiting when the a value hit’s 1 you exit when the array contains one item, something like:

    public String[][] combinations(String[] strings) 
    {
      if (strings.length > 1) 
      {
        ...
      }
      else 
      {
        return new String[][]{strings};
      }
    }
    

    For reasons with the Java API it’s much easier to use java.util.List rather than arrays so you want something like:

    public List<List<String>> combinations(List<String> strings) 
    {
      if (strings.size()> 1) 
      {
        ...
      }
      else 
      {
        List<List<String>> result = new ArrayList<List<String>>();
        result.add(strings);
        return result;
      }
    }
    

    Now it’s the ... that’s the important bit. You need to keep an list-of-lists that will be the result and iterate over the strings. For each of the strings you can add that string to the results and then you need create a sub-list that is minus the current string, which you use to call the combinations method again iterating over the result adding the current string each list it contains. In code it looks something like:

    public List<List<String>> combinations(List<String> strings) 
    {
      if (strings.size() > 1) 
      {
        List<List<String>> result = new ArrayList<List<String>>();
    
        for (String str : strings) 
        {
          List<String> subStrings = new ArrayList<String>(strings);
          subStrings.remove(str);
    
          result.add(new ArrayList<String>(Arrays.asList(str)));
    
          for (List<String> combinations : combinations(subStrings)) 
          {
            combinations.add(str);
            result.add(combinations);
          }
        }
    
        return result;
      }
      else 
      {
        List<List<String>> result = new ArrayList<List<String>>();
        result.add(new ArrayList<String>(strings));
        return result;
      }
    }
    

    In summary, what you’re doing is reducing the list of strings down to a single item, then combining it with the preceeding items to produce all the possible combinations as the thread returns up the call stack.

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

Sidebar

Related Questions

I have an arraylist of objects that I want to create all possible combinations
I have an ArrayList that has a nested ArrayList of Strings and I want
I have an ArrayList of strings, and I want to randomly change a string's
I have an ArrayList<JCheckBox> that i want to convert to an ArrayList<String> First I
I have an ArrayList containing Car objects and I want to get from that
I have an ArrayList full of strings that the user populated it with to
I have an ArrayList which get all it's entries as strings from DB List<Amthal>
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
i have an arraylist of strings in my servlet which im passing forwarding using
E.G. to create an ArrayList of Strings we have to do something like List<String>

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.