Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does size( ) works in java's ArrayList class?

So, in order to get the most efficient code, I really wanted to know how does the size() method in Java ArrayList work... Does it count every element, going through all the positions, like a simple list? or does it just gets the size by the last index registered?

Thanks in advance!

like image 981
flapas Avatar asked Sep 16 '25 10:09

flapas


1 Answers

Never hurts to look in the source code:

public int size() {
    return size;
}

It returns an instance variable - pretty damn fast.

like image 107
Perception Avatar answered Sep 19 '25 01:09

Perception