Код: Выделить всё
import java.util.List;
import java.util.ArrayList;
public class Combinations {
public static List generateCombinations(int[] arr) {
List result = new ArrayList();
List current = new ArrayList();
generate(arr, 0, current, result);
return result;
}
private static void generate(int[] arr, int index, List current, List result) {
if (index == arr.length) {
result.add(new ArrayList(current));
return;
}
current.add(arr[index]);
generate(arr, index + 1, current, result);
current.remove(current.size() - 1);
generate(arr, index + 1, current, result);
}
public static void main(String[] args) {
int[] arr = new int[] {1, 2, 3, 4, 5};
List combs = generateCombinations(arr);
System.out.println(combs);
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... -in-an-arr