[英]Error in Implementing a graph in java using simple arrays
I have created 3 classes Edge Vertex and Graph. 我创建了3个Edge Edge和Graph类。 The graph is the main class.
该图是主要类别。 The following is the code for the 3 classes:
以下是这三个类的代码:
Edge 边缘
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);
}
} }
Vertex 顶点
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 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);
}
} }
The printGraph()
function shows the error: 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)
I am not able to make the print graph function. 我无法执行打印图形功能。 How do i print all the edges in the graph?
如何在图表中打印所有边缘?
Change printEdge()
method to static 将
printEdge()
方法更改为静态
public static void printEdge(Edge e)
Your printGraph()
method is not correctly implemented. 您的
printGraph()
方法未正确实现。 It should be like this: 应该是这样的:
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); } }
In addEdge()
method, you need to add this also, otherwise edgeNumber will always be -1 在
addEdge()
方法中,您还需要添加它,否则edgeNumber始终为-1
e1.edgeNumber = edgeIndex;
In class Vertex
you have to override toString()
method, otherwise you will not be able to print the vertex's information in printEdge()
correctly. 在
Vertex
类中,您必须重写toString()
方法,否则您将无法在printEdge()
正确打印顶点的信息。
@Override public String toString() { return "Vertex [vertexId=" + vertexId + ", name=" + name + "]"; }
make your printEdge static: 使您的printEdge静态:
public static void printEdge(Edge e){
System.out.println("Edge Number :"+e.edgeNumber);
System.out.println(e.vertex1);
System.out.println(e.vertex2);
System.out.println(e.name);
System.out.println(e.weight);
}
change you printGraph method: 更改您的printGraph方法:
public void printGraph() {
System.out.println("Number of vertices in the graph : " + numVertices);
for (int i = 0; i < numEdges; i++) {
Edge.printEdge(edgeList[i]);
}
}
call it like this: g1.printGraph(); 这样称呼它:g1.printGraph();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.