for(int j = 0;j < data.length; j++){
for(int i = 0;i < data.length;i++){
System.out.print(data[i]+",");
}
System.out.println();
}

解决方案 »

  1.   

    for(int j = 0;j < data.length; j++){
    for(int i = 0;i < data.length;i++){
    System.out.print("Data["+Data[i]+"]"+",");
    }
    System.out.println();
    }
      

  2.   

    就是全排列吧
    这个问题很常见
    我以前就是用Stack来做的
      

  3.   

    package mine;
    import java.math.*;public class Test {private int[] a;
    private BigInteger numLeft;
    private BigInteger total;//-----------------------------------------------------------
    // Constructor. WARNING: Don't make n too large.
    // Recall that the number of permutations is n!
    // which can be very large, even when n is as small as 20 --
    // 20! = 2,432,902,008,176,640,000 and
    // 21! is too big to fit into a Java long, which is
    // why we use BigInteger instead.
    //----------------------------------------------------------public Test(int n) {
    if (n < 1) {
    throw new IllegalArgumentException("Min 1");
    }
    a = new int[n];
    total = getFactorial(n);
    reset();
    }//------
    // Reset
    //------public void reset() {
    for (int i = 0; i < a.length; i++) {
    a[i] = i;
    }
    numLeft = new BigInteger(total.toString());
    }//------------------------------------------------
    // Return number of permutations not yet generated
    //------------------------------------------------public BigInteger getNumLeft() {
    return numLeft;
    }//------------------------------------
    // Return total number of permutations
    //------------------------------------public BigInteger getTotal() {
    return total;
    }//-----------------------------
    // Are there more permutations?
    //-----------------------------public boolean hasMore() {
    return numLeft.compareTo(BigInteger.ZERO) == 1;
    }//------------------
    // Compute factorial
    //------------------private static BigInteger getFactorial(int n) {
    BigInteger fact = BigInteger.ONE;
    for (int i = n; i > 1; i--) {
    fact = fact.multiply(new BigInteger(Integer.toString(i)));
    }
    return fact;
    }//--------------------------------------------------------
    // Generate next permutation (algorithm from Rosen p. 284)
    //--------------------------------------------------------public int[] getNext() {if (numLeft.equals(total)) {
    numLeft = numLeft.subtract(BigInteger.ONE);
    return a;
    }int temp;// Find largest index j with a[j] < a[j+1]int j = a.length - 2;
    while (a[j] > a[j + 1]) {
    j--;
    }// Find index k such that a[k] is smallest integer
    // greater than a[j] to the right of a[j]int k = a.length - 1;
    while (a[j] > a[k]) {
    k--;
    }// Interchange a[j] and a[k]temp = a[k];
    a[k] = a[j];
    a[j] = temp;// Put tail end of permutation after jth position in increasing orderint r = a.length - 1;
    int s = j + 1;while (r > s) {
    temp = a[s];
    a[s] = a[r];
    a[r] = temp;
    r--;
    s++;
    }numLeft = numLeft.subtract(BigInteger.ONE);
    return a;}public static void main(String[] args) {
    int[] indices;
    String[] elements = {
    "a", "b", "c"};
    Test x = new Test(elements.length);
    StringBuffer permutation;
    while (x.hasMore()) {
    permutation = new StringBuffer();
    indices = x.getNext();
    for (int i = 0; i < indices.length; i++) {
    permutation.append(elements[indices[i]]);
    }
    System.out.println(permutation.toString());
    }}

    给你找了一个
      

  4.   

    /**
     * 一个简单实现,还可优化!
     */
    package untitled1;import java.util.*;public class Untitled1 {
      public static void main(String[] args)
      {
        String[] data = new String[] {"0", "1", "2", "3"};
        for (int i = 0; i < data.length; i++)
        {
          String temp = data[0];
          data[0] = data[i];
          data[i] = temp;
          String[] newdataj = new String[4];      for (int j = 1; j < data.length; j++)
          {
            System.arraycopy(data,0,newdataj,0,data.length);
            String tempj = newdataj[1];
            newdataj[1] = newdataj[j];
            newdataj[j] = tempj;
            String[] newdatak = new String[4];
            for(int k =2; k < data.length; k++)
            {
              System.arraycopy(newdataj,0,newdatak,0,data.length);
              String tempk = newdatak[2];
              newdatak[2] = newdatak[k];
              newdatak[k] = tempk;
              System.out.println(Arrays.asList(newdatak));
            }//end for
          }//end for
        }//end for
      }
    }
    输出:
    [0, 1, 2, 3][0, 1, 3, 2][0, 2, 1, 3][0, 2, 3, 1][0, 3, 2, 1][0, 3, 1, 2][1, 0, 2, 3][1, 0, 3, 2][1, 2, 0, 3][1, 2, 3, 0][1, 3, 2, 0][1, 3, 0, 2][2, 0, 1, 3][2, 0, 3, 1][2, 1, 0, 3][2, 1, 3, 0][2, 3, 1, 0][2, 3, 0, 1][3, 0, 1, 2][3, 0, 2, 1][3, 1, 0, 2][3, 1, 2, 0][3, 2, 1, 0][3, 2, 0, 1]
      

  5.   

    public class ArrayDemo{
      public static void main(String args[]){
        int data[]= new int[4];
          for(int i=0;i<data.length;i++)
    data[i]=i;
          for(int i=0;i<data.length;i++){
    for(int j=0;j<data.length;j++){
      if(j==i) continue;
      for(int k=0;k<data.length;k++){
        if((k==i)||(k==j)) continue;
        for(int m=0;m<data.length;m++){
          if((m==i) || (m==j) || (m==k)) continue;
          System.out.print(data[i]);
          System.out.print(data[j]);
          System.out.print(data[k]);
          System.out.print(data[m]);
          System.out.print("\n");
        }
      }
    }
         }
      }
    }