java类型LinkedList的方法getCount未定义
我正试图得到名单上的人数。我编写了单独的函数来获取计数。当我把这个调用到main中时,它是抛出错误。 错误是
getCount is undefined for type LinkedList.
我的代码是
import java.util.*;
import java.util.LinkedList.*;
public class LengthCount {
Node head;
// Insert a new node from the front.
public void push(int new_data){
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
// Function for getting count
public int getCount(){
int count = 0;
Node temp = head;
while(temp != null){
count++;
temp = temp.next;
}
return count;
}
public static void main(String[] args) {
LinkedList llist = new LinkedList();
llist.push(1);
llist.push(3);
llist.push(1);
llist.push(2);
llist.push(1);
System.out.println("Counts of node is : "+llist.getCount()); // Error in this line
}
}
有人能帮帮我吗
# 1 楼答案
我猜你是想知道名单的长度。然后使用API。这是你修改过的代码
# 2 楼答案
GetCount是在类LengthCount中定义的方法。因此,您必须创建类的对象来访问该方法,或者使用此方法。getCount()
您不能使用列表。getCount()由于链表类没有该方法,所以它有list。大小()