Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Nevermind, figured it out.

  • 09-03-2012 08:04AM
    #1
    Closed Accounts Posts: 816 ✭✭✭


    OK so I'm doing a project for college at the moment and I'm tearing my hair out over this algorithm I've devised. It finds all possible paths in a directed graph from sources to destinations.

    Anyway I know it's working cos I'm printing my paths (linkedlists) as I go along, however I can't store them as lists in an array of lists.

    //This line just checks that it has reached a terminal node then saves the List to savedPaths[]
    
    if (x.getConnections(pathList.getLast()).isEmpty()){
                    
                    try{                  
                        //Something wrong here, won't add pathList to savedPaths[a]?
                    savedPaths[a]= pathList;
                    //savedPaths[a].addAll(pathList);
                    System.out.println("savedPaths:");
                    System.out.println(savedPaths[a].toString());
                    a++;
                    }
                    catch(Exception e){
                        System.out.println(e.toString());
                    }
                    
                    
                }
    

    savedPaths is just an array of LinkedLists. I've tried adding pathList both ways as you can see (using addAll and = ). It works with the = but then if I print all of the savedPaths all I get is the last path in every place?

    Any ideas?

    Help appreciated!:D


Comments

  • Closed Accounts Posts: 816 ✭✭✭Opinicus


    I just realised it's probably because I'm referencing the pathList instead of copying it. As it changes so does the savedPath value. How do I get around this?


  • Closed Accounts Posts: 816 ✭✭✭Opinicus


    Nevermind fixed it using clone();


    savedPaths[a] = (LinkedList<Integer>) pathList.clone();:cool:


Advertisement