2014-01-30 47 views
4

假設我有3個列表:['q','w'],['a','s'],['z','x']。如何從這些列表中獲取可能的組合列表?所以我得到一個清單[['q','a','z'],['q','s','z']]等等。我做的方法有兩個,但找不出一個N個清單:獲取列表元素的組合列表

static <E> ArrayList combine(ArrayList<E> one,ArrayList<E> two) 
{ 
    ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>(); 
    for(E e:one) 
    { 
     for(E e2:two) 
     { 
      ArrayList ps=new ArrayList(); 
      ps.add(e); 
      ps.add(e2); 
      combs.add(ps); 
     } 
    } 
    return combs; 
} 

我發現,這是由番石榴的Sets.cartesianProduct完成。

回答

2

你需要N個嵌套循環,這是很難的。

儘管你可以使用遞歸來實現這一點。

static <E> ArrayList combine(ArrayList<E> soFar, ArrayList<E>... lists) 
{ 
    // Rather than constantly making and remaking this list could just use one 
    // and pass it around and add stuff to it. This works though. 
    ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>(); 

    // Loop through the first list looking for elements 
    for(E e:lists[0]) 
    { 
     // Create a new List to build this combination 
     ArrayList<E> temp = new ArrayList<>(soFar); 
     // Add this element to the combination 
     temp.add(e); 
     // If there are more lists recurse down 
     if (lists.length > 1) { 
      // Use recursion to add all combinations of the remaining lists 
      combs.addAll(combine(temp, lists.subList(1))); 
     } else { 
      // There are no more lists so we are done, add temp to the combos 
      combs.add(temp); 
     } 
    } 
    return combs; 
} 


// Call this method to start things going, the other one should probably be private 
static <E> ArrayList combine(ArrayList<E>... lists) 
    return combine(new ArrayList<E>(), lists); 
} 
+0

你如何調用lists.subList?這不是一個數組嗎? – Alexiy

+0

好點,這裏的所有代碼都是直接輸入的,所以需要進行一些調整。 :)使用http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOfRange%28T[],%20int,%20int%29或只是傳遞完整的數組使用soFar.size()作爲數組的索引。 (第二種方法會更有效率)。 –

0

了,你可以創建一個內部類構造函數(參數...),所以你可以把這個類的一個列表,它處理所有的組合

1

你可能想看看類https://github.com/javagl/Combinatorics/blob/master/src/main/java/de/javagl/utils/math/combinatorics/MixedRangeCombinationIterable.java( 「獨立」 級,只是&插入複製到項目)

用法示例:

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

public class Combinations 
{ 
    public static void main(String[] args) 
    { 
     List<List<Character>> lists = new ArrayList<List<Character>>(); 
     lists.add(Arrays.asList('q','w')); 
     lists.add(Arrays.asList('a','s')); 
     lists.add(Arrays.asList('z','x')); 

     MixedRangeCombinationIterable<Character> iterable = 
      new MixedRangeCombinationIterable<Character>(lists); 
     for (List<Character> element : iterable) 
     { 
      System.out.println(element); 
     } 
    } 
} 

您是實際LY計算輸入的http://en.wikipedia.org/wiki/Cartesian_product的元素設置

5

專爲懶人設計(使用番石榴):

Set<List<String>> result = Sets.cartesianProduct(
       ImmutableSet.of("q", "w"), 
       ImmutableSet.of("a", "s"), 
       ImmutableSet.of("z", "x") 
     ); 

System.out.println(result); 

輸出:

[ [q, a, z], [q, a, x], [q, s, z], [q, s, x], [w, a, z], [w, a, x], [w, s, z], [w, s, x] ]