не может найти переменную символа ... V расширяет Объект, объявленный в классе - PullRequest
0 голосов
/ 19 мая 2018

Я практикую 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)) {^ символ: переменная location_bourList: переменная типа V, где V - переменная типа: V расширяет объект, объявленный в классе UndirectedGraph graph \ undirected \ UndirectedGraph.java: 23: error: невозможно найти символ if (one.neighbourList.containsKey (two) && two.neighbourList.containsKey (one)) {^ symbol: переменная liebourList location: переменная two типа V
, где V - это переменная типа: V extendsОбъект объявлен в классе UndirectedGraph 2 ошибки

И я застрял здесь.Кто-нибудь может мне помочь?

1 Ответ

0 голосов
/ 19 мая 2018

one и two имеют тип V, который для всех целей в вашем примере является Object.Этот тип V не имеет поля neighbourList, поэтому вы не можете писать ниже, так как он не компилируется:

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