简体   繁体   English

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

[英]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 "); 我希望此代码输出以下内容,它具有三个以逗号分隔的数组列表条目作为分隔符("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 . list1包含慢速稳定list2包含蓝白色绿色,list3包含鲸鲨。 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. 这可以使用嵌套的for循环来完成。 In your case 3 layers. 在您的情况下为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);
            }
        }
    }

Also, it's always more performant if you use StringBuilder to concat multiple String . 另外,如果您使用StringBuilder来连接多个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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:使用第一个数组的前3个整数,然后是第二个数组的3个整数,将2个数组组合成第三个数组 - Java: Combine 2 arrays into a third array using the first 3 integers of the first array then 3 integers from the second one 用户提供的整数数组的第一,第二和第三大数 - First, Second and Third largest number from user provided integer array 将第二列和第三列从2d数组检索到数组 - Retrieving second and third column from a 2d array to an array 如何从数组中获取第二个最大值,第三个最大值等 - How to get the second highest value from an array, the third highest value etc 打印字符串中的字符(第一个字符;然后是第一个和第二个;然后是第一个,第二个和第三个) - Print characters from a string (first character; then first and second; then first, second, and third) 通过添加第一个和第二个 Arrays 的元素来填充第三个数组 - Fill the Third Array by adding the elements of the First and Second Arrays 找到第二个和第三个最大元素数组java - find second and third maximum element array java 如何将两个 arrays 中的常用词保存到第三个数组中? - How do I save common words from two arrays into a third array? 如果第二个数组中的值与第一个数组中的值相同,如何从另一个数组中删除一个值 - How to remove one arraylist value from another arraylist if the value in second array is same in first arraylist 如何使用 java 中的 do while 循环从第一个数组中填充第二个数组? - How to fill second array from the first one using do while loop in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM