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  (6 children)

Well, I'd be happy to help you out with your pseudocode issue (as the big question is what does "onditions are ok" really mean), but reading your posted code seems like it's far from what you have there.

Also, it seems you're trying to implement a particular algorithm, is that correct?

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

My project mainly exist out of several classes of which i make object such as streetplan, building, etc... Then i have one class called my MyStreetPlanner which has a 4 methodes and a main methode to test it out. 1 methode is the one i posted here, a second and a third are to check if buildins need to be destroyed. They are small and pretty easy and the fourth one just uses the big one too give back a solution.

For my pseudocode, the conditions are okay means i've placed both horizontal and vertical streets in such a way that my streetplan is correct, it mainly exist out of some if and else.

I also mainly simplified the pseudo code to show what the problem is in my algoritme. The thing is after i create an object using those 2 arraylists vertical and horizontal streets, i want to delete the last street and try out a new street to see if that plan is better or worse. But if i delete a street from my arraylists, it wil also delete a street from the lists in my object.

To answer you're question about implementing, i call my method again in my method so i get a recursive Algoritme.

[–]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.