[java]順列 お手軽 2015/05/10

Javaです。
順列です。
組み合わせの列挙なのですが、
書けといわれてすぐ書けなくて。。。。
>_<!

困ったときのapacheさんです。

Math - Commons Math: The Apache Commons Mathematics Library
Collections - Home

Mathにあるんだろなーと思って探したんですが、どうもランダムに生成するのはあったのですが、たんに組みわせを全部とりだいたいという ニーズにあわなくて、するとCollectionsのほうにいい感じのものがありました。
ちなみに、最新のMathとコレクションの現時点でのバージョンは、
                 [org.apache.commons/commons-math3 "3.5"]
                 [org.apache.commons/commons-collections4 "4.0"]
 です。


PermutationIteratorはcommons-collections4からあります。
※使うのは、PermutationIteratorですよ!!

出力結果
[0, 1, 2, 3]
[0, 1, 3, 2]
[0, 3, 1, 2]
[3, 0, 1, 2]
[3, 0, 2, 1]
[0, 3, 2, 1]
[0, 2, 3, 1]
[0, 2, 1, 3]
[2, 0, 1, 3]
[2, 0, 3, 1]
[2, 3, 0, 1]
[3, 2, 0, 1]
[3, 2, 1, 0]
[2, 3, 1, 0]
[2, 1, 3, 0]
[2, 1, 0, 3]
[1, 2, 0, 3]
[1, 2, 3, 0]
[1, 3, 2, 0]
[3, 1, 2, 0]
[3, 1, 0, 2]
[1, 3, 0, 2]
[1, 0, 3, 2]
[1, 0, 2, 3]

以下コード

List<Integer> list = Arrays.asList(new Integer[]{0,1,2,3}); PermutationIterator<Integer> iterator = new PermutationIterator<>(list); while(iterator.hasNext()) { System.out.println(iterator.next()); }



: