Scala Buffer: Size or Length?

后端 未结 4 1766
無奈伤痛
無奈伤痛 2021-01-08 01:20

I am using a mutable Buffer and need to find out how many elements it has.

Both size and length methods are defined, inherited

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-08 01:39

    In this case, they can be considered synonyms. You may want to watch out with some other cases such as Array - whilst length and size will always return the same result, in versions prior to Scala 2.10 there may be a boxing overhead for calling size (which is provided by a Scala wrapper around the Array), whereas length is provided by the underlying Java Array.

    In Scala 2.10, this overhead has been removed by use of a value class providing the size method, so you should feel free to use whichever method you like.

提交回复
热议问题