简体   繁体   中英

How to print a combination of words one from first array the second from second array and third from third array?

I want this code to output the following it has three Array List Entries separated by commas as its delimiter ("slow,steady"),("blue, white,green")("whale, shark "); .It has to output the following

slow blue whale 
slow white whale 
slow white shark 
steady blue whale 
steady white shark 
and so forth.

I am able to separate each of the words and put them in three separate lists. list1 contains slow steady list2 contains blue white green and list3 contains whale shark . But I am not able to concatenate them.Any inputs are welcome.

import java.util.StringTokenizer;  
import java.util.ArrayList;
import java.util.List;
import java.util.*;  

public class HelloWorld {    

    String input;   //input String  
    StringBuffer output;  //Holds the output  
    String delimiter = ","; //Delimiter (default comma (,))  
    String arrayOfWords[];  
    List<String> list1 = new ArrayList<String>();
    List<String> list2 = new ArrayList<String>();
    List<String> list3 = new ArrayList<String>();

    int listCount;

    /* 
     * Generates combinations by applying the  
     * concept of recursion 
     */  


    public void generateCombinations(String input, String delimiter,int listCount)   
    {       
          output = new StringBuffer();             
          this.input = input;    
          this.delimiter = delimiter;  
          this.listCount = listCount;

                 String[] stockArr = new String[list1.size()];
                 String[] stockArr1 = new String[list2.size()];
                 String[] stockArr2 = new String[list2.size()];

             if(listCount == 1) {
              String items1[] = input.split(",");
               for(int i=0;i< items1.length; i++){

                 list1.add(items1[i]);
               }

             }

             else if(listCount == 2) {
               String items2[] = input.split(",");
               for(int i=0;i< items2.length; i++){

                 //System.out.println(items2[i]);
                 list2.add(items2[i]);
               }

             }

             else {
              String items3[] = input.split(",");
               for(int i=0;i< items3.length; i++){

                // System.out.println(items3[i]);
                 list3.add(items3[i]);
               }

             }


         stockArr = list1.toArray(stockArr);
         stockArr1 = list2.toArray(stockArr1);
         stockArr2 = list3.toArray(stockArr2);


      for(String s1 : stockArr){

             System.out.print(s1);         
      }        


      for(String s2 : stockArr1){

             System.out.print(s2);     
    }        



      for(String s3 : stockArr2){

             System.out.print(s3);       
      }        


      for(int i=0;i<list1.size();i++){
        String s1 = list1.get(i).toString();
              for(int j=0;j<list2.size();j++){
                    String s2 = list2.get(j).toString();
                     System.out.println(s1+s2);
              }


     }


 }   

    public static void main(String[] args) {    
        ArrayList <String> strings = new ArrayList <String>();
        int i=1;

        for(String string : strings){
        new HelloWorld().generateCombinations(string , ",",i);  
            ++i;
        }

    }    
}   

This can be done using nested for loops. In your case 3 layers.

    stockArr = list1.toArray(stockArr);
    stockArr1 = list2.toArray(stockArr1);
    stockArr2 = list3.toArray(stockArr2);

    for (String a : stockArr) {
        for (String b : stockArr1) {
            for (String c : stockArr2) {
                System.out.println(a + " " + b + " " + c);
            }
        }
    }

Also, it's always more performant if you use StringBuilder to concat multiple String . ie

    for (String a : stockArr) {
        for (String b : stockArr1) {
            for (String c : stockArr2) {
                StringBuilder sb = new StringBuilder();
                sb.append(a)
                  .append(" ")
                  .append(b)
                  .append(" ")
                  .append(c);
                System.out.println(sb.toString());
            }
        }
    }

try this one

  public static void main(String[] args) {

    List<String[]> afterSplit = new ArrayList<String[]>();
    afterSplit.add("slow, steady".split(","));
    afterSplit.add("blue, white, green".split(","));
    afterSplit.add("whale, shark ".split(","));
    for (int i = 0; i < afterSplit.get(0).length; i++) {
        for (int j = 0; j < afterSplit.get(1).length; j++) {
            for (int k = 0; k < afterSplit.get(2).length; k++) {
                System.out.println(afterSplit.get(0)[i].trim() + " "
                        + afterSplit.get(1)[j].trim() + " "
                        + afterSplit.get(2)[k]);

            }
        }

    }

}

Finally had it working good :) Bonus : Now, it doesn't matter how many words you need to concatenate.

import java.util.*;
import java.util.stream.*;

public class Test {

    public static void generateCombinations(ArrayList <String> strings, String delimiter){

        int stringsSize = strings.size();
        int i=0;
        String text = "";
        parseList(text, stringsSize ,i, strings, delimiter);

    }

    public static void parseList(String text, int stringsSize, int i,    ArrayList <String> strings, String delimiter) {
        String stringStream = strings.get(i);
        String[] list = stringStream.split(delimiter);
        if (i==stringsSize-1) {
            for (String string : list){
                System.out.println(text + " " + string.replaceAll("\\s+",""));
            }
        } else {
            for (String string : list){
                String text2 = text + " " + string.replaceAll("\\s+","");
                int j = i+1;
                parseList(text2, stringsSize, j, strings, delimiter);
            }
        }
    }

    public static void main(String[] args) {    
        ArrayList <String> strings = new ArrayList <String>();
        strings.add("slow,steady");
        strings.add("blue, white, green");
        strings.add("whale, shark ");

        generateCombinations(strings , ",");  

    }  
}

Results in :

 slow blue whale
 slow blue shark
 slow white whale
 slow white shark
 slow green whale
 slow green shark
 steady blue whale
 steady blue shark
 steady white whale
 steady white shark
 steady green whale
 steady green shark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM