“线程中的异常”主“java.Lang.NullPointerException”错误

时间:2014-08-15 15:28:56

标签: java algorithm

我正在编写关于java中prim的算法扩展的程序。我正在使用以下书籍算法,第4版中的代码。  我的问题如下: 在运行时,for循环读取所有边缘直到最后一个循环才循环,它只读取第一行 运行程序后,我得到以下结果和错误:

0 0.71000 1 0.46000 2 0.80000 3 0.36000 4 0.26000 5 0.57000 6 0.68000 7 0.85000 16 0 4 五 0.35 4-5 0.35000

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:4

at EdgeWeightedGraph.addEdge(EdgeWeightedGraph.java:207)

at EdgeWeightedGraph.<init>(EdgeWeightedGraph.java:152)

at PrimMST.main(PrimMST.java:174)

这是文件。感谢

import java.util.*;
import java.io.*;

public class EdgeWeightedGraph {
    private final int V;
    //private double VertexCost;
    private int E;
  //private  int degree;
    private Bag<Edge>[] adj;
    private ArrayList<Vertex> vertex;
 //private ArrayList<Edge>[] adj;

    /**
     * Initializes an empty edge-weighted graph with <tt>V</tt> vertices and 0 edges.
     */
    public EdgeWeightedGraph( int V ) {
         //vertex = new  ArrayList<Vertex>();
        if (V < 0) throw new IllegalArgumentException("Number of vertices must be nonnegative");
    //if (VertexCost < 0) throw new IllegalArgumentException("The cost of the vertices must be nonnegative");
        this.V = V;
    // (Bag<Edge>[])
        this.E = 0;
       adj = (Bag<Edge>[]) new Bag[V];
     //vertex = new  ArrayList<Vertex>();
        for (int v = 0; v < V; v++) {
            adj[v] = new Bag<Edge>();
    // System.out.println(adj[v]);

        }
    }

    /**
     * Initializes a random edge-weighted graph with <tt>V</tt> vertices and <em>E</em> edges.
     * param V the number of vertices
     * param E the number of edges
     */
    public EdgeWeightedGraph(int V, int E) {
        this(V);
        if (E < 0) throw new IllegalArgumentException("Number of edges must be nonnegative");

    // for (int j = 0; j < V; j++) {
       // double cost = Math.round(100 * Math.random()) / 100.0;
    //Vertex u = new Vertex(v,cost);
         //vertex.add(u);
    //}

        for (int i = 0; i < E; i++) {
            int v = (int) (Math.random() * V);
            int w = (int) (Math.random() * V);
            double weight = Math.round(100 * Math.random()) / 100.0;
            Edge e = new Edge(v, w, weight);


            addEdge(e);
        }
    }

    /**  

     * Initializes an edge-weighted graph from an input stream.

     */
    public EdgeWeightedGraph(In in) {
       // this(in.readInt());
        vertex = new  ArrayList<Vertex>();
     adj = (Bag<Edge>[]) new Bag[vertex.size()];
    V = in.readInt();
    E = in.readInt();
    //System.out.println(adj);
        //System.out.println(E);
    for (int j =0;j < V; j++){
    int v = in.readInt();
      //  System.out.println(v);
     double cost = in.readDouble();
        Vertex u = new Vertex(v, cost);
       // System.out.println( cost);
    vertex.add(u);    //addVertex(v)
    System.out.println(u);

    }   

    if (E < 0) throw new IllegalArgumentException("Number of edges must be nonnegative");


    System.out.println(E);
    for (int i = 0; i < E; i++) {

        System.out.println(i);
           int v = in.readInt();
        System.out.println(v);

            int w = in.readInt();
        System.out.println(w);
            double weight = in.readDouble();  
     System.out.println(weight);          
          Edge e = new Edge(v, w, weight);
     //System.out.println(e);
         addEdge(e);



        }

        }

    /**
     * Initializes a new edge-weighted graph that is a deep copy of <tt>G</tt>.

     */
    public EdgeWeightedGraph(EdgeWeightedGraph G) {
        this(G.V());
        this.E = G.E();
        for (int v = 0; v < G.V(); v++) {
            // reverse so that adjacency list is in same order as original
            Stack<Edge> reverse = new Stack<Edge>();
            for (Edge e : G.adj[v]) {
                reverse.push(e);
            }
            for (Edge e : reverse) {
                adj[v].add(e);
            }
        }
    }


    /**
     * Returns the number of vertices in the edge-weighted graph.

     */
    public int V() {
        return V;
    }

    /**
     * Returns the number of edges in the edge-weighted graph.

    public int E() {
        return E;
    }

    /**
     * Adds the undirected edge <tt>e</tt> to the edge-weighted graph.
     * 
     */
    public void addEdge(Edge e) {
        int v = e.either();
        int w = e.other(v);

        if (v < 0 || v >= V) throw new IndexOutOfBoundsException("vertex " + v + " is not between 0 and " + (V-1));
        if (w < 0 || w >= V) throw new IndexOutOfBoundsException("vertex " + w + " is not between 0 and " + (V-1));
        adj[v].add(e);
        adj[w].add(e);
        E++;

    }

 /**  
     * Returns the edges incident on vertex <tt>v</tt>.

     */
    public Iterable<Edge> adj(int v) {
        if (v < 0 || v >= V) throw new IndexOutOfBoundsException("vertex " + v + " is not between 0 and " + (V-1));
        return adj[v];
    }
//return the vertex degree at each vertex in the graph
    public int VertexDegree(){
     int VertexDegree=0;

    for ( int v =0; v<V; v++){
       for ( Edge e : adj[v]){
        //for(int i=0; i <= adj[v];i++) {
        // degree+=1;
    VertexDegree = adj[v].size();

         }
         }
         return VertexDegree();
    }



    /**
     * Returns all edges in the edge-weighted graph.

     */
    public Iterable<Edge> edges() {
        Bag<Edge> list = new Bag<Edge>();
        for (int v = 0; v < V; v++) {
            int selfLoops = 0;
            for (Edge e : adj(v)) {
                if (e.other(v) > v) {
                    list.add(e);
                }
                // only add one copy of each self loop (self loops will be consecutive)
                else if (e.other(v) == v) {
                    if (selfLoops % 2 == 0) list.add(e);
                    selfLoops++;
                }
            }
        }
        return list;
    }

    /**
     * Returns a string representation of the edge-weighted graph.
         */
    public String toString() {
        String NEWLINE = System.getProperty("line.separator");
        StringBuilder s = new StringBuilder();
        s.append(V + " " + E + NEWLINE);
        for (int v = 0; v < V; v++) {
            s.append(v + ": ");
            for (Edge e : adj[v]) {
                s.append(e + "  ");
            }
            s.append(NEWLINE);
        }
        return s.toString();
    }

1 个答案:

答案 0 :(得分:1)

public EdgeWeightedGraph(In in) {
    vertex = new  ArrayList<Vertex>();
    adj = (Bag<Edge>[]) new Bag[vertex.size()];
    V = in.readInt();
    E = in.readInt();
    for (int j =0;j < V; j++){
        int v = in.readInt();
        double cost = in.readDouble();
        Vertex u = new Vertex(v, cost);
        vertex.add(u);
    }
    /* Snipped */
}

此构造函数未正确初始化adj。由于vertex.size()返回0,adj的大小为零。 V可能有一个不相关的值,因此您对错误的检查不会捕获它,抛出您看到的异常。我假设这是出了什么问题。