以abcde为例:
1. 从最后5位开始(从头开始),互相打头,组成字符串
a{bcde} 执行第2步。 ------- > 用{}括起来的代表,向下一步传递的参数param
b{ceda} 执行第2步。
c{edab}.....
直到循环结束。
2. 从最后4位开始操作,{param}每个字母互相打头,组成字符串
(b打头在之前已经执行完了,所以这步从c打头开始)
b{cde}, 执行第3步。
c{deb},执行第3步。
d{ebc},执行第3步。
e{bcd},执行第3步。
此步骤结束,归位{param}
3. 在从最后3位开始操作, {param}每个字母互相打头,组成字符串:
c{de},执行第4步。
d{ec},执行第4步。
e{cd},执行第4步。
此步骤结束,归位{param}
4. 从最后2位开始操作,{param}每个字母互相打头,组成字符串:
de ed,归位{param}
对字符串进行操作实例代码:
public class CopyOfAnagram {
static int size;
static int count;
static char[] arrChar = new char[100];
static String str = "abcde";
public static void main(String args []) {
count = 0;
size = str.length();
for(int i = 0; i < size; i++) {
arrChar[i] = str.charAt(i);
}
parameStr2(str);
}
public static void parameStr2(String param) {
if(param.length() == 1) {
return ;
} else {
for(int i = 0; i < param.length(); i++) {
/*
* 注意:
* new String(arrChar,0,size):每次移动完,都要从数组中从新获得移动后的字符串
* size - param.length() + 1 : 从什么位置截取,不要在外边定义变量!
*/
String temp = new String(arrChar,0,size).substring(size - param.length() + 1);
parameStr2(temp);
if(param.length() == 2) {
printArr();
}
rotateArr(param.length());
}
}
}
private static void rotateArr(int newSize) {
int j;
int position = size - newSize;
char temp = arrChar[position];
for(j = position + 1; j < size; j++) {
arrChar[j - 1] = arrChar[j];
}
arrChar[j - 1] = temp;
}
private static void printArr() {
if(count < 9) {
System.out.print(" ");
} else if(count < 99) {
System.out.print(" ");
}
System.out.print(++count + " ");
for(int i = 0; i < size; i++) {
System.out.print(arrChar[i]);
}
System.out.print(" ");
if(count%6 == 0) {
System.out.println(" ");
}
}
}
还有对当前字符串的个数操作,和对字符串操作差不多,效果更好,但我认为字符串操作更好理解点。