求由0和1组成的不重复的所有排列,使用java编程(急求)

比如给出一个数字2,2个一排列,有4种情况,则所有排列情况为:

 1  0
 0  1
 0  0
 1  1

给出一个数字3,3个一排列,有8种情况,则所有排列情况为:

0 0 1
0 1 0
1 0 0
1 1 0
1 0 1
0 1 1
1 1 1
0 0 0

......

求帮助!

上面写错了

 public static void ejz(int o) {
        int w = calculate(o);
        String t = Integer.toBinaryString(w-1);
        int len = t.length();
        for (int i = 0; i < w; i++) {
            String p = Integer.toBinaryString(i);
            int d = len-p.length();
            String d0 = "";
            for (int j = 0; j< d; j++) {
                d0 += "0";
            }
            System.out.println(d0+p);
        }
    }

calculate方法和上面的一样

你给的栗子有错误吧、到底单独的000 111 00 11 算不算一个?如果算,那么我猜测应该是2的N次方

对,比如给数字4,那么就有16种。我想要的就是:比如在控制台输入4,则列出这16种情况,望指教!

 public static void ejz(int o) {
        String t = Integer.toBinaryString(o);
        int len = t.length();
        for (int i = 1; i <= o; i++) {
            String p = Integer.toBinaryString(i);
            int d = len-p.length();
            String d0 = "";//
            for (int j = 0; j< d; j++) {
                d0 += "0";
            }
            System.out.println(d0+p);
        }
    }

调用ejz试一下呢

 public static void ejz(int o) {
        int w = calculate(o);
        String t = Integer.toBinaryString(w);
        int len = t.length();
        for (int i = 1; i <= w; i++) {
            String p = Integer.toBinaryString(i);
            int d = len-p.length();
            String d0 = "";
            for (int j = 0; j< d; j++) {
                d0 += "0";
            }
            System.out.println(d0+p);
        }
    }
    private static int calculate(int n) {
        if (n == 0)
            return 1;
        return 2 * calculate(n - 1);
    }

package com.test.comb;

import java.util.HashSet;
import java.util.Set;

public class DifferentCombination {
private static Set combinactionSet=new HashSet<>();

public static void main(String[] args) {
    // System.out.println(replaceStrByChar((5-2),"00000",'1'));
    calcCombinaction(10);
    // System.out.println(getDigitStrByChar(5,'0'));
}


 /**
  * 找规律,用代码描述出来.
 * 默认初始都为0
 * 
 * 将1从末位依次往前移动
 * 
 * 末位取1,从末位前一位,将一依次往前移动
 * 假设位数为5个  取长度为len
 * 00000
 * 1一个1
 * 00001   len  第一位算法  末位为1,依次左移   cycle len
 *  00011  len-1                    cycle len-第一个1所在的位数
 *                                  cycle len-
 *      00111  len-2
 *          01111 len-3 
 *          10111 
 *      
 *      01011 len-4
 *          11011
 *          
 *      10011 len-5
 *          
 *  00101 
 *      01101
 *  01001
 *  10001

 * 
 * 00010
 *  
 * 00100
 * ...
 * 10000
 * 2个1
 * 
 * 00001 为初始值
 * 
 * 00011
 * 00101 1在末位,有5种组合 len-1
 * 01001
 * 10001
 * 
 * 00010 为初始值
 * 
 * 00110
 * 01010 1在次位,有4种组合 len-2
 * 10010
 * 
 * 00100 为初始值
 * 
 * 01100 1在次次位,有3种组合len-3
 * 10100
 * 
 * 
 * 
 * 
 */

public static void calcCombinaction(int digitNum) {
    String initNum = getDigitStrByChar(digitNum, '0');
    for (int i = initNum.length(); i > 0; i--) {// 根据长度进行迭代
        String temp = replaceStrByChar(i, initNum, '1');
        combinactionSet.add(temp);
        System.out.println(temp);
        int firstIndex = temp.indexOf('1');
        coreCombinaction(firstIndex, temp);
    }
}
/**
 * 自我迭代
 * 获取第一个1所在的位数,依次向前替换为1
 * @param digitNum
 */
public static void coreCombinaction(int firstIndex, String combstr) {
    for (int i = firstIndex; i > 0; i--) {
        String temp = replaceStrByChar(i - 1, combstr, '1');
        System.out.println(temp);
        combinactionSet.add(temp);
        int firstIndex1 = temp.indexOf('1');
        coreCombinaction(firstIndex1, temp);
    }
}

/**
 * 替换指定位置字符
 * @param index start from 0
 * @param str
 * @return
 */
public static String replaceStrByChar(int index,String str,char destChar){
    StringBuffer sb=new StringBuffer();
    char[] charArray = str.toCharArray();
    for (int i = 0; i < charArray.length; i++) {
        char c = charArray[i];
        if(i==index){
            sb.append("1");
        }else{
            sb.append(c);
        }
    }
    return sb.toString();
}
/**
 * 获取指定长度指定字符的字符串
 * @param digitNum
 * @param c
 * @return
 */
public static String getDigitStrByChar(int digitNum, char c) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < digitNum; i++) {
        sb.append(c);
    }
    return sb.toString();
}

}

我的代码还可以简化的...童鞋们...自己都可以贴出来....

按照楼上大哥我改正后的

    public static void ejz(int o) {

        int w = calculate(o);
        String t = Integer.toBinaryString(w);
        int len = t.length();
        for (int i = 0; i < w; i++) {
            String p = Integer.toBinaryString(i);
            int d = len - p.length();
            StringBuilder d0 = new StringBuilder();
            for (int j = 1; j < d; j++) {
                d0.append("0");
            }
            System.out.println(d0 + p);
        }

    }

   private static int calculate(int n) {
        if (n == 0)
            return 1;
        return 2 * calculate(n - 1);
    }