创建空 ListBuffer 最有效的方法是什么?

发布于 2024-08-28 05:35:41 字数 264 浏览 4 评论 0原文

创建空 ListBuffer 最有效的方法是什么?

  1. val l1 = new mutable.ListBuffer[String]
  2. val l2 = mutable.ListBuffer[String] ()
  3. val l3 = mutable.ListBuffer.empty[String]< /code>

有什么区别吗?

What is the most efficient way to create empty ListBuffer ?

  1. val l1 = new mutable.ListBuffer[String]
  2. val l2 = mutable.ListBuffer[String] ()
  3. val l3 = mutable.ListBuffer.empty[String]

There are any pros and cons in difference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑轻吟梦一曲 2024-09-04 05:35:41

按效率排序:

  1. new mutable.ListBuffer[String]
  2. mutable.ListBuffer.empty[String]
  3. mutable.ListBuffer[String] ()

可以参见ListBuffer&的源代码通用伴侣

Order by efficient:

  1. new mutable.ListBuffer[String]
  2. mutable.ListBuffer.empty[String]
  3. mutable.ListBuffer[String] ()

You can see the source code of ListBuffer & GenericCompanion

临风闻羌笛 2024-09-04 05:35:41

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] () and mutable.ListBuffer.empty[String] both create an instanceof scala.collection.mutable.AddingBuilder first, which is then asked for a new instance of ListBuffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文