Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String[] findLink(String fW,String sW){
- Node lastNode=new Node(fW);
- Node goalNode=new Node(sW);
- while(true){
- ArrayList<Node> traversedNodes=new ArrayList<Node>(); // Instantiating a list of nodes it's been through, new for each iteration of the loop
- traversedNodes.add(lastNode); // Adding the last Node
- lastNode.getConnections(); // Getting the connections of the last Node
- for(Connector connection:lastNode.getConnections()){ // Testing the last Node'sconnections
- if(lastNode==connection.getOne()) lastNode=connection.getOne(); // For each connection will get the other end of connector
- else if(lastNode==connection.getTwo()) lastNode=connection.getTwo();
- if(lastNode.getWord()==goalNode.getWord()) break;
- }
- if(lastNode!=goalNode){
- String[] links=new String[traversedNodes.size()]; // Converting ArrayList into String[]
- for(int i=0;i==traversedNodes.size();i++){
- links[i]=traversedNodes.get(i).getWord();
- }
- return links;
- }
- }
- }
Add Comment
Please, Sign In to add comment