for(int j = 0;j < data.length; j++){
for(int i = 0;i < data.length;i++){
System.out.print(data[i]+",");
}
System.out.println();
}
for(int i = 0;i < data.length;i++){
System.out.print(data[i]+",");
}
System.out.println();
}
解决方案 »
- 各位朋友,问个java读取google网页内容的问题,请进来看
- myeclipse 中怎么给main中args 赋值
- java如何调用dll内的特定方法
- 【新手提问】为什么我这段代码的JOptionPane的对话框会出现找不到符号呢?
- 请教一个小问题
- 【关于Java UDP网络连接问题!在线等......】
- 急!!!哪位高手帮我看一下,下面的代码编译可以通过,但数据存不到数据库中!!!!我将万分感激!!!!
- 怎样判断一个对象是什么类型的类?
- 以前没来过这里,要怎么样才能增加可用分呀?一天五分太少了。(内空)
- 子类构造函数问题
- 国际化问题用struts1.1应该如何写第二问
- 跪求java chm 类库帮助说明,要详细地址散份了
for(int i = 0;i < data.length;i++){
System.out.print("Data["+Data[i]+"]"+",");
}
System.out.println();
}
这个问题很常见
我以前就是用Stack来做的
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());
}}
}
给你找了一个
* 一个简单实现,还可优化!
*/
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]
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");
}
}
}
}
}
}