簡體   English   中英

DAG 的索引越界錯誤

[英]Index out of bounds error for DAG

我正在編寫一個程序,用來自標准輸入的輸入找到 DAG 的最長路徑。我終於編譯了它,它說由於我的數組列表,它正在使用未經檢查或不安全的操作,但我得到了一個索引邊界錯誤,感覺就像我已經嘗試更改每個循環我一定會遺漏一些東西,在此先感謝您提供任何提示。 這是我的代碼:

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

public class countLongPaths
{
static final int NINF = Integer.MIN_VALUE;
public class AdjListNode
{
    private int v;
    private int weight;
    AdjListNode(int inV, int inW) 
        { 
            v = inV;  
            weight = inW; 
        }
    int getV() 
        { 
            return v; 
        }
    int getWeight()  
        { 
            return weight; 
        }
}//end of adj list class

public class Graph
    {
        private int V;
        private LinkedList<AdjListNode>adj[];
        //set up graph with given number of verticies
        Graph(int v)
        {
            V=v;
            adj = new LinkedList[V];
            for (int i = 0; i < v; ++i)
                adj[i] = new LinkedList<AdjListNode>();
        }
        //function to add edges to graph
        void addEdge(int u, int v, int weight)
        {
            AdjListNode node = new AdjListNode(v,weight);
            adj[u].add(node);// Add v to u's list
        }

        //function to set order to go through vertices
        void setOrder(int v, Boolean visited[], Stack stack)
        {
            //Set node to visited when on it
            visited[v] = true;
            Integer i;

            //for all nodes connected to current repeat
            Iterator<AdjListNode> it = adj[v].iterator();
            while (it.hasNext())
            {
                AdjListNode node =it.next();
                if (!visited[node.getV()])
                    setOrder(node.getV(), visited, stack);
            }
            //Once done with current add it to the stack
            stack.push(new Integer(v));
        }
        //function to find longest paths from s
        int longestPath()
        {
            Stack stack = new Stack();
            int LP[] = new int[V];
            //set all vertices to unvisited
            Boolean visited[] = new Boolean[V];
            for(int i = 1; i <= V; i++)
                visited[i] = false;
            //call set order function from each vertex
            for (int i = 1; i <= V; i++)
            {
                if(visited[i] == false)
                    setOrder(i, visited, stack);
            }
            //initialize distaces to all verices as negative infinity
            //set distace to source to 0
            LP[1] = 0;
            for(int i = 2; i <= V; i++)
                LP[i] = NINF;
            //go through vertices in order
            while(stack.empty() == false)
                {
                    int u = (int)stack.pop();
                    //update LP for adj vertices
                    Iterator<AdjListNode> it;
                    if (LP[u] != NINF)
                    {
                        it = adj[u].iterator();
                        while (it.hasNext())
                        {
                            AdjListNode i = it.next();
                            if(LP[i.getV()] < LP[u] + i.getWeight())
                                LP[i.getV()] = LP[u] + i.getWeight();
                        }
                    }
                }
                return LP[V];

        }
    }//end of graph class

    //Method to make a new graph
    public Graph newGraph(int number)
    {
        return new Graph(number);
    }

public static void main(String[]args)
{
    countLongPaths n = new countLongPaths();
    int GN = 0;
    int count = 1;
    Scanner scan = new Scanner(System.in);
    GN = scan.nextInt();
    while (count<= GN)
    {
        int N = 0;// nodes
        int M = 0;//edges

        N = scan.nextInt();
        M = scan.nextInt();

        //setup a new graph
        Graph g = n.newGraph(N);
        //set edges for new graph
        for(int i = 1; i <= M; i ++)
        {
            int I = scan.nextInt();
            int J = scan.nextInt();
            int W = scan.nextInt();
            g.addEdge(I, J, W);
        }
        int dist = 0;
        dist = g.longestPath();
        System.out.println("graph number: " + count);
        System.out.println("longest path: " + dist);
        System.out.println("number of longest paths: ");
        System.out.println();
        count++;
    }//end of while

}//end main
}//end program

使用當前代碼編輯 1這是錯誤:

  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at countLongPaths$Graph.<init>(countLongPaths.java:36)
at countLongPaths.newGraph(countLongPaths.java:108)
at countLongPaths.main(countLongPaths.java:127)

正如您的堆棧跟蹤所說,異常發生在您的Graph類構造函數中。

更具體地說,它發生在循環中的唯一行內:

adj = new LinkedList[V];
for (int i = 0; i <= v; ++i)
    adj[i] = new LinkedList<AdjListNode>();

假設你的意思是小寫v和大寫V都是同一個變量,你正在定義一個大小為V的數組,它的索引從0V-1 ,但你正在從0V (你的條件is i <= V ),這就是為什么你得到一個IndexOutOfBoundsException

只需更改循環的條件(刪除= ):

for (int i = 0; i < v; ++i)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM