I am making a program that generates all the subsets of a list and then permutes all those subsets. I have a method for generating subsets and a method for generating permutations . However, they do not work together ie when I permute the subsets I do not get all the permutations of the subsets and I get a StackOverflow error.
Here;s my code:
//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)
}
*/
}
Here's the output to the consul:
[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)
The problem here is that you pass in the permu method a list of size 0, ie, '[]'. As the end condition in permu method is 'n == 1', which will never be met in this case, the recursion goes down forever until the stack is overflowed.
Change the condition to 'n <= 1' or avoid passing in an empty list should fix the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.