I am trying to get all possible permutations of an ArrayList that are the same length as the input arrayList. I.e. an ArrayList of 1,2,3 would result in 123, 132, 213, 231, 321, 312, not including the shorter permutations like 1, 2, 12, 13... etc. Here is the code I have so far:
public void getAllPermutations(ArrayList<coordinate> coords) {
ArrayList<coordinate> sub = new ArrayList<coordinate>();
permutateSub(sub, coords);
}
private ArrayList<ArrayList<coordinate>> permutateSub(ArrayList<coordinate> sub,
ArrayList<coordinate> coords) {
int n = coords.size();
if(n == 0) System.out.println(sub);
else {
if(sub.size()==n) {
System.out.println(sub);
for(int i = 0; i<n; i++) {
ArrayList<coordinate> a = new ArrayList<coordinate>(sub);
a.add(coords.get(i));
ArrayList<coordinate> b = new ArrayList<coordinate>(coords);
b.remove(i);
permutateSub(a, b);
}
}
}
A coordinate is a class that just has x, y, and visited to hold 2D points for a project.
Currently I am using this code to print it to the console, but I would also appreciate it if someone could shed some light into how I would store this into an ArrayList>. Thanks.