1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
|
import java.util.ArrayList;
public class Main
{
public static void main(String[] args)
{
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
function(new int[] { 1, 2, 3, 4, 5 }, 0, new ArrayList<Integer>(), result);
for (ArrayList<Integer> list : result) {
System.out.println(list.toString());
}
}
public static void function(int[] numbers, int index, ArrayList<Integer> taken, ArrayList<ArrayList<Integer>> result) {
if (index == numbers.length)
result.add(taken);
else {
function(numbers, index + 1, taken, result); //do not take the current number
ArrayList<Integer> copy = new ArrayList<>(taken);
copy.add(numbers[index]);
function(numbers, index + 1, copy, result); //take it
}
}
} |