算法:全组合

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

public class Demo {

    private List<String> ITEM = Arrays.asList("1", "2", "3");

    private int SIZE = 3;

    public void array() {
        List<String> data = new ArrayList<String>();

        for (int i = 1; i <= SIZE; i++) {
            array(data, ITEM, "", i);
        }

        for (String str : data) {
            System.out.println(str);
        }
    }

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

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

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

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

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

 

1
2
3
11
12
13
21
22
23
31
32
33
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333

 

你可能感兴趣的:(算法)