日期:2014-05-20 浏览次数:20731 次
package com.zf.test;
import java.util.Arrays;
public class Test03 {
public static void main(String[] args) {
String[] array = {"a","b","c","d","e" ,"f"};
arrange(array , 0);
}
static void arrange(String array[] , int arrayIndex ){
if(arrayIndex == array.length - 1){
System.out.println(Arrays.toString(array));
return ;
}
for (int i = arrayIndex ; i < array.length; i++) {
swap(array , i , arrayIndex);
arrange(array , arrayIndex + 1) ;
swap(array , i , arrayIndex);
}
}
static void swap(String array[] , int i , int j){
String tmp = array[i];
array[i] = array[j];
array[j] = tmp ;
}
}
import java.util.Arrays;
public class Test9 {
static int count = 0;
public static void main(String[] args) {
String[] strs = new String[] { "a", "b", "c", "d", "e", "f" };
print(0, strs);
System.out.println("count:" + count);
}
static void print(int start, String[] arr) {
if (start == arr.length - 1) {
++count;
System.out.println(Arrays.toString(arr));
return;
}
for (int i = start; i < arr.length; ++i) {
print(start + 1, arr);
if (i < arr.length - 1) {
String temp = arr[0];
arr[0] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
}