Difference between size and length methods?

前端 未结 6 644
北海茫月
北海茫月 2020-12-12 14:46

What is the difference between .size() and .length ? Is .size() only for arraylists and .length only for arrays?

6条回答
  •  抹茶落季
    2020-12-12 14:58

    • .length is a field, containing the capacity (NOT the number of elements the array contains at the moment) of arrays.

    • length() is a method used by Strings (amongst others), it returns the number of chars in the String; with Strings, capacity and number of containing elements (chars) have the same value.

    • size() is a method implemented by all members of Collection (lists, sets, stacks,...). It returns the number of elements (NOT the capacity; some collections even don´t have a defined capacity) the collection contains.

提交回复
热议问题