簡體   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