【发布时间】:2016-01-20 13:21:19
【问题描述】:
我试图从类 String 的节点中获取,但编译器找不到变量。非常感谢您的帮助。
学生班:
public class Students {
private int[] marks;
private String name;
public Students(String name, int[] marks) {
this.name = name;
marks = new int[5];
for (int i = 0; i < marks.length; i++) {
this.marks[i] = marks[i];
}
}
public int getMarksI(int i) {
return marks[0];
}
public int[] getMarks() {
return marks;
}
public String getName() {
return name;
}
}
问题所在的函数:
public double AverageByName(Node < Students > s, String name) {
Node < Students > p = s;
while (p != null) {
if (name == p.getName()) // this is the problem "cannot find symbol p.getName"
{
//
}
p.setNext();
}
return false;
}
除此之外还有一个Node类和Student类的链表在main中创建没有问题。
【问题讨论】:
-
尽管您有实际问题,但您应该看看how do i compare strings in java
-
查看
p的类型——它是Node<Student>,而不是Student。现在您还没有向我们展示Node类,但大概它没有getName()方法...