链接列表通用数组创建错误无向图Java

时间:2017-10-22 05:40:19

标签: java linked-list breadth-first-search undirected-graph

所以我试图使用java在无向图中寻找路径。我觉得我可能在正确的轨道上,非常接近它,然而,我一直跑到路障并得到一个错误说。我使用链表来存储边和顶点。

  

Graph2.java:17:错误:通用数组创建           adj = new LinkedList [v];

这是我的代码

import java.io.*;
import java.util.*;
import java.util.LinkedList;
import java.util.Scanner;

// This class represents a directed graph using adjacency list
// representation
class Graph2
{
    private int V;   // No. of vertices
    private LinkedList<Integer> adj[]; //Adjacency List

    //Constructor
    Graph2(int v)
    {
        V = v;
        adj = new LinkedList<Integer>[v];
        for (int i=0; i<v; ++i)
            adj[i] = new LinkedList<Integer>();
    }

    //Function to add an edge into the graph
    void addEdge(int v,int w)  {   adj[v].add(w);   }

    //prints BFS traversal from a given source s
    Boolean isReachable(int s, int d)
    {
        LinkedList<Integer>temp;

        // Mark all the vertices as not visited(By default set
        // as false)
        boolean visited[] = new boolean[V];

        // Create a queue for BFS
        LinkedList<Integer> queue = new LinkedList<Integer>();

        // Mark the current node as visited and enqueue it
        visited[s]=true;
        queue.add(s);

        // 'i' will be used to get all adjacent vertices of a vertex
        Iterator<Integer> i;
        while (queue.size()!=0)
        {
            // Dequeue a vertex from queue and print it
            s = queue.poll();

            int n;
            i = adj[s].listIterator();

            // Get all adjacent vertices of the dequeued vertex s
            // If a adjacent has not been visited, then mark it
            // visited and enqueue it
            while (i.hasNext())
            {
                n = i.next();
                System.out.println(n);
                // If this adjacent node is the destination node,
                // then return true
                if (n==d)
                    return true;

                // Else, continue to do BFS
                if (!visited[n])
                {
                    visited[n] = true;
                    queue.add(n);
                }
            }
        }

        // If BFS is complete without visited d
        return false;
    }

    // Driver method
    public static void main(String args[])
    {
        // Create a graph given in the above diagram
        Graph g = new Graph(9999999);
        try{
        File file = new File("Graph.txt");
        Scanner scan = new Scanner(file);
        while(scan.hasNextLine())
        {
            String [] tempArray = scan.nextLine().split(",");
            g.addEdge(Integer.parseInt(tempArray[0]), Integer.parseInt(tempArray[1]));
        }
        scan.close();
    }
    catch(FileNotFoundException fs)
    {

    }

        int u = 1;
        int v = 891950;
        if (g.isReachable(u, v))
            System.out.println("There is a path from " + u +" to " + v);
        else
            System.out.println("There is no path from " + u +" to " + v);;
    }
}

1 个答案:

答案 0 :(得分:1)

声明此变量的正确方法是:

private LinkedList<Integer> adj;

初​​始化:

adj = new LinkedList<Integer>();

要将元素放入此列表,您可以使用:

adj.add(index, element);

元素可能是LinkedList对象,但您应该在声明时将泛型类型更改为LinkedList 阅读有关Java docs中的LinkedList的更多信息。