Recursive method with ArrayList in java

5

Good morning, I am in the middle of a problem with an exercise of a task.

I have a class like this:

UML of the class

And I have to put together a recursive public method, which returns an ArrayList with the total number of sub sectors (taking into account that each sub sector can have sub sectors as well)

The class (in addition to the get and set methods that do not paste them to avoid filling so much code)

public class Sector {

private int numero;
private String denominacion;
private String tipo;

private ArrayList<Sector> sectores = new ArrayList<>();   

public Sector(int numero, String denominacion, String tipo) {
    this.numero = numero;
    this.denominacion = denominacion;
    this.tipo = tipo;
}

This is the method code

public ArrayList<Sector> obtenerTotalSubsectores(Sector sector, ArrayList<Sector> sectores) {
    sectores.add(this);
        if (sector.getSectores() != null) {
            for(Sector sector1 : sector.getSectores()) {
                obtenerTotalSubsectores(sector1, sectores);
            }
        }        
    return sectores;
}

and the main code

    Sector s1 = new Sector(100, "sales", "sales");
    Sector s1_1 = new Sector (101, "minor sales", "minor");
    Sector s1_2 = new Sector (102, "mayor sales", "mayor");
    Sector s1_2_1 = new Sector (102, "lala sales", "lalala");

    s1.getSectores().add(s1_1);
    s1.getSectores().add(s1_2);
    s1_2.getSectores().add(s1_2_1);

    Sector s2 = new Sector(200, "Sistemas", "sistemas");        
    Sector s2_1 = new Sector(200, "Soporte Técnico", "soporte");

    s2.getSectores().add(s2_1);
    s1.getSectores().add(s2);   

    s1.obtenerTotalSubsectores(s1, s1.getSectores());

and the error that throws me

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at ejercicio_c_actividad.Sector.obtenerTotalSubsectores(Sector.java:79)
at ejercicio_c_actividad.Main.main(Main.java:61)
C:\xxx\xxx\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

Any idea what I'm doing wrong?

    
asked by Tom 18.08.2016 в 05:54
source

1 answer

2

SOLUTION TO YOUR CODE

You have two failures, first of all, although it would be the right thing to do, you can not add this to each recursion, because the method is executed by Sector and you pass another one !!!! This will add the same to each round, ignoring the sector to process itself. So:

public ArrayList<Sector> obtenerTotalSubsectores(Sector sector, ArrayList<Sector> sectores) {
    sectores.add(sector);
    if (sector.getSectores() != null) {
        for (Sector sector1 : sector.getSectores()) {
            obtenerTotalSubsectores(sector1, sectores);
        }
    }
    return sectores;
}

The second is that you send a ArrayList of sectors full, which does not have much logic, you must create a new object to fill, modifying your main:

ArrayList<Sector> sectores = new ArrayList<Sector>();
s1.obtenerTotalSubsectores(s1, sectores);


System.out.println("TOTAL SECTORES EN EL " + s1.getNumero());
for (Sector s : sectores) {
    System.out.println(s.toString());
}

sectores.clear();
s2.obtenerTotalSubsectores(s2, sectores);

System.out.println("\n\nTOTAL SECTORES EN EL " + s2.getNumero());
for (Sector s : sectores) {
    System.out.println(s.toString());
}

We have this output (I have added a toString() standard to check the outputs more easily)

TOTAL SECTORES EN EL 100
Sector [numero=100, denominacion=sales, tipo=sales, sectores=[Sector [numero=101, denominacion=minor sales, tipo=minor, sectores=[]], Sector [numero=102, denominacion=mayor sales, tipo=mayor, sectores=[Sector [numero=102, denominacion=lala sales, tipo=lalala, sectores=[]]]], Sector [numero=200, denominacion=Sistemas, tipo=sistemas, sectores=[Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]]]]]
Sector [numero=101, denominacion=minor sales, tipo=minor, sectores=[]]
Sector [numero=102, denominacion=mayor sales, tipo=mayor, sectores=[Sector [numero=102, denominacion=lala sales, tipo=lalala, sectores=[]]]]
Sector [numero=102, denominacion=lala sales, tipo=lalala, sectores=[]]
Sector [numero=200, denominacion=Sistemas, tipo=sistemas, sectores=[Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]]]
Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]


TOTAL SECTORES EN EL 200
Sector [numero=200, denominacion=Sistemas, tipo=sistemas, sectores=[Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]]]
Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]

CORRECT SOLUTION

Now, clarified that you are failing, I think you are not using the recursion correctly, I mean, since the method has the Sector , you do not have to pass it to the method every time, since it will be executed within the same, you only need to pass the list to fill.

So as not to get involved, or to get involved, let's see the code:

public ArrayList<Sector> obtenerTotalSubsectores(ArrayList<Sector> sectores) {
    sectores.add(this);
    if (this.getSectores() != null) {
        for (Sector sector1 : this.getSectores()) {
            sector1.obtenerTotalSubsectores(sectores);
        }
    }
    return sectores;
}

Now that you can add this since the recursion is running correctly from the sector processed at that moment, downloading the method of an attribute and clarifying the code (although increasing the abstraction)

The same main modified would be like this:

ArrayList<Sector> sectores = new ArrayList<Sector>();
s1.obtenerTotalSubsectores(sectores);


System.out.println("TOTAL SECTORES EN EL " + s1.getNumero());
for (Sector s : sectores) {
    System.out.println(s.toString());
}

sectores.clear();
s2.obtenerTotalSubsectores(sectores);

System.out.println("\n\nTOTAL SECTORES EN EL " + s2.getNumero());
for (Sector s : sectores) {
    System.out.println(s.toString());
}

And it would give us the same result:

TOTAL SECTORES EN EL 100
Sector [numero=100, denominacion=sales, tipo=sales, sectores=[Sector [numero=101, denominacion=minor sales, tipo=minor, sectores=[]], Sector [numero=102, denominacion=mayor sales, tipo=mayor, sectores=[Sector [numero=102, denominacion=lala sales, tipo=lalala, sectores=[]]]], Sector [numero=200, denominacion=Sistemas, tipo=sistemas, sectores=[Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]]]]]
Sector [numero=101, denominacion=minor sales, tipo=minor, sectores=[]]
Sector [numero=102, denominacion=mayor sales, tipo=mayor, sectores=[Sector [numero=102, denominacion=lala sales, tipo=lalala, sectores=[]]]]
Sector [numero=102, denominacion=lala sales, tipo=lalala, sectores=[]]
Sector [numero=200, denominacion=Sistemas, tipo=sistemas, sectores=[Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]]]
Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]


TOTAL SECTORES EN EL 200
Sector [numero=200, denominacion=Sistemas, tipo=sistemas, sectores=[Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]]]
Sector [numero=201, denominacion=Soporte Técnico, tipo=soporte, sectores=[]]
    
answered by 18.08.2016 / 09:25
source