爪哇 - 排列的ArrayList元素(整数) - 不能让它正常工作 [英] Java - Permutation of ArrayList elements (Integer) - Can't get it to work properly

查看:88
本文介绍了爪哇 - 排列的ArrayList元素(整数) - 不能让它正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在四处寻找了不少解决我的问题。我解决了很多问题,但是这一次仍然是困扰我:S这已经有很长一段时间我都没有碰Java编程(一般程序),所以要了解那里! ;)

我的目标是让所有的组合可能出整数数组中。当我用下面的code,适用于整数的测试序列{1,2,3,4},我期望有:
结果1 2 3 4
结果,1 2 4 3
结果,1 3 2 4
结果,1 3 4 2
结果2 1 3 4
结果2 1 4 3
结果(...)
结果,但这里是我所得到的
结果1 2 3 4
结果,1 2 3 4 4 3
结果,1 2 3 4 4 3 3 2 4

没有任何人有任何线索,建议甚至是解决方案吗?在此先感谢!

 公共类计算{
(......)
  公共无效置换(ArrayList的<整数GT; SOFAR,ArrayList的<整数GT;休息){
    如果(rest.isEmpty())this.fillMatrice(convertIntegers(SOFAR)); //那里进去pviously创建为int的箭头$ P $
    其他{
        对于(INT K = 0; K< rest.size(); K ++){
            ArrayList的<整数GT;接下来=新的ArrayList<整数GT;();
            接下来= SOFAR;
            next.add(rest.get(K));
            ArrayList的<整数GT;剩余=新的ArrayList<整数GT;();
            清单<整数GT;子列表= rest.subList(0,k)的;
            为(int类型的= 0;一个与所述; sublist.size();一++)remaining.add(sublist.get(一));
            子列表= rest.subList第(k + 1,rest.size());
            为(int类型的= 0;一个与所述; sublist.size();一++)remaining.add(sublist.get(一));
            置换(下一个,剩下的);
        }
    }
}
公共静态的ArrayList<整数GT; convertArray(INT []整数){
    ArrayList的<整数GT; convArray =新的ArrayList<整数GT;();
    的for(int i = 0; I< integers.length;我++)convArray.add(整数[I]);
    返回convArray;
}
公共静态INT [] convertIntegers(列表<整数GT;的整数){
    INT [] = RET新INT [integers.size()];
    的for(int i = 0; I< ret.length;我++)RET [I] = integers.get(I).intValue();
    返回RET;
}
公共计算(){
    (......)
    ArrayList的<整数GT; SOFAR =新的ArrayList<整数GT;();
    INT [] =测试{1,2,3,4};
    置换(SOFAR,convertArray(试验));
}


解决方案

您可以尝试递归来解决这个问题:

 公共静态无效printPermutations(INT [] N,INT [] NR,INT IDX){
    如果(IDX == n.length){//为递归[基本条款]停止条件
        的System.out.println(Arrays.toString(正));
        返回;
    }
    的for(int i = 0; I< = NR [IDX];我++){
        N [IDX] =我;
        printPermutations(N,NR,IDX + 1); //递归invokation,未来元素
    }
}

更多信息可以从这个环节可以了:
 <一href=\"http://stackoverflow.com/questions/9632677/combinatorics-generate-all-states-array-combinations\">Combinatorics:生成所有国 - 阵列组合

您可以在这里复制相同的逻辑。

I've been looking around quite a bit to solve my issue. I got many problems solved but this one is still haunting me :S It's been a long time I haven't touch Java programming (programming in general) so be understanding out there! ;)

My goal is to get all the combination possible out of an array of integers. When I use the following code, applied to the test array of integer {1, 2, 3, 4}, I expect to have:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
2 1 3 4
2 1 4 3
(...)
but here is what I get
1 2 3 4
1 2 3 4 4 3
1 2 3 4 4 3 3 2 4

Does anybody have a clue, a suggestion or even a solution? Thanks in advance!

public class Calculation{
(...)
  public void Permute(ArrayList<Integer> soFar,ArrayList<Integer> rest){
    if(rest.isEmpty())    this.fillMatrice(convertIntegers(soFar)); // there it goes in a previously created arrow of int
    else{
        for(int k=0;k<rest.size();k++){
            ArrayList<Integer> next=new ArrayList<Integer>();
            next=soFar;
            next.add(rest.get(k));
            ArrayList<Integer> remaining=new ArrayList<Integer>();
            List<Integer> sublist = rest.subList(0, k);
            for(int a=0;a<sublist.size();a++)   remaining.add(sublist.get(a));
            sublist = rest.subList(k+1,rest.size());
            for(int a=0;a<sublist.size();a++)   remaining.add(sublist.get(a));
            Permute(next,remaining);
        }
    }
}
public static ArrayList<Integer> convertArray(int[] integers){
    ArrayList<Integer> convArray=new ArrayList<Integer>();
    for(int i=0;i<integers.length;i++)  convArray.add(integers[i]);
    return convArray;
}
public static int[] convertIntegers(List<Integer> integers){
    int[] ret = new int[integers.size()];
    for(int i=0;i<ret.length;i++)   ret[i]=integers.get(i).intValue();
    return ret;
}
public Calculation() {
    (...)
    ArrayList<Integer> soFar=new ArrayList<Integer>();
    int[] test={1,2,3,4};
    Permute(soFar,convertArray(test));
}

解决方案

You can try Recursion to solve this issue:

public static void printPermutations(int[] n, int[] Nr, int idx) {
    if (idx == n.length) {  //stop condition for the recursion [base clause]
        System.out.println(Arrays.toString(n));
        return;
    }
    for (int i = 0; i <= Nr[idx]; i++) { 
        n[idx] = i;
        printPermutations(n, Nr, idx+1); //recursive invokation, for next elements
    }
}

More info can be had from this link: Combinatorics: generate all "states" - array combinations

You can replicate the same logic here as well.

这篇关于爪哇 - 排列的ArrayList元素(整数) - 不能让它正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆