In Java, there is a List interface and size() method to compute the size of the List.
- 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()?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.
In general, most concrete List implementations will store their current count locally, making size O(1) and not O(n)