算法:全排列

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo {

    private List<String> ITEM = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9");

    private int SIZE = 9;

    public void array() {
        List<String> data = new ArrayList<String>();
        array(data, ITEM, "");
    }

    public void array(List<String> data, List<String> item, String prefix) {

        if (prefix.length() == SIZE) {
            data.add(prefix);
            return;
        }

        for (int i = 0; i < item.size(); i++) {

            List<String> tmp = new ArrayList<String>(item);

            array(data, tmp, prefix + tmp.remove(i));
        }
    }
}

 

你可能感兴趣的:(全排列)