方法main(String [])的代码超过65535字节限制

时间:2015-10-15 08:20:41

标签: java dijkstra

所以我需要使用Java Dijkstra算法。我发现here是Dijkstra的一个实例。但在我的情况下,我有大约5000个顶点。我得到错误:方法main(String [])的代码超过65535字节限制。 stackoverflow上有一些这样的主题,但我找不到实现如何解决这个问题。有人可以给我一些关于如何解决这个问题的代码。

import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

class Vertex implements Comparable<Vertex>
{
    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; }
}

public class Dijkstra
{
    public static void computePaths(Vertex source)
    {
        source.minDistance = 0.;
        PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
    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 < v.minDistance) {
            vertexQueue.remove(v);

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

    public static List<Vertex> getShortestPathTo(Vertex target)
    {
        List<Vertex> path = new ArrayList<Vertex>();
        for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
            path.add(vertex);

        Collections.reverse(path);
        return path;
    }

    public static void main(String[] args)
    {
        // mark all the vertices 
        Vertex X1 = new Vertex("A");
        //...till Vertex X5000..

        // set the edges and weight
        X1.adjacencies = new Edge[]{ new Edge(X2, 8) };
        //...till X5000.adjacencies...    

        computePaths(X1); // run Dijkstra
        System.out.println("Distance to " + X5 + ": " + X5.minDistance);
        List<Vertex> path = getShortestPathTo(X5);
        System.out.println("Path: " + path);
    }
}

修改

我正在尝试从MySQL表中获取数据,但是我在声明顶点时遇到了问题。

String query = "SELECT * FROM coordinates";

Statement st = conn.createStatement();         
ResultSet rs = st.executeQuery(query);      
while (rs.next())
{
  int id = rs.getInt("id");
  String vert = Integer.toString(id);

  //Which approach will work?        
  Vertex vert = new Vertex(vert);          
}
st.close();  

1 个答案:

答案 0 :(得分:3)

您是否在代码中对图表的定义进行了硬编码?不要那样做。请改为从数据文件中读取顶点和边。