简体   繁体   English

从数组列表的元素中检索所有组合

[英]Retrieving all combinations from elements of arraylists

I want to know a procedure have combinations from Arraylists.我想知道一个过程有来自 Arraylists 的组合。 Scenario is as follows.场景如下。 Lets say there are 3 different Arraylists like below.假设有 3 个不同的 Arraylist,如下所示。

ex:前任:

  1. [Silver, White, Grey, Purple] 【银、白、灰、紫】
  2. [Petrol, Hybrid] [汽油、混合动力]
  3. [Toyota, Micro] [丰田、微]

combinations can be created like given below, out of the above three Arraylists.组合可以像下面给出的那样创建,在上面的三个 Arraylists 中。

ex:前任:

  1. Silver Petrol Toyota银色汽油丰田
  2. Silver Petrol Micro银色汽油微型
  3. Silver Hybrid Toyota银色混合动力丰田
  4. Silver Hybrid Micro银色混合微型
  5. White Petrol Toyota白色汽油丰田
  6. White Petrol Micro白色汽油微型
  7. White Hybrid Toyota白色混合动力丰田
  8. White Hybrid Micro白色混合微型
  9. Grey Petrol Toyota灰色汽油丰田
  10. Grey Petrol Micro灰色汽油微型
  11. Grey Hybrid Toyota灰色混合动力丰田
  12. Grey Hybrid Micro灰色混合微型
  13. Purple Petrol Toyota紫色汽油丰田
  14. Purple Petrol Micro紫油微
  15. Purple Hybrid Toyota紫色混合动力丰田
  16. Purple Hybrid Micro紫色混合微型

In my case the number of arraylists are dynamic.在我的情况下,数组列表的数量是动态的。 The size of one arraylist is also dynamic.一个数组列表的大小也是动态的。 In the scenario like that I want to know the method to implement using Java.在这样的场景中,我想知道使用 Java 实现的方法。 Can anyone please suggest me a way to this using Java?任何人都可以建议我使用Java来解决这个问题吗?

Thanks谢谢

You can put all the List you have to a totalList, whose type is List>.您可以将您拥有的所有List 放入一个totalList,其类型为List>。 Combine 2 lists at a time to populate all of the combination cases.一次组合 2 个列表以填充所有组合案例。

Below is an exmaple for your reference, and it will print the information as you want.下面是一个示例供您参考,它会根据您的需要打印信息。

Note: It supports dynamic number of Lists and dynamic size of List .注意:它支持 List 的动态数量和 List 的动态大小

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {

    public static void main(String[] args) {

        List<String> listOne = Arrays.asList("Silver", "White", "Grey",
                "Purple");
        List<String> listTwo = Arrays.asList("Petrol", "Hybrid");
        List<String> listThree = Arrays.asList("Toyota", "Micro");
        List<List<String>> totalList =  Arrays.asList(listOne,listTwo,listThree);

        new Test().printAllCases(totalList);
    }

    public void printAllCases(List<List<String>> totalList) {
        List<String> result = new ArrayList<String>(totalList.get(0));

        for(int index = 1; index < totalList.size(); index++) {
            result = combineTwoLists(result, totalList.get(index));
        }

        /* print */
        int count = 0;
        for(String s: result) {
            System.out.printf("%d. %s\n", ++count, s);
        }
    }

    private List<String> combineTwoLists(List<String> list1, List<String>   list2) {
        List<String> result = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        for(String s1 : list1) {
            for(String s2: list2) {
                sb.setLength(0);
                sb.append(s1).append(' ').append(s2);
                result.add(sb.toString());
            }
        }
        return result;
    }
}

If you're not opposed to outside libraries, guava-libraries provides a very nice Sets.cartesianProduct method that can do precisely this.如果您不反对外部库, guava-libraries提供了一个非常好的Sets.cartesianProduct方法,可以准确地做到这一点。 Of course, you'd wind up converting your lists over...当然,你最终会转换你的列表......

Set<String> colors = ImmutableSet.of("Silver", "White", "Grey", "Purple");
Set<String> fuelTypes = ImmutableSet.of("Petrol", "Hybrid");
Set<String> brands = ImmutableSet.of("Toyota", "Micro");

Set<List<String>> result = Sets.cartesianProduct(colors, fuelTypes, brands);
System.out.println(result);

Well think of this pseudo code:想想这个伪代码:

for (i for size of array1) {
    for (j for size of array2) {
        for (k for size of array3) {
            print array1[i] + array2[j] + array3[k]
        }
    }
}

In Java something like this:在 Java 中是这样的:

int i, j, k;
int count = 0;
for (i = 0; array1.size(); i++) {
    for (j = 0; array2.size(); j++) {
        for (k = 0; array3.size(); k++) {
            count++;
            System.out.println(count + ". " +array1.get(i) + array2.get(j) + array3.get(k));
        }
    }
}

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

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