- public class Test
- {
-
-
- public static void main(String args[])
- {
- String str = "abc";
- char[] a = str.toCharArray();
- sort(a, 0);
- System.out.println("---------------------");
- for(int i=0; i<str.length(); i++)
- {
-
- a = subString(str, i).toCharArray();
-
- sort(a, 0);
-
- System.out.println("--------------------");
- }
- }
-
- //删除一个字符串中的第i个字母 返回一个新的字符串
- static String subString(String str, int i)
- {
- if(i>0)
- {
- str = str.substring(0, i) + str.substring(++i, str.length());
- return str;
- }
- else
- return str.substring(++i, str.length());
- }
- static void sort(char[] str,int index)
- {
- int i;
- if (index == str.length)
- {
- for (i = 0; i < str.length; i++)
- {
- System.out.print(str[i]);
- }
- System.out.println("");
- return;
- }
- for (i = index; i < str.length; i++)
- {
- //交换字符
- char temp;
- temp = str[index];
- str[index] = str[i];
- str[i] = temp;
-
- sort(str, index + 1);
-
- temp = str[index];
- str[index] = str[i];
- str[i] = temp;
- }
- }
- }
复制代码 |