是ArrayList.size()方法中缓存? [英] Is ArrayList.size() method cached?

查看:197
本文介绍了是ArrayList.size()方法中缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是尺寸()方法,你可以调用现有的的ArrayList< T> 缓存?
抑或是preferable在性能关键code,我只是存储尺寸()在当地的诠释?

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?

我希望它确实缓存,当你不调用之间添加/删除项目尺寸()

I would expect that it is indeed cached, when you don't add/remove items between calls to 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.

太阳JDK实施尺寸()仅仅是:

The Sun JDK implementation of size() is just:

public int size() {
    return size;
}

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

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