简体   繁体   中英

permutations and combinations of Arraylists in Java?

I have three Arraylists like this

List<String> list1 = new ArrayList<String>();    
List<String> list2 = new ArrayList<String>();    
List<String> finallist = new ArrayList<String>();   

list1 contains items "a","b" list2 contains items 1 , 2, 3 how to make a finallist that will contain "a1" "a2" "a3" "b1" "b2" "b3"

Assuming that the lists look like this:

    List<String> list1 = Arrays.asList("a", "b", "c");
    List<String> list2 = Arrays.asList("1", "2", "3");

then:

    List<String> finalList = new ArrayList<String>();
    for (String letter: list1) {
        for (String number: list2) {
            finalList.add(letter + number);
        }
    }

    System.out.println(finalList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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