Scala中ListBuffer高效遍历
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Scala中ListBuffer高效遍历,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1278字,纯文字阅读大概需要2分钟。
内容图文
package com.dt.scala.list object ListBuffer_Internals { def main(args: Array[String]): Unit = { val list = List(1,2,3,4,5,6,7,8,9) increment(list) increment_MoreEffective(list) increment_MostEffective(list) } def increment(list :List[Int]) : List[Int] = list match {//函数建立堆栈需要消耗大量的内存 case List() => List() case head :: tail => head + 1 :: increment(tail) } def increment_MoreEffective(list : List[Int]) : List[Int] = {//比上边那种有较少的内存消耗。理论上讲支持所有元素个数。但是这里会产生很多种中间结果的list对象,效率是非常低的。 var result = List[Int]() for(element <- list) result = result ::: List(element + 1) result } def increment_MostEffective(list: List[Int]) : List[Int] = {//最高效、既能够使用循环遍历,又会避免创建那么多的中间对象,就引入了ListBuffer import scala.collection.mutable.ListBuffer //可变 var buffer = new ListBuffer[Int] for(element <- list) buffer += element + 1 buffer.toList } //ListBuffer源码: // def += (x:A) : this.type = { // if (exported) copy() // if (start.isEmpty){ // last0 = new :: (x,Nil) // start = last0 // } else { // val last1 = last0 // last0 = new :: (x,Nil) // last1.tl = last0 //不断在后边追加成员 // } // len += 1 // this // } // // override def toList:List[A] = { // exported = !start.isEmpty // start // } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/u013361361/article/details/47774505
内容总结
以上是互联网集市为您收集整理的Scala中ListBuffer高效遍历全部内容,希望文章能够帮你解决Scala中ListBuffer高效遍历所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】