title: A* Algorithm Java
body: <p>I have spent entire weekend playing around with this. I am trying to store the nodes in PriorityQueue data structure. My astar function doesnt seem to be doing what it should. Anyone mind having a look?</p>
  
  <pre><code>public void aStar(Node from, Node to) {
  
      PriorityQueue&lt;Node&gt; exploreList = new PriorityQueue&lt;Node&gt;();
      ArrayList&lt;Node&gt; visited = new ArrayList&lt;Node&gt;();
      ArrayList&lt;Node&gt; successors = new ArrayList&lt;Node&gt;();
  
      Node current = from;
      System.out.println(current.getName());
      while (current != to) {
              successors = current.getConnected();
              Collections.sort(successors);
              for (Node n : successors) {
                      if (!visited.contains(n)) {
                              exploreList.add(n);
                      }
                      for (Node n1 : successors) {
                              if (n.fSum() &gt; n1.fSum()) {
                              exploreList.remove(n);
                              exploreList.add(n1);
                              }      
                      }
              }
              visited.add(current);
              current = exploreList.remove();
              System.out.println(current.getName());
      }
  </code></pre>
  
  <p>Node Class here</p>
  
  <pre><code>   public class Node implements Comparable {
     private String name;
     private int travDist;
     private int straightDist;
     private ArrayList&lt;Arc&gt; arcs;
  
  /**
   * Constructor for a new node
   * 
   * @param n
   */
     public Node(String n, int aTravDist, int aStraightDist) {
         name = n;
         travDist = aTravDist;
         straightDist = aStraightDist;
  
         arcs = new ArrayList&lt;Arc&gt;();
    }
  
  /**
   * Adds a new arc
   * 
   * @param to
   * @param c
   */
  public void addArc(Node to, int c) {
      arcs.add(new Arc(to, c));
  }
  
  /**
   * Gets the list of connected nodes to this node
   * 
   * @return
   */
  public ArrayList&lt;Node&gt; getConnected() {
      ArrayList&lt;Node&gt; returnData = new ArrayList&lt;Node&gt;();
      for (Arc a : arcs) {
          returnData.add(a.getNode());
      }
      return returnData;
  }
  
  @Override
  public int compareTo(Object o) {
      //return name.compareTo(((Node) o).getName());
      Integer sum = ((Node)o).fSum();
      return sum.compareTo(fSum());
  }
  
  public int fSum () {
  
      return travDist + straightDist;
  }
  
  /**
   * Gets the name of the Node
   * 
   * @return
   */
  public String getName() {
      return name;
  }
  
  
  }
  </code></pre>