【发布时间】:2016-06-21 05:57:05
【问题描述】:
我正在尝试获取列表的计数。我编写了单独的函数来获取计数。当我将其调用为主时,它会引发错误。 错误是
类型 LinkedList 的 getCount 未定义。
我的代码是
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
}
}
谁能帮帮我
【问题讨论】:
-
您在 LengthCount 类中定义了该方法。为什么你希望它出现在 java.util.LinkedList 中?
-
LinkedList 没有成员函数 getCount() (API)。
-
getcount 是用户定义函数。我们可以用列表调用用户定义函数吗?谢谢!!
标签: java list data-structures