繁体   English   中英

找不到符号变量…V扩展了在类中声明的对象

[英]cannot find symbol variable … V extends Object declared in class

我正在学校里学习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)){^符号:variable neighbourList location:变量V类型之一,其中V是类型变量:V扩展了在类UndirectedGraph graph \\ undirected \\ UndirectedGraph.java:23中声明的对象:错误:找不到符号if(one.neighbourList.containsKey(two)&& two.neighbourList.containsKey(one)){^符号:变量neighbourList位置:类型V的变量2
其中V是类型变量:V扩展了在类UndirectedGraph中声明的对象2错误

我被困在这里。 谁能帮我?

onetwo的类型为V ,在您的例子中,出于所有目的,它是一个Object 该类型V没有neighbourList字段,因此您无法在下面编写代码,因为它无法编译:

if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM