Java列表置换堆栈溢出

时间:2015-11-26 03:40:15

标签: java list arraylist printing stack-overflow

我正在制作一个程序来生成列表的所有子集,然后置换所有这些子集。我有一种生成子集的方法和一种生成排列的方法。但是,它们不能一起工作,即当我置换子集时,我没有得到子集的所有排列,我得到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)

1 个答案:

答案 0 :(得分:2)

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

将条件更改为'n&lt; = 1'或避免传入空列表以解决问题。

相关问题