arrays的所有可能组合

我有一个字符串数组

{"ted", "williams", "golden", "voice", "radio"} 

我想要以下forms的所有这些关键字的组合:

 {"ted", "williams", "golden", "voice", "radio", "ted williams", "ted golden", "ted voice", "ted radio", "williams golden", "williams voice", "williams radio", "golden voice", "golden radio", "voice radio", "ted williams golden", "ted williams voice", "ted williams radio", .... } 

我已经好几个小时没有有效的结果(高级编程的副作用??)。

我知道解决方案应该是显而易见的,但老实说,我卡住了! Java / C#中的解决方案被接受。

编辑

  1. 这不是一个功课
  2. “泰德威廉姆斯”和“威廉姆斯特德”被认为是一样的,所以我只想要“泰德威廉姆斯”

编辑2 :在回答答案中的链接之后,结果是Guava用户可以在com.google.common.collect.Sets中使用powerset方法。

编辑:正如FearUs所指出的,更好的解决方案是使用Guava的Sets.powerset(Set set) 。

编辑2:更新了链接。


这个解决方案快速而肮脏的翻译:

 public static void main(String[] args) { List> powerSet = new LinkedList>(); for (int i = 1; i <= args.length; i++) powerSet.addAll(combination(Arrays.asList(args), i)); System.out.println(powerSet); } public static  List> combination(List values, int size) { if (0 == size) { return Collections.singletonList(Collections. emptyList()); } if (values.isEmpty()) { return Collections.emptyList(); } List> combination = new LinkedList>(); T actual = values.iterator().next(); List subSet = new LinkedList(values); subSet.remove(actual); List> subSetCombination = combination(subSet, size - 1); for (List set : subSetCombination) { List newSet = new LinkedList(set); newSet.add(0, actual); combination.add(newSet); } combination.addAll(combination(subSet, size)); return combination; } 

测试:

 $ java PowerSet ted williams golden [[ted], [williams], [golden], [ted, williams], [ted, golden], [williams, golden], [ted, williams, golden]] $ 

我只是遇到了这个问题,对发布的StackExchange答案并不满意,所以这是我的答案。 这将返回Port对象数组中的所有组合。 我会留给读者来适应你正在使用的任何课程(或使其通用)。

此版本不使用递归。

 public static Port[][] combinations ( Port[] ports ) { List combinationList = new ArrayList(); // Start i at 1, so that we do not include the empty set in the results for ( long i = 1; i < Math.pow(2, ports.length); i++ ) { List portList = new ArrayList(); for ( int j = 0; j < ports.length; j++ ) { if ( (i & (long) Math.pow(2, j)) > 0 ) { // Include j in set portList.add(ports[j]); } } combinationList.add(portList.toArray(new Port[0])); } return combinationList.toArray(new Port[0][0]); } 

这是一个提示:

 All-Subsets(X) = {union for all y in X: All-Subsets(Xy)} union {X} 

我的优化解决方案基于Matthew McPeak提供的解决方案。 此版本可避免不必要的arrays副本。

 public static  T[][] combinations(T[] a) { int len = a.length; if (len > 31) throw new IllegalArgumentException(); int numCombinations = (1 << len) - 1; @SuppressWarnings("unchecked") T[][] combinations = (T[][]) java.lang.reflect.Array.newInstance(a.getClass(), numCombinations); // Start i at 1, so that we do not include the empty set in the results for (int i = 1; i <= numCombinations; i++) { @SuppressWarnings("unchecked") T[] combination = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), Integer.bitCount(i)); for (int j = 0, ofs = 0; j < len; j++) if ((i & (1 << j)) > 0) combination[ofs++] = a[j]; combinations[i - 1] = combination; } return combinations; } 
 package rnd; import java.util.ArrayList; public class Rnd { public static void main(String args[]) { String a[] = {"ted", "williams", "golden", "voice", "radio"}; ArrayList result =new ArrayList<>(); for(int i =0 ;i< a.length; i++){ String s = ""; for(int j =i ; j < a.length; j++){ s += a[j] + " " ; result.add(s); } } } } 

我知道这个问题已经过时了,但我没有找到满足我需求的答案。 因此,使用power set的概念和guava库的有序排列,我能够获得原始数组中所有元素组合的数组。

我想要的是这个:

如果我有一个包含三个字符串的数组

 ArrayList tagsArray = new ArrayList<>(Array.asList("foo","bar","cas")); 

我希望在数组中包含所有元素的可能组合:

 {"foo","bar","cas","foobar","foocas","barfoo","barcas","casfoo","casbar","foobarcas","casbarfoo","barcasfoo" . . . . . } 

因此,为了获得这个结果,我使用google的guava lib实现了下一个代码:

  import static com.google.common.collect.Collections2.orderedPermutations; import static java.util.Arrays.asList; public void createTags(){ Set tags = new HashSet<>(); tags.addAll(tagsArray); Set> tagsSets = Sets.powerSet(tags); for (Set sets : tagsSets) { List myList = new ArrayList<>(); myList.addAll(sets); if (!myList.isEmpty()) { for (List perm : orderedPermutations(myList)) { System.out.println(perm); String myTag = Joiner.on("").join(perm); tagsForQuery.add(myTag); } } } for (String hashtag : tagsForQuery) { System.out.println(hashtag); } } 

我希望这对某人有所帮助,这不是一个功课,而是一个Android应用程序。