class="java" name="code">package 全排列; import java.util.ArrayList; public class Main { private static ArrayList<String> a = new ArrayList<String>(); public static void main(String[] arg){ String[] ss = {"1","2","3","4","5","6","7","8","9"}; long begin = System.currentTimeMillis(); pailie(0,ss.length-1,ss); for(int i=0;i<a.size();i++){ System.out.println(a.get(i)+" "); } long end = System.currentTimeMillis(); System.out.println((end-begin)/1000f+"秒"); } public static void pailie(int start,int end,String[] ss){ if(start==end){ String s=""; for(int i=0;i<ss.length;i++){ s+=ss[i]; } a.add(s); } for(int i=start;i<=end;i++){ String index=ss[start]; ss[start]=ss[i]; ss[i]=index; pailie(start+1,end,ss); index=ss[start]; ss[start]=ss[i]; ss[i]=index; } } }