2015-11-07 132 views
1

我想這段代碼的輸出有三個數組列表中的條目由逗號分隔爲分隔符下面("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; 
     } 

    }  
} 
+0

所以,你想在年底創造一個巨大的字符串? – lukescomputers

+0

感謝您的回覆。我試圖打印的是:緩慢的藍鯨 - 第1行緩慢的白鯨 - 第2行,等等全部在分隔線 –

+0

有沒有任何模式? –

回答

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 
+0

感謝response.I試圖it.It工作perfectly.The遞歸的概念似乎以適應這裏完美。 –

+0

不客氣,這對我來說也是一種很好的鍛鍊。 – Fundhor

+0

沒問題。我只是有一個問題,我無法確定當我試圖通過for循環打印時,代碼中出現了什麼問題。任何想法。可能是列表每次重新初始化爲空? –

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到Concat的多個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()); 
      } 
     } 
    } 
+0

感謝reply.I添加第三for循環太多,但itsn't印刷三個layers.It打印出空白 –

+0

我想指出,它不是__always__更好的性能。這取決於情況和基準需要做的準確地確定效率。 –

+0

加入這個刪除空白:sb.append(myWord.replaceAll(「\\ S +」,「」)) – Fundhor

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

      } 
     } 

    } 

} 
+0

這一個工程too.But如果只有三根弦是present.Thanks雖然 –

相關問題