我创建了3个类Edge Vertex和Graph。
该图是主要类别。
以下是这三个类的代码:

边缘

public class Edge {
Vertex vertex1;
Vertex vertex2;
int weight;
String name;
int edgeNumber;

Edge(){
    weight=-1;
    name=null;
    edgeNumber=-1;
}
Edge(Vertex vertex1,int edgeNumber,Vertex vertex2,int weight,String name){
    this.vertex1=vertex1;
    this.vertex2=vertex2;
    this.weight=weight;
    this.name=name;
    this.edgeNumber=edgeNumber;
}

public void printEdge(Edge e){
    System.out.println("Edge Number :"+edgeNumber);
    System.out.println(e.vertex1);
    System.out.println(e.vertex2);
    System.out.println(e.name);
    System.out.println(e.weight);
}


}

顶点

public class Vertex {
int vertexId;
String name;

Vertex(){
    vertexId=-1;
    name=null;
}
public void createVertex(Vertex ver1,int id,String vName){
    ver1.vertexId=id;
    ver1.name=vName;
}


}

Graph.java

public class Graph {
int numVertices;
int numEdges;
int[][] matrix=new int[20][20];
Vertex[] vertexList =new Vertex[10];
Edge[] edgeList=new Edge[10];
/*
 * @param args the command line arguments
 */
// Intitalise the graph
//All values in matrix are zero
//Vertex names are also taken from name
Graph(int[][] vertexArr,int numVertices){
    this.numVertices=numVertices;

    for(int i=0;i<numVertices;i++){
        for(int j=0;j<numVertices;j++){
            vertexArr[i][j]=0;
        }
    }
}
public int graphDegree(Graph G,int vertex){
    int i,degree=0;
    for(i=0;i<numVertices;i++){
        if(matrix[vertex][i]==1)
            degree++;
    }
    return degree;
}
public void addEdge(Edge e1,int edgeIndex,Vertex v1,Vertex v2,String nameIn,int weightIn){
    int old=matrix[v1.vertexId][v2.vertexId];
    if(old!=0)
        System.out.println("The edge already exists");
    //creating new edge
    matrix[v1.vertexId][v2.vertexId]=1;
    matrix[v2.vertexId][v1.vertexId]=1;
    e1.vertex1=v1;
    e1.vertex2=v2;
    e1.name=nameIn;
    e1.weight=weightIn;
    edgeList[edgeIndex]=e1;
    numEdges++;
}
public void printGraph(Graph G){
    System.out.println("Number of vertices in the graph : "+G.numVertices);
    for(int i=0;i<numEdges;i++){
        G.printEdge(edgeList[i]);
    }

}
public static void main(String[] args) {

    int vertexCount=5;

    Vertex v1=new Vertex();
    Vertex v2=new Vertex();
    Vertex v3=new Vertex();
    Vertex v4=new Vertex();
    v1.createVertex(v1,0,"Delhi");
    v2.createVertex(v2,1,"Noida");
    v3.createVertex(v3,2,"Mumbai");
    v4.createVertex(v4,3,"Kanpur");
    int[][] graphStore=new int[vertexCount][vertexCount];
    Graph g1=new Graph(graphStore,vertexCount);

    Edge edge1=new Edge();
    Edge edge2=new Edge();
    g1.addEdge(edge1,0,v1,v2,"DtoN",10);
    g1.addEdge(edge2,1,v2,v3,"NtoM",50);
    g1.printGraph(g1);
}


}

printGraph()函数显示错误:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: graph.Graph.printEdge
at graph.Graph.printGraph(Graph.java:63)
at graph.Graph.main(Graph.java:86)


我无法执行打印图形功能。如何在图表中打印所有边缘?

最佳答案

printEdge()方法更改为静态

public static void printEdge(Edge e)

您的printGraph()方法未正确实现。应该是这样的:

public void printGraph(Graph G){
System.out.println("Number of vertices in the graph : "+G.numVertices);
for(int i=0;i<numEdges;i++){
Edge.printEdge(e);
  }
}

addEdge()方法中,您还需要添加它,否则edgeNumber始终为-1

e1.edgeNumber = edgeIndex;

在类Vertex中,您必须重写toString()方法,否则将无法正确在printEdge()中打印顶点的信息。

@Override
public String toString() {
return "Vertex [vertexId=" + vertexId + ", name=" + name + "]";
}

10-04 13:10