public class StringPermutation { /** * 方法名稱:main() * 方法描述: * @param * @return String * @Exception */ public static void main(String[] args) { // TODO Auto-generated method stub char[] ch = "aba".toCharArray(); permutation(ch, 0); } private static boolean isSwap(char[] s, int begin, int end){ for(int i=begin;i<end;i++){ if(s[i] == s[end]){ return false; } } return true; } /** * 方法名稱:permutation() * 方法描述:index:當前第幾個數, size:共有多少個數 * @param * @return String * @Exception */ public static void permutation(char[] s, int index){ if(index >= s.length){ System.out.println(new String (s)); }else{ //begin 與其後面的字符進行交換 for(int i=index;i<s.length;i++){ if(isSwap(s, index, i)){ swap(s, index, i); permutation(s, index+1); swap(s, index, i); } } } } public static void swap(char[] s,int i, int j){ char temp = s[i]; s[i] = s[j]; s[j] = temp; } } 參考:http://blog.csdn.net/hackbuteer1/article/details/7462447