Search code examples
javastack-overflow

StackOverflowError exception ruining interesting and complex program


So this problem is a little bit complicated to understand what I'm trying to do.

Basically, I am trying to randomly generate 3 vectors, all of size 11.

The first vector must have a 1 at position 0 with the next 5 positions being 0 (e.g. 100000) while the next five digits can be either 0 1 or 2, however there can only be one zero used in the last 5 digits, hence 10000012101 would be valid but 10000012001 wouldn't.

The same is applicable to the second and third vector however the first 1 will move a place for the second and the third (010000xxxxx for the second, and 001000xxxxx for the third).

There are more conditions that have to be satisfied. Each vector must differ from each other in at least 5 positions (10000011210 would differ from 01000022100 in 5 positions which would work).

However, there is also a final constraint which states that if you add the vectors modulo 3, then the result of adding these two must have at least 5 NON zero values in the vector.

I have went about this by using arraylists. As I know the first 6 elements of each arraylist for each vector I manually put these in, and for the next 5 elements I randomly assign these, if there is more than one 0 in the last five digits, i call the method again recursively.

The problem I have with this program is that when I try to run my code it comes up with a
Exception in thread "main" java.lang.StackOverflowError at java.util.ArrayList.get(Unknown Source)

I think it's because it's continuously trying to loop and therefore crashing but I'm not sure. See below for the code.

import java.util.ArrayList;

/**
 * The purpose of this class is to be able to capture different ways 
 * of generating six vectors that will produce a collection of 729 
 * vectors that guarantee 9 out of 11 correct. 
 */

public class GenerateVectors {

    static ArrayList<Integer> firstVector = new ArrayList<Integer>();
    static ArrayList<Integer> secondVector = new ArrayList<Integer>();
    static ArrayList<Integer> thirdVector = new ArrayList<Integer>();
    static ArrayList<Integer> sumOfXandY = new ArrayList<Integer>();

    //Creates the first vectors to ensure it starts with "1,0,0,0,0,0" 
    //and has at most one more zero in the last 5 digits
    public void createFirstVector(){

        int[] fir stVector1 = {1,0,0,0,0,0};
        for (int i=0; i<firstVector1.length; i++) {
            firstVector.add(firstVector1[i]);
        }
        for(int i = 0; i < 5; i++){
            int x = (int) (Math.random()*3);
            firstVector.add(x);
    }
    int j = 0;
    for(int i = 6; i<firstVector.size(); i++){
        if(firstVector.get(i).equals(0)){
            j++;
        }
    }
    if(j>1){
        OneZeroInLastFive(firstVector);
    }


    int[] sum = {0,0,0,0,0,0,0,0,0,0,0};
        for (int i=0; i<sum.length; i++) {
            sumOfXandY.add(sum[i]);
        }
    }

    //Edits the vector if there is more than 0 in the last five digits
    public void OneZeroInLastFive(ArrayList<Integer> x){
        int j = 0;
        for(int i = 6; i<x.size(); i++){
            if(x.get(i).equals(0)){
                j++;
            }
        }
        if(j>1){
            x.set(6, (int) (Math.random()*3));
            x.set(7, (int) (Math.random()*3));
            x.set(8, (int) (Math.random()*3));
            x.set(9, (int) (Math.random()*3));
            x.set(10, (int) (Math.random()*3));
            j = 0;
            OneZeroInLastFive(x);
        }
    }


    //Creates the second vector with the last 5 digits random
    public void createSecondVector(){
        int[] secondVector1 = {0,1,0,0,0,0};
        for (int i=0; i<secondVector1.length; i++) {
            secondVector.add(secondVector1[i]);
        }
        for(int i = 0; i < 5; i++){
            int x = (int) (Math.random()*3);
            secondVector.add(x);
        }
    }

    //Creates the third vector with the last 5 digits random
    public void createThirdVector(){
        int[] thirdVector1 = {0,0,1,0,0,0};
        for (int i=0; i<thirdVector1.length; i++) {
            thirdVector.add(thirdVector1[i]);
        }
        for(int i = 0; i < 5; i++){
            int x = (int) (Math.random()*3);
            thirdVector.add(x);
        }
    }

    /**
     * Will edit the second vector to ensure the following conditions are satisfied 
     * - The sum of x and y modulo 3 has at least 5 NON zeros
     * - x and y must DIFFER in at least 5 places 
     * - There is only one zero within the last 5 digits 
     *
     */
    public void checkVectors(ArrayList<Integer> x, ArrayList<Integer> y){
        int k = 0;
        int m = 0;
        for(int j = 0; j < x.size(); j++){
            if(x.get(j).equals(y.get(j))){
                ;
            }
            else{
                k++;
            }   
        }
        for(int i = 6; i<y.size(); i++){
            if(y.get(i).equals(0)){
                m++;
            }
        }
        if((k>4 && m<1)&& checkNonZeros(x,y)){

            System.out.println("Conditions met");
        }
        else{
            y.set(6, (int) (Math.random()*3));
            y.set(7, (int) (Math.random()*3));
            y.set(8, (int) (Math.random()*3));
            y.set(9, (int) (Math.random()*3));
            y.set(10, (int) (Math.random()*3));
            k = 0;
            m = 0;
            checkVectors(x,y);
        }
    }


    public ArrayList<Integer> addTwoVectors(ArrayList<Integer> x, ArrayList<Integer> y, ArrayList<Integer> z){
        for(int i = 0; i<x.size(); i++){
            int j = x.get(i);
            int k = y.get(i);
            z.set(i, ((j+k)%3));
        }
        return z;
    }


    public boolean checkNonZeros(ArrayList<Integer> x, ArrayList<Integer> y){
        addTwoVectors(x,y, sumOfXandY);
        int j = 0;
        for(int i = 0; i<firstVector.size(); i++){
            if(sumOfXandY.get(i).equals(0)){
                ;
            }
            else{
                j++;
            }
        }
        if(j<5){
            return false;
        }
        else {
            return true;
        }
    }

    public static void main(String[] args){
        GenerateVectors g = new GenerateVectors();
        g.createFirstVector();
        g.createSecondVector();
        g.createThirdVector();

        g.checkVectors(firstVector,secondVector);
        g.checkVectors(secondVector,thirdVector);

        System.out.println(firstVector);
        System.out.println(secondVector);
        System.out.println(thirdVector + "\n");

        System.out.println(g.checkNonZeros(firstVector, secondVector));
        System.out.println(g.checkNonZeros(secondVector,thirdVector));
        System.out.println(sumOfXandY);
    }

}

Any help would be much appreciated!!!


Solution

  • The problem is that you have methods that recursively call themselves in order to 'redo', which may happen many times before you get a success. This is fine in languages like scheme or ml which do proper tail recursion, but java does not, so you get stack overflows.

    In order to fix this you need to manually convert the recursive code into a loop. Code that looks like:

    method(arg1, arg2) {
        Code_block_1;
        if (test) {
            Code_block_2;
        } else {
            Code_block_3;
            method(newarg1, newarg2);
        }
    }
    

    needs to become something like:

    method(arg1, arg2) {
        Code_block_1;
        while(!test) {
            Code_block_3;
            arg1 = newarg1;
            arg2 = newarg2;
            Code_block_1;
        }
        Code_block_2;
    }
    

    You can then refactor stuff to get rid of/merge the duplicated code if you wish.