Java 如何获取链表中的元素数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19349952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the number of elements in a linked list?
提问by Dr. Barber
I have a method called getLength
我有一个叫做 getLength 的方法
public int getLength(LinkedList<Boolean> numbers){
int length = numbers.size();
return length;
}
So it passes in a linked list of boolean values and names it numbers. What I'm trying to do is just get the length of the linked list, how many elements there are in it.
所以它传入一个布尔值的链表并将其命名为数字。我想要做的只是获取链表的长度,其中有多少元素。
Am I using .size()
correctly in that case?
.size()
在这种情况下我使用正确吗?
回答by 1024chickens
Yes, .size() will return the number of items in the linked list:
是的, .size() 将返回链表中的项目数:
http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#size()
http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#size()
回答by Kaijiro
On the Java API, we can read :
在 Java API 上,我们可以阅读:
size() : Returns the number of elements in this LinkedList.
size() :返回此 LinkedList 中元素的数量。
So, I think that's what you want to get.
所以,我认为这就是你想要得到的。
Source : http://developer.android.com/reference/java/util/LinkedList.html
来源:http: //developer.android.com/reference/java/util/LinkedList.html