Graph类型不能是Graphm的超级接口;超级接口必须是接口

时间:2019-07-09 02:38:41

标签: java

我在做什么错了,我该如何解决?

这是文本中的示例:

  

请注意,书中的邻接矩阵图实现(图11.6,第11.2节)没有提供实际创建图的方法。实现11.6算法并对其进行扩展,以便用户可以从文件中读取图形描述。然后,setEdge函数可以使用此数据来构建图形。提供一个“演示”不同图形功能的主程序。

因此,我尝试了多次尝试来解决该问题,但是,我认为总体问题是我只是不完全了解如何正确编写Graph类。为了反映将其称为公共推论的必要更改,当我尝试执行此代码时,代码会造成各种混乱。老实说,我觉得我可能错过了一些小东西,但是经过4个小时的辛苦工作,我选择去社区寻求我的错误的根源以及重写Graph类的正确方向, 。

这是文本提供的代码:

class Graphm implements Graph {
private int[][] matrix; // The edge matrix
private int numEdge; // Number of edges
public int[] Mark; // The mark array
public Graphm() {} // Constructors
public Graphm(int n) {
Init(n);
}
public void Init(int n) {
Mark = new int[n];
matrix = new int[n][n];
numEdge = 0;
}
public int n() { return Mark.length; } // # of vertices
public int e() { return numEdge; } // # of edges
/** @return v's first neighbor */
public int first(int v) {
for (int i=0; i
if (matrix[v][i] != 0) return i;
return Mark.length; // No edge for this vertex
}
/** @return v's next neighbor after w */
public int next(int v, int w) {
for (int i=w+1; i
if (matrix[v][i] != 0)
return i;
return Mark.length; // No next edge;
}
/** Set the weight for an edge */
public void setEdge(int i, int j, int wt) {
assert wt!=0 : "Cannot set weight to 0";
if (matrix[i][j] == 0) numEdge++;
matrix[i][j] = wt;
}
/** Delete an edge */
public void delEdge(int i, int j) { // Delete edge (i, j)
if (matrix[i][j] != 0) numEdge--;
matrix[i][j] = 0;
}
/** Determine if an edge is in the graph */
public boolean isEdge(int i, int j)
{ return matrix[i][j] != 0; }
/** @return an edge's weight */
public int weight(int i, int j) {
return matrix[i][j];
}
/** Set/Get the mark value for a vertex */
public void setMark(int v, int val) { Mark[v] = val; }
public int getMark(int v) { return Mark[v]; }
}

这是我实际的Graph类:

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Graph
{
/* Makes use of Map collection to store the adjacency list for 
each vertex.*/
private Map> Adjacency_List;
/*
   * Initializes the map to with size equal to number of vertices in a graph
   * Maps each vertex to a given List Object
   */
public Graph(int number_of_vertices)
{
Adjacency_List = new HashMap>();
for (int i = 1 ; i <= number_of_vertices ; i++)
{
Adjacency_List.put(i, new LinkedList());
}
}
/* Adds nodes in the Adjacency list for the corresponding vertex */
public void setEdge(int source , int destination)
{
if (source > Adjacency_List.size() || destination > 
Adjacency_List.size())
{
System.out.println("the vertex entered in not present ");
return;
}
List slist = Adjacency_List.get(source);
slist.add(destination);
List dlist = Adjacency_List.get(destination);
dlist.add(source);
}
/* Returns the List containing the vertex joining the source vertex */
public List getEdge(int source)
{
if (source > Adjacency_List.size())
{
System.out.println("the vertex entered is not present");
return null;
}
return Adjacency_List.get(source);
}
/*
   * Main Function reads the number of vertices and edges in a graph.
   * then creates a Adjacency List for the graph and prints it
   */
public static void main(String...arg)
{
// set number of vertices
int number_of_vertices = 10;
Graph adjacencyList = new Graph(number_of_vertices);
   /* set the edges present in the graph */
adjacencyList.setEdge(1, 2);
adjacencyList.setEdge(1, 3);
adjacencyList.setEdge(1, 4);
adjacencyList.setEdge(2, 5);
adjacencyList.setEdge(3, 8);
adjacencyList.setEdge(3, 10);
adjacencyList.setEdge(4, 8);
adjacencyList.setEdge(4, 6);
adjacencyList.setEdge(4, 7);
adjacencyList.setEdge(5, 6);
adjacencyList.setEdge(7, 9);
adjacencyList.setEdge(8, 9);
adjacencyList.setEdge(9, 10);
   /* Prints the adjacency List representing the graph.*/
System.out.println ("the given Adjacency List for the graph \n");
for (int i = 1 ; i <= number_of_vertices ; i++)
{
System.out.print(i+"->");
List edgeList = adjacencyList.getEdge(i);
for (int j = 1 ; ; j++ )
{
if (j != edgeList.size())
{
System.out.print(edgeList.get(j - 1 )+"->");
}else
{
System.out.print(edgeList.get(j - 1 ));
break;
}
}
System.out.println();
}
}
}

我从“类Graphm实现图{ 错误指出:“类型Graph不能是Graphm的超级接口;超级接口必须是接口” 现在,我有一个同学重写了本书提供的初始Graphm,并收到了零,因为作业是根据所提供的内容来构建课程。

0 个答案:

没有答案