Java`List`方法`size`如何工作? [英] How does `Java` `List` method `size` work?

查看:115
本文介绍了Java`List`方法`size`如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,有一个List接口和size()方法来计算List的大小.

In Java, there is a List interface and size() method to compute the size of the List.

  • 当我打电话给List.size()时,该怎么计数?
  • 它是线性计数还是确定计数并在size()时只返回值?
  • When I call List.size(), how does it count?
  • Is it counted linearly, or the count is determined and just the value is returned back when size()?

推荐答案

大小定义为列表中元素的数量.该实现未指定size()成员函数的操作方式(遍历成员,返回存储的计数等),因为List是接口而非实现.

Size is defined as the number of elements in the list. The implementation does not specify how the size() member function operates (iterate over members, return stored count, etc), as List is an interface and not an implementation.

通常,大多数具体的List实现将本地存储其当前计数,从而使大小为O(1)而不是O(n)

In general, most concrete List implementations will store their current count locally, making size O(1) and not O(n)

这篇关于Java`List`方法`size`如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆