Исключение StackOverflowError, разрушающее интересную и сложную программу - PullRequest
1 голос
/ 05 января 2012

Так что эта проблема немного сложна, чтобы понять, что я пытаюсь сделать.

По сути, я пытаюсь случайным образом сгенерировать 3 вектора, все размером 11.

Первый вектор должен иметь 1 в позиции 0, а следующие 5 позиций - 0 (например, 100000), в то время какследующие пять цифр могут быть либо 0 1, либо 2, однако в последних 5 цифрах может использоваться только один ноль, поэтому 10000012101 будет действительным, а 10000012001 - нет.

То же самое применимо ко второму и третьему вектору, однако первое 1 переместит место для второго и третьего (010000xxxxx для второго и 001000xxxxx для третьего).

ТамЕсть больше условий, которые должны быть выполнены.Каждый вектор должен отличаться друг от друга как минимум в 5 позициях (10000011210 будет отличаться от 01000022100 в 5 позициях, которые будут работать).

Однако есть и последнее ограничение, которое гласит, что если вы добавите векторы по модулю 3, тогда результат сложения этих двух должен иметь не менее 5 NON нулевых значений в векторе.

Я сделал это с помощью массивов.Поскольку я знаю первые 6 элементов каждого массива для каждого вектора, в который я их вручную вставил, и для следующих 5 элементов я назначаю их случайным образом, если в последних пяти цифрах больше одного 0, я вызываю метод снова рекурсивно.

Проблема, с которой я столкнулся в этой программе, заключается в том, что при попытке запустить мой код возникает исключение
в потоке "main" java.lang.StackOverflowError в java.util.ArrayList.get (Неизвестный источник)

Я думаю, это потому, что он постоянно пытается зацикливаться и, следовательно, вылетает, но я не уверен.Ниже приведен код.

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);
    }

}

Любая помощь будет принята с благодарностью !!!

1 Ответ

3 голосов
/ 05 января 2012

Проблема в том, что у вас есть методы, которые рекурсивно вызывают себя, чтобы «повторить», что может произойти много раз, прежде чем вы добьетесь успеха.Это хорошо в таких языках, как схема или ml, которые выполняют правильную хвостовую рекурсию, но java этого не делает, поэтому вы получаете переполнение стека.

Чтобы это исправить, вам нужно вручную преобразовать рекурсивный код в цикл.Код, который выглядит следующим образом:

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

должен выглядеть примерно так:

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

Затем вы можете выполнить рефакторинг, чтобы избавиться от дублированного кода или объединить его, если хотите.

...