Scala 缓冲区:大小或长度? [英] Scala Buffer: Size or Length?
问题描述
我正在使用一个可变的 Buffer
并且需要找出它有多少个元素.
I am using a mutable Buffer
and need to find out how many elements it has.
size
和 length
方法都被定义,继承自不同的特征.
Both size
and length
methods are defined, inherited from separate traits.
是否存在任何实际性能差异,或者可以将它们视为完全同义词?
Is there any actual performance difference, or can they be considered exact synonyms?
推荐答案
它们是同义词,主要是 Java 决定集合 size
和 length
用于Array
和 String
.一个总是根据另一个定义,您可以通过查看源代码轻松了解哪个是哪个,scaladoc 上提供了其链接.只需找到定义特征,打开源代码,然后搜索 def size
或 def length
.
They are synonyms, mostly a result of Java's decision of having size
for collections and length
for Array
and String
. One will always be defined in terms of the other, and you can easily see which is which by looking at the source code, the link for which is provided on scaladoc. Just find the defining trait, open the source code, and search for def size
or def length
.
这篇关于Scala 缓冲区:大小或长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!