Is ArrayList.size() method cached?

后端 未结 7 1846
再見小時候
再見小時候 2020-12-31 02:58

I was wondering, is the size() method that you can call on a existing ArrayList cached? Or is it preferable in performance critical code t

7条回答
  •  执念已碎
    2020-12-31 03:17

    Why would it need to be? ArrayList implements a List interface that is backed by an array, after all.

    I would assume it to just have a size member, that is incremented when you insert things and decremented when you remove, and then it just returns that.

    I haven't looked at more than the API docs now, though.

提交回复
热议问题