Scala Buffer: Size or Length?

后端 未结 4 1753
無奈伤痛
無奈伤痛 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:42

    They are synonyms, as the scaladoc for Buffer.size states:

    The size of this buffer, equivalent to length.

    The scaladoc for Buffer.length is explicit too:

    The length of the buffer. Note: xs.length and xs.size yield the same result.

    Simple advice: refer to the scaladoc before asking a question.

    UPDATE: Just saw your edit adding mention of performance. As Daniel C. Sobral aid, one is normally always implemented in term of the other, so they have the same performance.

提交回复
热议问题