简体   繁体   中英

cannot find symbol variable … V extends Object declared in class

I'm practising Java for school, and I'm in trouble now.

This is my Graph.java file:

package graph;

public interface Graph<V>{
    public boolean hasEdge(V one, V two);
    public void addNode(V other);
    public void addEdge(V other);
}

And this is my UndirectedGraph.java file:

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
        }
    }
}

And I got the following errors:

graph\\undirected\\UndirectedGraph.java:23: error: cannot find symbol if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){ ^ symbol: variable neighbourList location: variable one of type V where V is a type-variable: V extends Object declared in class UndirectedGraph graph\\undirected\\UndirectedGraph.java:23: error: cannot find symbol if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){ ^ symbol: variable neighbourList location: variable two of type V
where V is a type-variable: V extends Object declared in class UndirectedGraph 2 errors

And I'm stuck here. Can anyone help me?

one and two are of type V , which for all purposes in your example is an Object . This type V doesn't have a neighbourList field so you can't write below as it doesn't compile:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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