scala basic
第一个程序
//object是共生对象,相当于java的静态类 object HelloScala { def main(args: Array[String]){ println("Hello Scala!!!A new World!!!") for(arg <- args) println(arg) //循环打印参数 } }
流程控制
/** * scala中的if表达式是有值的!! 这个Java是不一样的 * 如果if后面没有else部分,默认的实现if else * {}中有多条语句,最后一条语句是计算结果 * {}代表一个语句块,语句快是有值的,值就是最后一条语句,返回类型应该设定成最后一句的类型 */ var age = 10 val result = if (age >25) "worker" else "student" println(result) //else前后的内容一个是字符串,一个是数字,所以result2 val result2 = if (age > 18) "Adult" else 1 //result3 类型是 val result3 = if (age > 18) "Adult" println(result3) var x,y = 0 var result4 = if(age < 18){ x = x + 1 y = y + 1 x + y }else 0 println(result4) //def to(end: Int): Range.Inclusive // 0.to(5) 也可以 for(i <- 0 to 5 if i % 2 == 0){ println(i) } /* * 想跳出for的话,除了加入if守卫以外,还可以是用return关键字 */ var flag = true var sum = 0 for (i <- 0 to 6 if flag){ sum = sum + i if(5 == i) flag = false } println("sum = " + sum)
数组
import scala.collection.mutable.ArrayBuffer //强大的scala的worksheet,即时显示执行结果(右边为自动显示结果) object ScalaInAction { println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet val b = ArrayBuffer[Int]() //> b : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() b+=1 //> res0: learning.basics.ScalaInAction.b.type = ArrayBuffer(1) b+=(1,2,3) //> res1: learning.basics.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3) b++=Array(4,5) //> res2: learning.basics.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 4, 5) b.insert(2, 6) b //> res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 6, 2, 3, //去掉最后3位 //| 4, 5) b.trimEnd(3) b //> res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 6, 2) b.insert(2,7,8,9) b //> res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 7, 8, 9, //| 6, 2) b.remove(2) //> res6: Int = 7 b //> res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 8, 9, 6, //| 2) b.remove(2,3) b //> res8: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2) b.toArray //> res9: Array[Int] = Array(1, 1, 2) b //> res10: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2) for(i <- 0 until b.length) println(b(i)) //> 1 //| 1 //| 2 val c = Array(2,3,4,7,9) //> c : Array[Int] = Array(2, 3, 4, 7, 9) val result = for(item <-c) yield item + 1 //> result : Array[Int] = Array(3, 4, 5, 8, 10) //符合条件的乘以2 _ 代替符号 是很经典的用法 val result1 =c.filter(_ % 2 == 0).map(2 * _) //> result1 : Array[Int] = Array(4, 8) Array(2,4,6,8).sum //> res11: Int = 20 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.mkString(" and ") //> res12: String = 1 and 2 and 7 and 9 e.mkString("<",",",">") //> res13: String = <1,2,7,9> val matrix = Array.ofDim[Double](3,4) //> matrix : Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, //| 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0)) matrix(2)(1) = 42 matrix //> res14: Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0. //| 0, 0.0, 0.0), Array(0.0, 42.0, 0.0, 0.0)) }
Map和Tuple
val map = Map("book"->10,"gun"->18,"ipad"->1000) // yield 给map每个元素赋值 for((k,v) <- map) yield (k,v * 0.9) val scores = scala.collection.mutable.Map("Scala" -> 7, "Hadoop" -> 8, "Spark" -> 10 ) //getOrElse使用很普遍,当Hadoop为空的时候,返回0 val hadoopScore = scores.getOrElse("Hadoop", 0) //追加元素 scores += ("R" -> 9) //删除元素 scores -= "Hadoop" val sortedScore = scala.collection.immutable.SortedMap("Scala" -> 7, "Hadoop" -> 8, "Spark" -> 10 ) val tuple =(1,2,3.14,"Rocky","Spark","Flink") val third = tuple._3 val (first,second,thirda,fourth,fifth,sixth) = tuple val (f, s, _, _, _,_) = tuple //通过大小写原则分类 "Rocky Spark".partition(_.isUpper) val symbols = Array("[", "-", "]") val counts = Array(2,5,2) val pairs = symbols.zip(counts) for ((x,y) <- pairs) print(x*y)
函数
object For_Function_Advanced { def main(args: Array[String]): Unit = { // for(i <- 1 to 2; j <- 1 to 2) print((100*i + j) + " ") //循环嵌套 // println // for(i <- 1 to 2; j <- 1 to 2 if i != j) print((100*i + j) + " ") // println // // // def addA(x : Int) = x +100 // val add = (x : Int) => x +200 //匿名函数 // println("The result from a function is : " + addA(2)) // println("The result from a val is : " + add(2)) // def fac(n:Int):Int = if (n <= 0) 1 else n * fac(n - 1) // println("The result from a fac is : " + fac(10)) // def combine(content:String, left: String = "[", right: String = "]") = left + content +right // println("The result from a combine is : " + combine("I love Spark", "<<")) // def connected(args: Int*) = { // var result =0 // for(arg <- args) result += arg // result // } // println("The result from a connected is : " + connected(1,2,3,4,5,6)) // }
浙公网安备 33010602011771号