Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Nevermind, figured it out.

Options
  • 09-03-2012 9: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