如何从第一个数组打印一个单词组合,从第二个数组打印第二个,第三个数组打第三个?

时间:2015-11-07 15:42:35

标签: java arrays string combinations

我希望此代码输出以下内容,它有三个以逗号分隔的数组列表条目作为其分隔符("slow,steady"),("blue, white,green")("whale, shark ");。它必须输出以下内容

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

我可以将每个单词分开并将它们放在三个单独的列表中。 list1包含缓慢稳定的list2包含蓝白色绿色而list3包含鲸鲨。但是我无法将它们连接起来。欢迎提供任何意见。

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;
        }

    }    
}   

3 个答案:

答案 0 :(得分:1)

这可以使用嵌套的for循环来完成。在你的情况下3层。

    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);
            }
        }
    }

此外,如果您使用StringBuilder连接多个String,它总是更高效。即。

    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());
            }
        }
    }

答案 1 :(得分:0)

尝试这个

  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]);

            }
        }

    }

}

答案 2 :(得分:0)

终于让它运转良好:) 额外奖励:现在,连接需要多少单词并不重要。

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 , ",");  

    }  
}

结果:

 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