无法访问toString中的变量

时间:2014-04-19 03:03:27

标签: java tostring dijkstra

我回来了另一个关于我的Dijkstra算法的问题。我修复了上一个问题,但现在我想制作一个toString()方法。

当我尝试制作它时,我使用的变量无法从toString()访问,我不明白为什么。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;

public class Graph
{

ArrayList<Vertex> vertexObjects = new ArrayList<Vertex>();
ArrayList<Edge> edgeObjects = new ArrayList<Edge>();
ArrayList<Vertex> visitedObjects = new ArrayList<Vertex>();
ArrayList<Vertex> unvisitedObjects = new ArrayList<Vertex>();
ArrayList<Edge> tempEdge = new ArrayList<Edge>();
int numVertices = 0;

public void readFile(String textfile)
{
    try {
        Scanner s = new Scanner(new File(textfile));
        String sameVertex = "";

        while (s.hasNext()) {
            String preVertex = s.next();
            String postVertex = s.next();
            String distance = s.next();

            Edge temp = new Edge(preVertex, postVertex, distance);
            edgeObjects.add(temp);

            if (!(preVertex.equals(sameVertex)))
            {
                Vertex herp = new Vertex(preVertex, Double.POSITIVE_INFINITY, false, null);
                vertexObjects.add(herp);
                sameVertex = preVertex;
                numVertices++;
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("I can't find that file!");
    }
}

public void dijkstra(String startVertex, String endVertex)
{


    // Change the distance of the startVertex to 0 and all others to infinity.
    for (int i = (numVertices-1); i >= 0; i--)
    {
        if (vertexObjects.get(i).vertexName.equals(startVertex))
        {
            vertexObjects.get(i).distance = 0;
        } else {
            vertexObjects.get(i).distance = Double.POSITIVE_INFINITY;
        }
    }

    //Set the node with lowest distance value to Current Status
    unvisitedObjects = vertexObjects;
    double smallDistance = Double.POSITIVE_INFINITY;
    while(unvisitedObjects.size() != 0) {
        //set current node to vertex with shortest distance
        String currentNode = "";
        for (int j = (unvisitedObjects.size()-1); j >= 0; j--) {
            if (unvisitedObjects.get(j).distance <= smallDistance) {
                smallDistance = unvisitedObjects.get(j).distance;
                currentNode = unvisitedObjects.get(j).vertexName;
            }
        }
        //remove the smallest distance having node from the unvisited array
        //and place into visited array.
        for (int g = (unvisitedObjects.size()-1); g >= 0; g--) {
            if (unvisitedObjects.get(g).vertexName.equals(currentNode))
            {
                visitedObjects.add(unvisitedObjects.get(g));
                unvisitedObjects.remove(g);
            }
        }
        //for all the nodes that are adjacent to the current node, update their
        //distance values if they are larger than the weight plus previous distances.
        for (int w = (edgeObjects.size()-1); w >= 0; w--) {
            if (edgeObjects.get(w).startVertex == currentNode) {
                tempEdge.add(edgeObjects.get(w));
            }
            for (int t = (tempEdge.size()-1); t >=0; t--) {
                for (int p = (vertexObjects.size()-1); p >= 0; p--) {
                    if (tempEdge.get(t).endVertex == vertexObjects.get(p).vertexName)
                    {
                        if ((Double.parseDouble(tempEdge.get(t).edgeWeight) + smallDistance) < vertexObjects.get(p).distance) {
                            vertexObjects.get(p).distance = (Double.parseDouble(tempEdge.get(t).edgeWeight) + smallDistance);
                        }
                    }
                }
            }   
        }
    }

    String smallDString = Double.toString(smallDistance);


}

public Graph(String textfile, String startingVertex, String endingVertex) {
    String graphFile = textfile;
    String startVertex = startingVertex;
    String endVertex = endingVertex;
}

public String toString() {
    return ("The shortest path from "+startVertex+" to "+endVertex+" is "+smallDistance+".");
}
}

4 个答案:

答案 0 :(得分:0)

你需要在课堂上创建它们。不只是局部变量。例如,在构造函数中,您可以执行以下操作:

private String graphFile;  // declare variable to store value

public Graph(String textfile, String startingVertex, String endingVertex) {
    graphFile = textfile;  // assign value in constructor
    ...

答案 1 :(得分:0)

您无法访问它们,因为它们是在函数中初始化的。您需要将它们声明为全局变量。

ArrayList<Vertex> vertexObjects = new ArrayList<Vertex>();
ArrayList<Edge> edgeObjects = new ArrayList<Edge>();
ArrayList<Vertex> visitedObjects = new ArrayList<Vertex>();
ArrayList<Vertex> unvisitedObjects = new ArrayList<Vertex>();
ArrayList<Edge> tempEdge = new ArrayList<Edge>();
int numVertices = 0;
String startVertex, smallDistance, endVertex = "";

可能是问题。

答案 2 :(得分:0)

您只在public Graph(...)构造函数中声明了变量,而不是在类的其余部分的范围内。

您需要在类体(通常在顶部附近)声明它们,以便方法可以访问它们。

答案 3 :(得分:0)

这是因为您在构造函数中声明并初始化了变量(graphFilestartVertexendVertex,)。这意味着它们只在构造函数中可见/可用。您需要将它们声明为类变量,然后您可以在构造函数中初始化它们。例如

public class Graph {
    String graphFile;

    public Graph(String textfile) {
        this.graphFile = textfile;
    }
}

你也可以为其他人做同样的事情。