Java - ArrayList 元素的排列(整数) - 无法使其正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11343848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Permutation of ArrayList elements (Integer) - Can't get it to work properly
提问by Barbe Rouge
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! ;)
我一直在环顾四周以解决我的问题。我解决了很多问题,但这个问题仍然困扰着我 :S 我已经很长时间没有接触 Java 编程(一般编程)了,所以请理解那里!;)
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
我的目标是从整数数组中获得所有可能的组合。当我使用以下代码,应用于整数 {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
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));
}
采纳答案by UVM
You can try Recursion
to solve this issue:
您可以尝试Recursion
解决这个问题:
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.
您也可以在此处复制相同的逻辑。
回答by JayZee
Try this, it seems to work, it uses recursion.
试试这个,它似乎有效,它使用递归。
public class Permute {
public static List<List<Integer>> permute(Integer...myInts){
if(myInts.length==1){
List<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(myInts[0]);
List<List<Integer> > listOfList = new ArrayList<List<Integer>>();
listOfList.add(arrayList);
return listOfList;
}
Set<Integer> setOf = new HashSet<Integer>(Arrays.asList(myInts));
List<List<Integer>> listOfLists = new ArrayList<List<Integer>>();
for(Integer i: myInts){
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(i);
Set<Integer> setOfCopied = new HashSet<Integer>();
setOfCopied.addAll(setOf);
setOfCopied.remove(i);
Integer[] isttt = new Integer[setOfCopied.size()];
setOfCopied.toArray(isttt);
List<List<Integer>> permute = permute(isttt);
Iterator<List<Integer>> iterator = permute.iterator();
while (iterator.hasNext()) {
List<java.lang.Integer> list = iterator.next();
list.add(i);
listOfLists.add(list);
}
}
return listOfLists;
}
public static void main(String[] args) {
List<List<Integer>> permute = permute(1,2,3,4);
System.out.println(permute);
}
}
If you don't like the List> you can easily change from arrays to list using the methods from list and static methods from java.util.Collections and java.util.Arrays.
如果您不喜欢 List>,您可以使用列表中的方法以及 java.util.Collections 和 java.util.Arrays 中的静态方法轻松地从数组更改为列表。