- public class Traversal {
- public static void main(String[] args) {
- new Traversal().traversal('a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
- }
- public void traversal(Character... cs) {
- List<Character> o = new LinkedList<Character>(Arrays.asList(cs));
- List<Character> f = new LinkedList<Character>();
- traversal_(o, f);
- }
- /**
- * @param o2 未使用的元素的集合
- * @param f2 以使用的元素的集合
- */
- private void traversal_(List<Character> o2, List<Character> f2) {
- println(f2);
- for (int i = 0; i < o2.size(); i++) {
- LinkedList<Character> o = new LinkedList<Character>(o2);
- LinkedList<Character> f = new LinkedList<Character>(f2);
- f.add(o.remove(i));
- traversal_(o, f);
- }
- }
- /**
- * 校验并输出结果
- */
- public void println(List<Character> f2) {
- if (f2.size() == 0) {
- return;
- }
- char[] cs = new char[f2.size()];
- int i = 0;
- for (char c : f2) {
- cs[i] = c;
- i++;
- }
- System.out.println(new String(cs));
- }
- }
复制代码 |