size() 在java 的ArrayList 类中是如何工作的? [英] How does size( ) works in java's ArrayList class?
本文介绍了size() 在java 的ArrayList 类中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,为了得到最高效的代码,我真的很想知道Java ArrayList
中的size()
方法是如何工作的... 算不算每个元素,遍历所有位置,就像一个简单的列表?还是只是通过最后注册的索引获取大小?
So, in order to get the most efficient code, I really wanted to know how does the size()
method in Java ArrayList
work... Does it count every element, going through all the positions, like a simple list? or does it just gets the size by the last index registered?
提前致谢!
推荐答案
查看 源代码:
public int size() {
return size;
}
它返回一个实例变量 - 非常快.
It returns an instance variable - pretty damn fast.
这篇关于size() 在java 的ArrayList 类中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文