public class Test{
public static void main(String[] args) {
print("abc".toCharArray(),"");
}
public static void print(char[] chr,String deleteStr){
for(int i = 0;i<chr.length;i++){
String delStr = deleteStr;
for(int j = 0;j<chr.length;j++){
if(j == i)continue;//chr[i] == chr[j]没必要比较了.
System.out.println(delStr + Character.toString(chr[i]) + Character.toString(chr[j]));
}
delStr += Character.toString(chr[i]);
char[] deleteChar = deleteCurrentChar(chr,i);
print(deleteChar,delStr);
}
}
public static char[] deleteCurrentChar(char[] chr,int idx){//删除当前位字母.
char[] chrResult = new char[chr.length - 1];//去掉一个长度
int j = 0;
for(int i = 0;i<chr.length;i++,j++){
if(i == idx){
if(i == chr.length - 1){
break;
}
i++;
chrResult[j] = chr[i];
continue;
}
chrResult[j] = chr[i];
}
return chrResult;
}
} |