递归求解全排列的方法:
import java.util.*; public class ArrangeProblem { public static void main(String[] args) { List<String> list = new ArrayList<String>(); Scanner scan = new Scanner(System.in); String str = scan.nextLine(); for (int i = 0; i < str.length(); i++) { list.add(i, str.substring(i, i + 1)); } new ArrangeProblem().perm(list, 0, list.size() - 1); } void perm(List<String> list, int k, int m) { if (k == m) { for (int i = 0; i <= m; i++) System.out.print(list.get(i)); System.out.println(); } else { for (int i = k; i <= m; i++) { Collections.swap(list, k, i); perm(list, k + 1, m); Collections.swap(list, k, i);//还原集合中数的顺序 } } } }注:计算机算法设计与分析(第三版) 11页