Hi guys,

i am working on a Project about mobile Navigation on a lego Mindstorms Robot but i am stuck right now.

I get this error:"The method MinDistanceNode() is undefined for the type ArrayList<Node>"

I have four different Classes. The problem is probablyin "DijkstraAlgorithm" or in the Method "minDistanceNode" which is in the Class Node.

First class DijkstraAlgorithm
package navigation;

import java.util.ArrayList;
import maps.topological.Node;
import maps.topological.TopoMap;

class DijkstraAlgorithm {

    public static void compute(int startNode, TopoMap graph) {

        int numberOfNodes = Node.numberOfNodes;

        int[] distance = new int[numberOfNodes];
        int[] parent = new int[numberOfNodes];
        int[][] weight = new int[numberOfNodes][numberOfNodes];

        // Initialisierung von Distanzen und Vorgaengerknoten
        for (int iNode = 0; iNode < numberOfNodes; ++iNode) {
            distance[iNode] = -1;
            parent[iNode] = -1;
        }
        // Distanz des Startknotens zu sich selbst
        distance[startNode] = 0;
        parent[startNode] = -1;


        // Write all Nodes in a Nodeslist
        ArrayList<Node> nodesUnvisited = graph.getNodes();

        // Dijkstra-Algorithmus: do it until all Nodes are visited
        // and the distances are measured
        while (nodesUnvisited.size() > 0) {

            // get the Node with the smallest distance to your start Node
            int actual = nodesUnvisited.MinDistanceNode();             //Here is the error
            // ... and delete it from the list
            nodesUnvisited.remove(actual);


        }
    }
}
second Class Node
package maps.topological;

import java.util.ArrayList;
import java.util.Iterator;

import lejos.geom.Point;


public class Node {
    public static short numberOfNodes = 0;

    private short number;
    public static int nodeNumber;
    private Point position;
    // ggf. einfache Liste
    private ArrayList<Edge> edges;



    Node(float x, float y) {
        numberOfNodes += 1;
        number = numberOfNodes;
        position = new Point(x, y);
        edges = new ArrayList<Edge>();
    }

    public short getNumber() {
        return number;
    }

    public Point getPosition() {
        return position;
    }





    /**
     * Findet den Nachbarknoten mit der minimalen Distanz
     * 
     * @return Knotenindex dieses Knotens, -1 wenn kein Nachbar existiert
     */
    public int MinDistanceNode() {
        Iterator<Edge> iter = edges.iterator();
        int nodeNumber = -1;
        float lastWeight;

        if (iter.hasNext()) {
            Edge e = iter.next();
            lastWeight = e.weight;
            nodeNumber = e.targetNodeNumber;

            while (iter.hasNext()) {
                e = iter.next();

                if (e.weight < lastWeight) {
                    nodeNumber = e.targetNodeNumber;
                    lastWeight = e.weight;
                }

            }
        }
        return nodeNumber;
    }
}
Third Class TopoMap
package maps.topological;

import java.util.ArrayList;
import java.util.Iterator;

import lejos.geom.Point;

/**
 * Topologische Karte - implementiert durch die Adjazenzlistenrepräsentation
 * eines Graphen
 * 
 */
public class TopoMap {

    public ArrayList<Node> nodes;

    // zulässiger Positionsfehler für das Aufinden von Knoten
    private float error = 0.25f;

    public TopoMap() {
        nodes = new ArrayList<Node>();
    }

    /**
     * @param error
     */
    public TopoMap(float error) {
        nodes = new ArrayList<Node>();
        this.error = error;
    }

    /**
     * Fügt einen Knoten mit den Koordinaten (x,y) ein
     * 
     * @param x
     * @param y
     */
    public void addNode(float x, float y) {
        if (this.getNodeByPos(x, y) == null) {
            nodes.add(new Node(x, y));
        }
    }

    /**
     * 
     * @return sort the List
     */
    public ArrayList<Node> getNodes() {
        ArrayList<Node> ret = new ArrayList<Node>();

        for (short i = 0; i <= nodes.size(); i++) {
            ret.add(i, this.getNodeByNumber(i));
        }

        return ret;
    }

}
fourth Class Edge
package maps.topological;

/**
 * Kante einer topologischen Karte
 */
public class Edge implements Comparable<Edge> {
    short targetNodeNumber;
    float weight;

    Edge(short j, float weight) {
        targetNodeNumber = j;
        this.weight = weight;
    }

    Edge(short toNode) {
        targetNodeNumber = toNode;
        this.weight = 1;
    }

    public void updateWeight(float newWeight) {
        this.weight = newWeight;
    }

    @Override
    public int compareTo(Edge o) {
        // Kanten zu gleichem Knoten sollten nicht doppelt vorkommen
        if (this.targetNodeNumber < o.targetNodeNumber)
            return -1;
        else
            return 1;
    }
}

What am i doing wrong? Please help me!

The method MinDistanceNode() is defined in the Node class, so to call it you need a single instance of Node, not a whole ArrayList of Nodes

Ok so it should rather look like "Node.MinDistanceNode();" shouldn't it?

But how can i call the Method from the Class "Node" and use it on the ArrayList "nodesUnvisited" in my Class "DijkstraAlgorithm"?

No, that's how you call a static method. It should look like someInstanceOfNode.MinDistanceNode()

Since I have no idea what the method does, and it's undocumented, it's hard for me to answer. In terms of syntax it's an easy answer... the method is an instance method of Node and has no parameters, so there's no way you can apply it to an ArrayList.

i think when you are using arraylist,you have to call specific method with object of arraylist describing specific index.

like eg.

nodesunvisited[index].MinDistanceNode();

Also you can iterate it over a loop to get minimum distance node.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.