2

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.

3
  • Looks like this could be a duplicate of stackoverflow.com/questions/4240080/… Commented Sep 6, 2014 at 21:10
  • Funny, I see the declaration of method permutateSub as if it's supposed to return an ArrayList<ArrayList<coordinate>> object, but I don't see return anywhere in the function's code. Commented Sep 6, 2014 at 21:20
  • Oops, my mistake. Regardless, it would still return shorter permutations. Commented Sep 6, 2014 at 21:26

2 Answers 2

5

Take a look at Guava's Collections2 permutations method.

Example (source)

public void permutations () {
    List<Integer> vals = Ints.asList(new int[] {1, 2, 3});

    Collection<List<Integer>> orderPerm = Collections2.permutations(vals);

    for (List<Integer> val : orderPerm) {
        logger.info(val);
    }
}

/* output:
 [1, 2, 3]
 [1, 3, 2]
 [3, 1, 2]
 [3, 2, 1]
 [2, 3, 1]
 [2, 1, 3]
*/
Sign up to request clarification or add additional context in comments.

2 Comments

+1: No need to reinvent the wheel. Guava will be helpful in many situations anyway.
In the future, I will definitely make use of this. Thanks very much.
3

Here's one way to do it:

public static void permutation(List<coordinate> nums) {
    List<List<coordinate>> accum = new ArrayList<List<coordinate>>();
    permutation(accum, Arrays.<coordinate>asList(), nums);
    System.out.println(accum);
}

private static void permutation(List<List<coordinate>> accum, List<coordinate> prefix, List<coordinate> nums) {
    int n = nums.size();
    if (n == 0) {
        accum.add(prefix);
    } else {
        for (int i = 0; i < n; ++i) {
            List<coordinate> newPrefix = new ArrayList<coordinate>();
            newPrefix.addAll(prefix);
            newPrefix.add(nums.get(i));
            List<coordinate> numsLeft = new ArrayList<coordinate>();
            numsLeft.addAll(nums);
            numsLeft.remove(i);
            permutation(accum, newPrefix, numsLeft);
        }
    }
}

10 Comments

Just to mention it, this is using Java 8 features.
Thank you for contributing! However, they are not lists of Integers, they are lists of coordinates. So there are several errors that pop up involving List.
You can just search and replace Integer with coordinate
@IngoBürk why dont you mention what Java 8 features is using as well
@AHalbert You are probably importing the wrong List interface/class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.