Java 中对 LinkedList 调用 size() 的时间复杂度是多少? [英] What is the time complexity of a size() call on a LinkedList in Java?
本文介绍了Java 中对 LinkedList 调用 size() 的时间复杂度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如标题所问,我想知道 LinkedList 类中的 size() 方法是否需要分摊 O(1) 时间或 O(n) 时间.
As the title asks, I wonder if the size() method in the LinkedList class takes amortized O(1) time or O(n) time.
推荐答案
O(1).你可以谷歌搜索源代码,你会得到这样的:
It's O(1). You can google for the source code and you will come to such:
来自 http://www.docjar.com/html/api/java/util/LinkedList.java.html
我看过的所有 Collection 类都将大小存储为变量,并且不会遍历所有内容来获取它.
All of the Collection classes I have looked at store a the size as a variable and don't iterate through everything to get it.
这篇关于Java 中对 LinkedList 调用 size() 的时间复杂度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文