我在学校练习Java,现在我遇到了麻烦。
这是我的Graph.java文件:
package graph;
public interface Graph<V>{
public boolean hasEdge(V one, V two);
public void addNode(V other);
public void addEdge(V other);
}
这是我的UndirectedGraph.java文件:
package graph.undirected;
import graph.*;
import java.util.*;
public class UndirectedGraph<V> implements Graph<V>{
private HashMap<V,V> neighbourList;
private TreeMap<V,V> prev;
private TreeMap<V,Integer> dist;
public UndirectedGraph(){
neighbourList = new HashMap<V,V>();
prev = new TreeMap<V,V>();
dist = new TreeMap<V,Integer>();
}
public boolean hasEdge(V one, V two){
if(!(this.neighbourList.containsKey(one) && this.neighbourList.containsKey(two))){
throw new java.util.NoSuchElementException("Nonexistent node.");
}
else{
if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){
return false;
}
return true;
}
}
public void addNode(V other){
if(!(this.neighbourList.containsKey(other))){
// some code will come here
}
}
public void addEdge(V other){
if(!(this.neighbourList.containsKey(other))){
// and some code will come here too
}
}
}
我收到了以下错误:
graph \ undirected \ UndirectedGraph.java:23:error:找不到符号 if(one.neighbourList.containsKey(two)&amp;&amp; two.neighbourList.containsKey(one)){ ^符号:变量neighbourList location:V类型的变量,其中V是类型变量: V扩展在类UndirectedGraph graph \ undirected \ UndirectedGraph.java中声明的Object:23:错误:找不到符号 if(one.neighbourList.containsKey(two)&amp;&amp; two.neighbourList.containsKey(one)){ ^符号:变量neighbourList位置:变量二类型V
其中V是一个类型变量: V扩展在UndirectedGraph类错误中声明的Object
我被困在这里。任何人都可以帮助我吗?
答案 0 :(得分:0)
one
和two
属于V
类型,在您的示例中,所有用途都是Object
。此类型V
没有neighbourList
字段,因此您无法在下面编写,因为它不会编译:
if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){