Dijkstra's algorithm Java Add on adjacencies
<pre><code>import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Dijkstra {

public static void computePaths(Vertex source) {
    source.minDistance = 0.;
    PriorityQueue&lt;Vertex&gt; vertexQueue = new PriorityQueue&lt;Vertex&gt;();
    vertexQueue.add(source);

    while (!vertexQueue.isEmpty()) {
        Vertex u = vertexQueue.poll();

        // Visit each edge exiting u
        for (Edge e : u.adjacencies) {
            Vertex v = e.target;
            double weight = e.weight;
            double distanceThroughU = u.minDistance + weight;
            if (distanceThroughU &lt; v.minDistance) {
                vertexQueue.remove(v);

                v.minDistance = distanceThroughU;
                v.previous = u;
                vertexQueue.add(v);
            }
        }
    }
}

public static List&lt;Vertex&gt; getShortestPathTo(Vertex target) {
    List&lt;Vertex&gt; path = new ArrayList&lt;Vertex&gt;();
    for (Vertex vertex = target; vertex != null; vertex = vertex.previous) {
        path.add(vertex);
    }

    Collections.reverse(path);
    return path;
}

    Vertex A = new Vertex("A");
    Vertex B = new Vertex("B");
    Vertex D = new Vertex("D");
    Vertex J = new Vertex("J");
    Vertex M = new Vertex("M");

    // set the edges and weight
    A.adjacencies = new Edge[]{new Edge(M, 8), new Edge(D, 11)};
    M.adjacencies = new Edge[]{new Edge(A, 8), new Edge(J, 10)};
    D.adjacencies = new Edge[]{new Edge(A, 11), new Edge(J, 5)};
    J.adjacencies = new Edge[]{new Edge(D, 5), new Edge(M, 10)};

    computePaths(J); // run Dijkstra
    System.out.println("Distance to " + A + ": " + A.minDistance);
    List&lt;Vertex&gt; path = getShortestPathTo(A);
    System.out.println("Path: " + path);
}
}

class Vertex implements Comparable&lt;Vertex&gt; {

public final String name;
public Edge[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;

public Vertex(String argName) {
    name = argName;
}

public String toString() {
    return name;
}

public int compareTo(Vertex other) {
    return Double.compare(minDistance, other.minDistance);
}

}

class Edge {

public final Vertex target;
public final double weight;

public Edge(Vertex argTarget, double argWeight) {
    target = argTarget;
    weight = argWeight;
}
}
</code></pre>

<p>Is there a way to add on to the adjacencies?
For example:</p>

<pre><code>A.adjacencies = new Edge[]{new Edge(M, 8)};
A.adjacencies = new Edge[]{new Edge(D, 11)};
</code></pre>

<p>Basically I want to check if A.adjacencies has any edge and if it does add the second line as a new edge without overwriting the first line.</p>

<p>Is there a way to do it? If there isn't, is there a way to work around this problem?</p>