【发布时间】:2021-04-10 19:43:12
【问题描述】:
我目前对我的任务感到困惑。我正在使用泛型并遇到错误,我不明白为什么会出现它。任何帮助将不胜感激!
这是错误:
C:\path>javac *.java
Graph.java:76: error: cannot find symbol
String nodeLabel = node.getLabel();
^
symbol: method getLabel()
location: variable node of type N
where N is a type-variable:
N extends Object declared in class Graph
我在下面发生此错误的行旁边的代码中添加了注释
以下是相关方法:
public void propogate(N node, float lambda, Graph<N,L> otherGraph) {
//find degree of node
int theDegree = getDegreeOfNode(node);
//determine num of susceptible neighnbors to be infected
int numToInfect = -1;
for (int i = 0; i < theDegree + 1; i++) {
float calc = (newInfected + i) / infectNodesProc;
if (compare(calc, lambda) > 0) {
float val1 = calc - lambda;
float val2 = lambda - ((newInfected + (i - 1)) / infectNodesProc);
if (compare(val1, val2) < 0) {
numToInfect = i;
} else {
numToInfect = i - 1;
}
break;
}
}
if (numToInfect > 0) {
otherGraph.infectNeighbors(node, theDegree, numToInfect);
newInfected += numToInfect;
}
infectNodesProc++;
}
public void infectNeighbors(N node, int theDegree, int numToInfect) {
//get equivalent node in this graph
String nodeLabel = node.getLabel(); // THIS IS THE LINE THAT THE ERROR IS TALKING ABOUT
Iterator<N> it = nodes.iterator();
N theNode = null;
while (it.hasNext()) {
N aNode = it.next();
if (aNode.getLabel().equals(nodeLabel)) {
theNode = aNode;
break;
}
}
ArrayList<N> suscNeighbors = getSuscNeighbors(theNode);
int toInfect = numToInfect;
while (suscNeighbors.size() > 0 && toInfect > 0) {
Random rand = new Random();
int randInd = rand.nextInt(suscNeighbors.size());
N removedNode = suscNeighbors.remove(randInd);
removedNode.state = StateEnum.INFECTIOUS;
toInfect--;
}
}
以上两个方法都在 Graph
Node 类是当行 String nodeLabel = node.getLabel(); 时 Generic 的 N 参数将是什么叫做。这个类存在所以我知道在我的包或任何东西中找到这个类不是问题。此外,getLabel() 方法是公共的且可访问的,因此它不是不正确的访问修饰符等问题。我很确定它与泛型有关。我是否必须在 Graph 类的顶部执行
非常感谢!
【问题讨论】:
标签: java debugging generics graph compiler-errors