Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将ArrayList添加到HashSet_Java_Arraylist_Hashset - Fatal编程技术网

在Java中将ArrayList添加到HashSet

在Java中将ArrayList添加到HashSet,java,arraylist,hashset,Java,Arraylist,Hashset,我的任务是实现一个蛮力算法,输出一些n的整数[1,2,…,n]的所有置换。但是,我似乎在将ArrayList对象添加到HashSet时遇到了一些问题: static Set<List<Integer>> allPermutations(int n){ if(n<=0){throw new IllegalArgumentException();} List<Integer> thisPermutation = new Ar

我的任务是实现一个蛮力算法,输出一些n的整数[1,2,…,n]的所有置换。但是,我似乎在将ArrayList对象添加到HashSet时遇到了一些问题:

static Set<List<Integer>> allPermutations(int n){
        if(n<=0){throw new IllegalArgumentException();}

        List<Integer> thisPermutation = new ArrayList<Integer>();
        for(int i=1; i<=n; i++){
            thisPermutation.add(i);
        }
        Set<List<Integer>> allPermutations = new HashSet<List<Integer>>();

        while(true){
            allPermutations.add(thisPermutation);
            thisPermutation = nextPermutation(thisPermutation);
            if(thisPermutation == null){break;}
        }
        return allPermutations;
}
静态设置所有置换(int n){
if(n序列get(i-1)){
j+=1;
}
int tempVal=序列.get(i-1);
序列集(i-1,序列get(j));
序列集(j,tempVal);
List reversed=新的ArrayList();
对于(int k=sequence.size()-1;k>=i;k--){
反向.add(sequence.get(k));
}
下一个列表=序列。子列表(0,i);
next.addAll(反向);
下一步返回;
}
列表序列
被传递给
nextpermutat
方法,并在方法内部进行修改:
序列.set(j,tempVal)。因此,每次调用
nextpermutat
方法()时,都会修改原始排列

但是,您可以轻松地修改代码,以便在方法中创建列表的副本:

下一个术语的静态列表(最终列表){
列表顺序=新的数组列表;
int i=sequence.size()-1;
// ...
}

下一个术语置换的实现似乎有问题。共享该函数也可以共享
nextpermutat
方法吗。
static List<Integer> nextPermutation(List<Integer> sequence){
        int i = sequence.size() - 1;
        while(sequence.get(i) < sequence.get(i-1)){
            i -= 1;
            if(i == 0){
                return null;
            }
        }
        int j = i;
        while(j != sequence.size()-1 && sequence.get(j+1) > sequence.get(i-1)){
            j += 1;
        }
        int tempVal = sequence.get(i-1);
        sequence.set(i-1, sequence.get(j));
        sequence.set(j, tempVal);

        List<Integer> reversed = new ArrayList<Integer>();
        for(int k = sequence.size()-1; k>=i; k--){
            reversed.add(sequence.get(k));
        }

        List<Integer> next = sequence.subList(0, i);
        next.addAll(reversed);

        return next;
}