1.读取文件:首先导入包:import scala.io.Source
val file = Source.fromFile("E:\\test.txt");
for(line <- file.getLines){
println(line);
}
2.定义函数
def addA(x : Int) = x+100;
val add = (x:Int) => x+200;
println("ddd"+addA(1));
println("cc"+add(2));
def combine(content:String,left:String = "[",right:String = "]") = left + content +right;
println("jadhakjd"+combine("i love wenqi"));
3.可变参数
def connected(args:Int*) = {
var result = 0;
for(arg <- args)
result+=arg
result
}
println(connected(1,2,3,4,5));
4.定义数组
val nums = new Array[Int](10); //> nums : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
val a = new Array[String](10);
5.可变数组
首先导入包import scala.collection.mutable.ArrayBuffer
val b = ArrayBuffer[Int]() //> b : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
b += 1 //> res0: com.ziyuan.web.TerAction.b.type = ArrayBuffer(1)
b //> res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)
b+=(1,2,3,4) //> res2: com.ziyuan.web.TerAction.b.type = ArrayBuffer(1, 1, 2, 3, 4)
b++=Array(8,9,12,12,13,16) //> res3: com.ziyuan.web.TerAction.b.type = ArrayBuffer(1, 1, 2, 3, 4, 8, 9, 12,
//| 12, 13, 16)
b.trimEnd(2) 移除数组后2个数
b //> res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 4,
//| 8, 9, 12, 12)
b.insert(2,6) 在数组下标2处插入6,数组下标是从0开始
b //> res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 6, 2, 3,
//| 4, 8, 9, 12, 12)
b.remove(2) 移除下标为2的数 //> res6: Int = 6
b.remove(2,3)移除从数组下标为2后面的3位数
val c = b.toArray 把可变数组转换为不可变数组 //> c : Array[Int] = Array(1, 1, 8, 9, 12, 12)
6.数组进阶操作
val result = for(elem <-c)yield elem*2 数组乘2 //> result : Array[Int] = Array(4, 6, 10, 14, 22)
c.filter(_%2 == 0).map(2 * _) 数组乘2 //> res7: Array[Int] = Array(4)
Array(1,7,8).sum 数组求和 //> res8: Int = 16
ArrayBuffer("dd","adad","fffa","sadadda").max 数组里字符数最多的 //> res9: String = sadadda
val d = ArrayBuffer(1,7,2,9) //> d : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 9)
val bSorted = d.sorted 数组排序 //> bSorted : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7,
//| 9)
val e = Array(1,7,2,9) //> e : Array[Int] = Array(1, 7, 2, 9)
scala.util.Sorting.quickSort(e) 数组快排
e //> res10: Array[Int] = Array(1, 2, 7, 9)
e.mkString(" and ") 连接数组 //> res11: String = 1 and 2 and 7 and 9
d.mkString("<",",",">") //> res12: String = <1,7,2,9>
7.map、tuple、zip
val map = Map("book"->10,"gun"->18,"ipad"->1000) //> map : scala.collection.immutable.Map[String,Int] = Map(book -> 10, gun -> 1
//| 8, ipad -> 1000)
for((k,v)<-map)yield (k,v*0.9) //> res0: scala.collection.immutable.Map[String,Double] = Map(book -> 9.0, gun -
//| > 16.2, ipad -> 900.0)
val hadoopScore = map.getOrElse("book",0) 根据key 取value,没有取0 //> hadoopScore : Int = 10
val tuple = (1,2,3.14,"a","b") 类型可以不一样 //> tuple : (Int, Int, Double, String, String) = (1,2,3.14,a,b)
val p = tuple._3 //> p : Double = 3.14
val(fir,se,gg,mm,nn)= tuple //> fir : Int = 1
//| se : Int = 2
//| gg : Double = 3.14
//| mm : String = a
//| nn : String = b
val(ii,ddd,_,_,_) = tuple //> ii : Int = 1
//| ddd : Int = 2
"Rcock Spark".partition(_.isUpper) 大写和小写分2个部分 //> res1: (String, String) = (RS,cock park)
val symbol = Array("[","-","]") //> symbol : Array[String] = Array([, -, ])
val couny = Array(2,5,2) 组合2个不同类型的数组 //> couny : Array[Int] = Array(2, 5, 2)
val pair = symbol.zip(couny) //> pair : Array[(String, Int)] = Array(([,2), (-,5), (],2))