java - 找不到符号变量... V 扩展类中声明的对象
问题描述
我正在为学校练习 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: 错误: 找不到符号 if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){ ^ 符号: 变量 neighbourList 位置: V 类型的变量之一,其中 V是一个类型变量:V extends Object 在类 UndirectedGraph graph\undirected\UndirectedGraph.java:23 中声明:错误:找不到符号 if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){ ^符号:变量 neighbourList 位置:V 类型的变量二,
其中 V 是类型变量:V 扩展在类 UndirectedGraph 2 中声明的对象
我被困在这里。谁能帮我?
解决方案
one
并且two
是 类型V
,在您的示例中出于所有目的都是Object
. 这种类型V
没有neighbourList
字段,所以你不能在下面写,因为它不能编译:
if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){
推荐阅读
- alloy - 如何创建 100% 的声明性模型?
- angular - Presigned 下载并在对象中查看
- java - HystrixCommandAspect 在纯 Java 应用程序中导致 NoSuchMethodError
- vba - VBA - 通过工作表运行的字典,其中包含一个键和两个相关列
- java - 打开片段
- ios - 在 iOS 应用程序中使用 FBSDKLoginManager 拒绝某些 Facebook 用户的 Facebook 个人资料链接
- r - R - 查找按位二进制邻居(一次翻转一位)
- ios - 使用 Swift for iOS 处理来自已关闭应用程序的推送通知,如何显示它然后返回关闭或暂停?
- c# - XAMARIN:我的 Listview 中有两个标签,我需要在 .cs 中获取它们的文本,但我无法通过 x:Name 访问它们
- java - 需要关于java线程池执行队列处理的建议