generate Permutations from a List of Integer - Java java.lang

Java examples for java.lang:Math Number

Description

generate Permutations from a List of Integer

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    public static Set<List<Integer>> generatePermutations(
            ArrayList<Integer> input, int startindex,
            Set<List<Integer>> combinations) {
        int size = input.size();
        if (size > startindex) {
            ArrayList<Integer> inputList = new ArrayList<Integer>(input);
            Collections.sort(inputList);
            combinations.add(inputList);
        } else {// w w  w  . j  a v a 2  s. co  m
            for (int i = startindex; i < size; i++) {
                int temp = input.get(i);
                input.set(i, input.get(startindex));
                input.set(startindex, temp);
                generatePermutations(input, startindex + 1, combinations);
            }
        }
        return combinations;
    }
}

Related Tutorials