你可以使用递归来做到这一点(不是我经常说的)
public static <T> void combinations(List<T> values, int maxCount, CombinationListener<T> listener) {
List<T> comb = (List<T>) Arrays.asList(new Object[maxCount]);
boolean[] used = new boolean[values.size()];
combinations0(values, used, comb, 0, maxCount, listener);
}
static <T> void combinations0(List<T> values, boolean[] used, List<T> comb, int idx, int maxCount, CombinationListener<T> listener) {
if (idx == maxCount) {
listener.onComlination(comb);
return;
}
for (int i = 0; i < values.size(); i++) {
if (used[i]) continue;
used[i] = true;
comb.set(idx, values.get(i));
combinations0(values, used, comb, idx + 1, maxCount, listener);
used[i] = false;
}
}
public static void main(String[] args) {
combinations(Arrays.asList(1, 2, 3, 4, 5, 6, 7), 5, new CombinationListener<Integer>() {
@Override
public void onComlination(List<Integer> list) {
System.out.println(list);
}
});
}
interface CombinationListener<T> {
void onComlination(List<T> list);
}
印刷
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 6]
[1, 2, 3, 4, 7]
[1, 2, 3, 5, 4]
[1, 2, 3, 5, 6]
[1, 2, 3, 5, 7]
[1, 2, 3, 6, 4]
[1, 2, 3, 6, 5]
[1, 2, 3, 6, 7]
... many deleted ...
[7, 6, 5, 2, 4]
[7, 6, 5, 3, 1]
[7, 6, 5, 3, 2]
[7, 6, 5, 3, 4]
[7, 6, 5, 4, 1]
[7, 6, 5, 4, 2]
[7, 6, 5, 4, 3]