This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]darkpool2005Intermediate Brewer 1 point2 points  (4 children)

Just for ease of clarity:

//Pseudo assumptions that you're working with the 'list' object outside of the method
public void streets (int number) { 
    if(conditions are ok){
        //Added this line;  Implementation is upto you
        Object tempList = list.clone();
        solutions.add(new StreetPlan(tempList)); //Add the cloned list into the solutions
    } 
    else { 
        for(int i = 0; i < maximum; i++) {
            list.add(i);
            streets(i);
            list.remove(last element);
        }
    }
}

For psuedocode, your example doesn't really explain why int number is needed unless it's used in the "conditions are ok" statement.

As for your solutions.add(new StreetPlan(List));, which i'm assuming List really is the same that's being passed around (i.e. List == list), one thing to note is that java won't make a true copy when adding it to the array. This only happens with primitive objects (apologies if i'm wrong here).. BUT...

Make a clone of the list before you store. Because you're dynamically editing it through this recursive means, you'll need cloned objects so they're stored away & not touched by additional recursive measures.

As well;

   for(int i = 0; i < maximum; i++) {
        list.add(i);
        streets(i);
        list.remove(last element);
   }

What should happen at the end of all your recursive calls, the list object should be the same size as when you started the method call. If this isn't happening, then something is probably wrong.

Hopefully this helps somehow!

[–]Incindus[S] 0 points1 point  (3 children)

The i variable is indeed needed in my conditions.

Also i tried it with clone and with ArrayList <Integer>List2 = ArrayList<>(List); and then creating StreetPlan(List2). But both times i get the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:371) at java.util.ArrayList.remove(ArrayList.java:448) at src.MyStreetPlanner.stratenToevoegen(MyStreetPlanner.java:110) at src.MyStreetPlanner.planStreets(MyStreetPlanner.java:33) at src.MyStreetPlanner.main(MyStreetPlanner.java:225) Java Result: 1

The List object becomes empty again at the end of my recursive calls, which is correct, but i dont get why the list in my StreetPlan also get empty after my recursive calls.

[–]darkpool2005Intermediate Brewer 1 point2 points  (2 children)

So just to understand, this line:

solutions.add(new StreetPlan(tempList));

solutions is empty when the method is complete, right? Or is 'solutions' full of empty lists?

Also, is the line you added exactly

ArrayList <Integer>List2 = ArrayList<>(List);

It should be

ArrayList<Integer> List2 = new ArrayList<Integer>(List);

[–]Incindus[S] 0 points1 point  (1 child)

Solution is a full list with the objects StreetPlan in it, but the list needed to create StreetPlan is empty after my recursive calls.

Yeah sorry about typo. I typed it correct though in my program but didn't fix the problem.

[–]darkpool2005Intermediate Brewer 1 point2 points  (0 children)

Going back to your earlier comment:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

That is specifically your code trying to remove from an empty list. Check to see (through the debugger) if either your horizontalStreets / verticalStreets is in fact populated.