This question already has answers here:
What is a StackOverflowError?
                                
                                    (14个回答)
                                
                        
                2年前关闭。
            
        

我有下一个错误:// Hola,tengo el siguiente错误:

注意:我知道这是内存错误,但是我想知道如何解决它,thnx。

#javac Resol.java

#java Resol
Exception in thread "main" java.lang.StackOverflowError
    at Nodo.<init>(Nodo.java:17)
    at Nodo.<init>(Nodo.java:17)
    at Nodo.<init>(Nodo.java:17)
    at Nodo.<init>(Nodo.java:17)
    at Nodo.<init>(Nodo.java:17)
    at Nodo.<init>(Nodo.java:17)
    at Nodo.<init>(Nodo.java:17)
    at Nodo.<init>(Nodo.java:17)
    at Nodo.<init>(Nodo.java:17)... to inf...


这是Resol类:

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import java.lang.Math;
import java.lang.*;

public class Resol{

    public static int cantVar;
    public static  int cantRest;
    public ArrayList<Nodo> lista;
    public static int[] costs = {65,55,60,45,40,50};
    public static int[][] matriz= {
    {1,1,0,0,0,0},
    {1,1,0,0,0,1},
    {0,0,1,1,1,0},
    {0,0,0,1,0,1},
    {0,1,0,0,1,1},
}; //matriz de restricciones

    public Resol(){
        this.cantVar=6;
        this.cantRest=5;
        this.lista = new ArrayList<Nodo>();
        this.costs=costs;
        this.matriz=matriz;

}


public static Nodo iniciarNodo(Nodo l, int i){
    if(l.inic==false){
        l.anterior=l;
        l.inic=true;
    }else{
        if(l.izq.inic==false){
            l.izq=new Nodo();
            l=l.izq;
            l.anterior=l.anterior.anterior;

        }
    }

    l.solucion.add(i);
    return l;
}

public static void esSolucion(Nodo l){
    Resol r = new Resol();
    r.lista.add(l);
    ListIterator it;
    it=l.solucion.listIterator();
    while(it.hasNext()){
        System.out.println("datos: "+ it.next());
    }

}




public static boolean esFactible(ArrayList<Integer> l){
    ListIterator it;
    //int suma;
    boolean cumple=false;

    for(int i = 0; i < cantVar; i++){
        it=l.listIterator();
        int j=0, suma=0;
        while(it.hasNext()){
            if(l.size() < cantVar){

                if((int)it.next()!=matriz[j][i]){
                    suma = suma +1;

                }
                j++;

            }
            if(suma==cantRest){
                break;

            }else{
                return false;
            }

        }
    }

    return true;
}

public static void volver(Nodo l){
    if(l.der!=null){
        volver(l.anterior);
    }else{
        Backtrack(l.der, 1);
    }

    return;
}


public static void agregar(Nodo l, int i){
    if(l==null){
        agregar(iniciarNodo(l, i),i);
    }else{
        if(l.solucion.size()<1){
            l.solucion = l.anterior.solucion;
            l.solucion.add(i);
        }

    }
}

public static void Backtrack(Nodo l,int i){
    if(l != null && l.inic==false){
        Backtrack(iniciarNodo(l, i), i);

    }else{
        if(esFactible(l.solucion) && l.solucion.size()==cantVar){
            esSolucion(l);
            volver(l.anterior);
            return;
        }
        //l.solucion.add(0);
        //agregar(l, i);
        if(esFactible(l.solucion)){

            agregar(l, i);
        }else{
            //Backtrack(l.anterior.izq, 1);
            Backtrack(l.anterior.der, 1);
        }

    }


}

    public static void main(String[] args){
        //ArrayList<Integer> sol = new ArrayList<>();
        Nodo sol;
        sol = new Nodo();
        System.out.println("esto hay: "+ sol.inic);
        Resol resol;
        resol = new Resol();
        //SetCoverProblem scp = SetCoverProblem.getInstance();
        resol.Backtrack(sol, 0);

    }



}


这是Nodo类:

   import java.util.ArrayList;
   import java.util.List;
   import java.util.ListIterator;
   import java.util.Random;
   import java.lang.Math;
   import java.lang.*;

public class Nodo{
    public Nodo izq;
    public Nodo der;
    public Nodo anterior;
    public long fitness;
    public boolean inic;
    public ArrayList<Integer> solucion;

public Nodo(){
    izq = new Nodo();
    der = new Nodo();
    this.fitness = 0;
    this.inic = false;
    this.solucion = new ArrayList<Integer>();

}

public Nodo getIzq(){
    return izq;
}

public Nodo getDer(){
    return der;
}

public Nodo getAnt(){
    return anterior;
}

public void setIzq(){
    this.izq=izq;
}

public void setDer(){
    this.der=der;
}

public void setAnt(){
    this.anterior=anterior;
}


}

每个帮助都会有用

最佳答案

查看您的Nodo类的构造函数。

public Nodo(){
    izq = new Nodo();
    der = new Nodo();
    this.fitness = 0;
    this.inic = false;
    this.solucion = new ArrayList<Integer>();

}


您是否注意到在前两行中,您再次调用了相同的构造函数?换句话说,每个构造函数调用都必须首先解析一个构造函数调用。那是一个无休止的过程。这就是为什么您得到StackOverflowError的原因。堆栈跟踪甚至通过告诉您行号来清楚地指出这一点。

Exception in thread "main" java.lang.StackOverflowError
    at Nodo.<init>(Nodo.java:17)
                              ^ Line number

关于java - 线程“主”中的异常java.lang.StackOverflowError Arbol binario回溯,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44853473/

10-09 07:27