简体   繁体   English

如何从ArrayList(Java)中的字符串数组中打印字符串的组合

[英]How to print combinations of Strings from String arrays in an ArrayList (Java)

So I have an ArrayList<String[]> list which contains some String[] that I need to get the combinations of. 所以我有一个ArrayList<String[]>列表,其中包含一些String[] ,我需要获取它们的组合。

So if list contained A = {a,b,c} and B = {d,e,f} I want to print 因此,如果列表包含A = {a,b,c}和B = {d,e,f}我想打印

ad , ae , af , bd , be , bf , cd , ce , cf adaeafbdbebfcdcecf

I am just having trouble seeing how I would have one fixed array then get the other so I can iterate through that. 我只是很难看到我将如何拥有一个固定的阵列然后得到另一个固定的阵列,因此我可以遍历该阵列。

Edit: To clarify I am trying to do this for N String[] so I cant just get the 0th and 1st element. 编辑:澄清一下,我正在尝试为N String[]做这个,所以我不能只得到第0个和第1个元素。

Iterate over two arrays and on each step make new string combination. 遍历两个数组,并在每个步骤上进行新的字符串组合。 After all just print combinations in some way. 毕竟只是以某种方式打印组合。

    List<String[]> input = ...;
    Set<String> allCombinations = new HashSet<>();
    for (int i = 0; i < input.size() - 1; i++) {
        for (int j = i + 1; j < input.size(); j++) {
            allCombinations.addAll(twoArraysCombinations(input.get(i), input.get(j)));
        }
    }
    // print allCombinations


private static Set<String> twoArraysCombinations(String[] first, String[] second) {
    Set<String> result = new HashSet<>();
    for (String f : first) {
        for (String s : second) {
            result.add(f + s);
        }
    }
    return result;
}

Edit: Author changed his question to specify that there are more than 2 lists in an array. 编辑:作者更改了他的问题,以指定一个数组中有两个以上的列表。 This answer works for 2, but it can used to expand to more. 该答案适用于2,但可以用于扩展到更多。 It's still unclear if we wants ALL possible combinations, or just combinations of 2 尚不清楚我们是否需要所有可能的组合,或者仅是2个的组合

Use nested loops to iterate over both ArrayList s and then concatenate and print the strings. 使用嵌套循环遍历两个ArrayList ,然后连接并打印字符串。

If both String[] arrays are in the ArrayList you should pull them out first with String[] A = yourArrayList.get(0) and String[] B = yourArrayList.get(1) 如果两个String[]数组都在ArrayList ,则应首先使用String[] A = yourArrayList.get(0)String[] B = yourArrayList.get(1)将它们拉出。

for (String aString : A) 
    for (String bString : B)
        System.out.println(aString + bString);

You can use: 您可以使用:

for (String a: list.get(0)) {
   for (String b: list.get(1)) {
      System.out.println(a + b);
   }
}

Where list is your original List<String[]> list是您的原始List<String[]>

Just use 2 for-loops 只需使用2个for循环

  String a [] ={"a","b","c"};
  String b [] ={"a","b","c"};
  ArrayList<String> d = new ArrayList<>();
  for (String a1 : a) {
    for (String b1 : b) {
    d.add(a1 + b1);
                        }
                      }
  System.out.println(d.toString());

Impressed with the answers so far....Read the question first guys 迄今为止的答案给人留下了深刻的印象。...首先阅读问题

I'll give you an outline of a solution (warning typos?). 我将为您提供解决方案的概述(警告错别字?)。 I'll leave it up to you to decide how to put commas between entries and other little intricacies incase this is a homework question. 如果这是一个家庭作业问题,我将由您决定如何在条目和其他小巧的地方加上逗号。 Another poster showed how to put the items in a list instead of printing them. 另一位海报展示了如何将这些物品放入列表而不是打印它们。 Depending on your exact needs, you may want to merge my example with theirs. 根据您的确切需求,您可能希望将我的示例与他们的示例合并。

List<String[]> stringLists = ....

for(int firstListIndex = 0; firstListIndex < stringLists.size(); firstListIndex++){
    String[] primaryList = stringLists.get(firstListIndex);
    for(int secondList = firstListIndex + 1; ....){
             String[] secondaryList = ....
             for(String first : primaryList){
                 for(String second : secondaryList){
                     System.out.println(first + second);
                 }
             }

    }

}

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

相关问题 如何将存储在字符串数组中的特定字符串打印到字符串 Arrays 的 ArrayList 中? - How to print a specific String stored into an array of String into an ArrayList of Strings Arrays? 如何在java中打印String数组的ArrayList,这是Map的值 - How to print a ArrayList of String arrays in java which is a value of Map 如何从ArrayList(Java)打印最短的字符串 - How to print shortest String from ArrayList (Java) 用Java打印数组的ArrayList - Print ArrayList of Arrays in Java 如何从 ArrayList 的 ArrayList 打印字符串 - How to print a string from an ArrayList of an ArrayList 如何从 Stream 更改<arrays[string]>至 Stream<strings> java 8</strings></arrays[string]> - How to change from Stream<arrays[String]> to Stream<Strings> java 8 在Java中查找字符串数组或字符串数​​组列表的交集 - Finding intersection of arrays of strings or arraylist of strings in Java 打印出 ArrayList 的所有可能组合<ArrayList<String> &gt; 递归地 - Print out all possible combinations of an ArrayList<ArrayList<String>> recursively 从给定范围的java中的arraylist打印出字符串? - Print out strings from an arraylist in a given range java? 如何从控制台将字符串列表添加到arraylist中,并且程序应在按ctrl + z时显示字符串的反写 - how to add list of strings into an arraylist from console and the program should print the reverse of string on pressing ctrl+z
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM