scala学习笔记(9): 语法续
2016-07-11 20:57
344 查看
1 不定长参数
2 数组初始化
3 变长数组
4 应用于Array、ArrayBuffer的算法
5 数组的mkString
def sum(args: Int*) = { var result = 0 for ( arg <- args) result += arg result }
2 数组初始化
val nums = new Array[Int](10) nums: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) val nums1 = Array(1, 2, 4) nums1: Array[Int] = Array(1, 2, 4)
3 变长数组
import scala.collection.mutable.ArrayBuffer val b = new ArrayBuffer[Int]() //b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() b += 1 // 添加元素到末尾 res9: b.type = ArrayBuffer(1) b += (2, 3, 4) // 添加多个元素用括号 res10: b.type = ArrayBuffer(1, 2, 3, 4) val a = 5 to 10 a: scala.collection.immutable.Range.Inclusive = Range(5, 6, 7, 8, 9, 10) b ++= a // 添加其他类型 res11: b.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
4 应用于Array、ArrayBuffer的算法
scala> val a = Array(1, 7, 2, 4, 5) //a: Array[Int] = Array(1, 7, 2, 4, 5) scala> a.sorted //res18: Array[Int] = Array(1, 2, 4, 5, 7) scala> a.sortWith(_ < _) //res19: Array[Int] = Array(1, 2, 4, 5, 7) scala> a.sortWith(_ > _) //res20: Array[Int] = Array(7, 5, 4, 2, 1)
5 数组的mkString
scala> val a = Array(1, 7, 2, 4, 5) //a: Array[Int] = Array(1, 7, 2, 4, 5) scala> a.mkString(" and ") //res21: String = 1 and 7 and 2 and 4 and 5 scala> a.mkString("[", ",", "]") //res22: String = [1,7,2,4,5]
相关文章推荐
- (4.2.32.3)android热修复之Andfix方式:Andfix的初步使用
- HDU 1203 I NEED A OFFER!
- 十进制转换为其他进制数
- 【Unity Shaders】法线纹理(Normal Mapping)的实现细节
- 工作中任务管理的四个原则和四个技能
- 第9讲 进程与服务
- android关于版本更新问题
- 密码学_RSA算法原理详解
- FPGA笔记(九)-实现ROM步骤
- PHP Mail() 函数最终解决办法
- Tomcat异常页面处理
- POJ 1087 A Plug for UNIX(网络流)
- Node.js 适应场景
- 优先级队列
- 自定义ViewGroup
- Adobe Photoshop cc 2015.5 V17.0安装与破解完全教程
- ie内核相关网址记录
- 正向代理与反向代理
- 第142课: Spark面试经典系列之Cache和Checkpoint
- 利用linux curl爬取网站数据