gpt4 book ai didi

Java 列表排列 stackoverflow

转载 作者:行者123 更新时间:2023-12-02 04:09:25 24 4
gpt4 key购买 nike

我正在制作一个程序,它生成列表的所有子集,然后排列所有这些子集。我有一个生成子集的方法和一个生成排列的方法。但是,它们不能一起工作,即当我排列子集时,我没有得到子集的所有排列,并且出现 StackOverflow 错误。

这是我的代码:

//import java.awt.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.List;
//import java.util.*;
public class help {
public static PrintWriter writer;

public static void main(String args[]){
try{
File cryptfile = new File("C:/Users/hansg17/Documents/new_text.txt");
writer = new PrintWriter(cryptfile,"UTF-8");
//this is all the writing code

writer.println("This is working");
//deals with file reading
File read = new File("C:/Users/hansg17/workspace/newJAVart/src/newJAVart/text.txt");
Scanner s = new Scanner(read);
ArrayList<String> reddit = new ArrayList<String>();
while(s.hasNextLine()){
reddit.add(s.next());
//System.out.println(s.next());


List<String> arrayList = new ArrayList<String>();


}







/////////////////////////Pay attention to stuff below////////////
ArrayList<String> test = new ArrayList<String>();
test.add("5");
test.add("this");


s.close();
int n = test.size();
;
allSets(test);
System.out.println("Above is subsets");
permu(n,test);
System.out.println("Above is permuatations of the list");

//the reason I use get(0) all the subsets are in the zeroth element
System.out.println(powerTest.toString());
System.out.println("List of Lists");
//why is this not the same a s

for(ArrayList<String> ic: powerTest){
permu(ic.size(),ic);
}
//this code should print all the permutations of all the subsets instead has stackoverflow eero



}catch(FileNotFoundException e ){
e.printStackTrace();

}catch(UnsupportedEncodingException e){
e.printStackTrace();
}/*catch(StackOverflowError e ){
//would this catch the text file being too long?
System.out.println("Looks like output is too long");
}*/


}





//builds power set
/**
* ArrayList does zvaeaefdsfe
* @param length asdfdsafafea
* @return ArrayList with all possible character strings of length n
*/




//List<String> list = new ArrayList<String>();

//powerSet = new HashSet<List<String>>();
static //List<String> list = new ArrayList<String>();

HashSet<ArrayList<String>> powerSet = new HashSet<ArrayList<String>>();
static ArrayList<ArrayList<String>> powerTest = new ArrayList<ArrayList<String>>();
public static void allSets(ArrayList<String> junk2){



if(powerSet.add(junk2)){
powerSet.add(junk2);

System.out.println(junk2.toString());
//the commented code below does not generate all permutations of the subsets why?
//permu(junk2.size(),junk2);
powerTest.add(junk2);

}
//writer.println(junk2.toString());

//recursivemethod to generate all lists and convert to string

for(int i = 0;i<junk2.size();i++){
ArrayList<String> temp = new ArrayList<String>(junk2);
temp.remove(i);
allSets(temp);

}

}

static int ring = 0;






/**
*
* @param a List
* @return all permutation of List
*/

//This is based on Heap's Algorithm Pseudo code
public static ArrayList<String> permu(int n, ArrayList<String> stuff){
if(n == 1){

System.out.println(stuff.toString());
return stuff;
}else{
for(int i = 0;i< n-1;i++){
permu(n-1,stuff);
if(n%2 == 0){
Collections.swap(stuff, i, n-1);
}else{
Collections.swap(stuff, 0, n-1);
}

}
return permu(n-1,stuff);



}
}

/**
*
* @param length
*
*/
/*public void alpNumList(int length){
ArrayList<String> alphnum = new ArrayList<String>()

//prints out all aphanumerical list of a certain size length

allSets(alphnum)

}
public void numList(int length){
ArrayList<String> num = new ArrayList<String>()
//prints out all numerical lists of a certain size
allSets(num)
}
public void alpList(int length){
//prints out all alphabetical list of certain length
ArrayList<String> alphabet = new ArrayList<String>();
allSets(alphabet)
}
*/








}

这是领事的输出:

 [5, this]
[this]
[]
[5]
Above is subsets
[5, this]
[this, 5]
Above is permutations of the list
[[5, this], [this], [], [5]]
List of Lists
This is what happens when I
[5, this]
[this, 5]
[this]
Exception in thread "main" java.lang.StackOverflowError
at test_project.help.permu(help.java:152)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)
at test_project.help.permu(help.java:166)

最佳答案

这里的问题是您在 permu 方法中传递了一个大小为 0 的列表,即“[]”。由于permu方法中的结束条件是“n == 1”,在这种情况下永远不会满足,因此递归会永远向下,直到堆栈溢出。

将条件更改为“n <= 1”或避免传入空列表应该可以解决问题。

关于Java 列表排列 stackoverflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33930187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com