创建空 ListBuffer 最有效的方法是什么?
创建空 ListBuffer 最有效的方法是什么?
val l1 = new mutable.ListBuffer[String]
val l2 = mutable.ListBuffer[String] ()
val l3 = mutable.ListBuffer.empty[String]< /code>
有什么区别吗?
What is the most efficient way to create empty ListBuffer ?
val l1 = new mutable.ListBuffer[String]
val l2 = mutable.ListBuffer[String] ()
val l3 = mutable.ListBuffer.empty[String]
There are any pros and cons in difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按效率排序:
new mutable.ListBuffer[String]
mutable.ListBuffer.empty[String]
mutable.ListBuffer[String] ()
可以参见
ListBuffer
&的源代码通用伴侣
Order by efficient:
new mutable.ListBuffer[String]
mutable.ListBuffer.empty[String]
mutable.ListBuffer[String] ()
You can see the source code of
ListBuffer
&GenericCompanion
new mutable.ListBuffer[String]
仅创建一个对象(列表缓冲区本身),因此它应该是最有效的方法。mutable.ListBuffer[String] ()
和mutable.ListBuffer.empty[String]
都首先创建一个scala.collection.mutable.AddingBuilder
的实例,然后请求 ListBuffer 的新实例。new mutable.ListBuffer[String]
creates only one object (the list buffer itself) so it should be the most efficient way.mutable.ListBuffer[String] ()
andmutable.ListBuffer.empty[String]
both create an instanceofscala.collection.mutable.AddingBuilder
first, which is then asked for a new instance of ListBuffer.