ArrayList.size() 方法是否被缓存?

编程入门 行业动态 更新时间:2024-10-28 08:27:40
本文介绍了ArrayList.size() 方法是否被缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想知道,您可以在现有 ArrayList 上调用的 size() 方法是否已缓存?或者在性能关键代码中我只是将 size() 存储在本地 int 中更可取?

I was wondering, is the size() method that you can call on a existing ArrayList<T> cached? Or is it preferable in performance critical code that I just store the size() in a local int?

我希望它确实被缓存,当您不在对 size() 的调用之间添加/删除项目时.

I would expect that it is indeed cached, when you don't add/remove items between calls to size().

我说得对吗?

更新
我不是在谈论内联或诸如此类的事情.我只想知道方法 size() 本身是在内部缓存该值,还是每次调用时动态计算.

update
I am not talking about inlining or such things. I just want to know if the method size() itself caches the value internally, or that it dynamically computes every time when called.

推荐答案

我不认为我会说它是缓存"的——但它只是存储在一个字段中,所以它足够快,可以经常调用.

I don't think I'd say it's "cached" as such - but it's just stored in a field, so it's fast enough to call frequently.

size() 的 Sun JDK 实现只是:

The Sun JDK implementation of size() is just:

public int size() {
    return size;
}

这篇关于ArrayList.size() 方法是否被缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-16 10:56:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/885838.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   方法   ArrayList   size

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!