scala进阶28-实现List高效遍历
2016-08-30 00:50
513 查看
object ListTest { def main(args: Array[String]): Unit = { val list = List(1, 3, 4, 5) 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) } /** * 这种方式实现,会产生很多中间的result */ def increment_MoreEffective(list: List[Int]) : List[Int] = { var result = List[Int]() for (element <- list) result = result ::: List(element + 1) result } /** * 借助ListBuffer,最高效的方式 */ def increment_MostEffective(list: List[Int]) : List[Int] = { import scala.collection.mutable.ListBuffer var buffer = new ListBuffer[Int] for (element <- list) buffer += element + 1 buffer.toList } }
相关文章推荐
- Spring的注入方式的浅讲一
- git 相关
- 安卓屏蔽返回键
- CocoPods报错:[!] The dependency `XXX` is not used in any concrete target.
- CodeForces 369 div2 C Coloring Trees DP
- javaee之Spring的整合
- 批量SQL插入性能优化
- vs调用rsa密钥退出的解决办法
- 买彩票-36随机选择7位数
- linux运维人员必会开源运维工具体系
- Concurrent下的线程安全集合
- NSArray 常用方法
- mark
- NSMutableArray 的简单用法
- mutex与semaphore的区别
- HTML 5 drag and drop 简介
- UVa1025/UVa437/UVa1347
- Oracle学习笔记之第三节之安装数据库
- Unity 学习顺序
- VS: 如何导入静态LIB库