We can determine the length of an ArrayList<E>
using its public method size()
, like
ArrayList<Integer> arr = new ArrayList(10);
int size = arr.size();
Similarly we can determine the length of an Array
object using the length
property
String[] str = new String[10];
int size = str.length;
Whereas the size()
method of ArrayList
is defined inside the ArrayList
class, where is this length
property of Array
defined?
Even though this is not a direct answer to the question, it is an addition to the
.length
vs.size()
argument. I was researching something related to this question so when I came across it I noticed that the definition(s) provided hereis not "exactly" correct.
The field length contains the number of available places to put a component, not the number of components present in the array. So it represents the total available memory allocated to that array, not how much of that memory is filled.
Example:
Output:
However, the
.size()
property of theArrayList
does give the number of elements in the list:Output: