您的位置:首页 > 其它

day05--scala

2016-05-19 20:46 435 查看
注释:右面是运行的结果

object ScalaInAction {
println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
val s=Array("hello","world")      //> s  : Array[String] = Array(hello, world)
val a=new Array[String](10)       //> a  : Array[String] = Array(null, null, null, null, null, null, null, null, n
//| ull, null)
s(0)="GOOO DBYE"
s                                 //> res0: Array[String] = Array(GOOO DBYE, world)
import scala.collection.mutable.ArrayBuffer
val b=ArrayBuffer[Int]()          //> b  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
b+=1                              //> res1: day05.ScalaInAction.b.type = ArrayBuffer(1)
b+=(1,2,3,4)                      //> res2: day05.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 4)
b++=Array(6,7,8)                  //> res3: day05.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 4, 6, 7, 8)
b                                 //> res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 4,
//|  6, 7, 8)
b.trimEnd(5)
b.insert(1,10)
b                                       //> res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 10, 1, 2)
b.remove(2,1)
b                                 //> res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 10, 2)
b.toArray                         //> res7: Array[Int] = Array(1, 10, 2)
for(i<- 0 until a.length)
println(i+" "+a(i))                         //> 0 null
//| 1 null
//| 2 null
//| 3 null
//| 4 null
//| 5 null
//| 6 null
//| 7 null
//| 8 null
//| 9 null

val c=Array(2,3,5,7,11)                      //> c  : Array[Int] = Array(2, 3, 5, 7, 11)
val result=for(elem<-c) yield 2*elem         //> result  : Array[Int] = Array(4, 6, 10, 14, 22)
for(elem<-c if elem%2==0 ) yield 2*elem      //> res8: Array[Int] = Array(4)

c.filter(_%2==0).map(2*_)                    //> res9: Array[Int] = Array(4)
c                                            //> res10: Array[Int] = Array(2, 3, 5, 7, 11)

Array(1,2,4,5,6,7).sum                       //> res11: Int = 25

ArrayBuffer("in","love","loewwe").max        //> res12: String = love

val d=ArrayBuffer(1,7,2,9)                   //> d  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 9)
val e=d.sorted                               //> e  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7, 9)

val f=Array(1,7,2,9)                         //> f  : Array[Int] = Array(1, 7, 2, 9)
scala.util.Sorting.quickSort(f)
f                                            //> res13: Array[Int] = Array(1, 2, 7, 9)

e.mkString("and")                            //> res14: String = 1and2and7and9   //def mkString(sep: String): String = mkString("", sep, "")
s.mkString("<",",",">")                      //> res15: String = <GOOO DBYE,world>// def mkString(start: String, sep: String, end: String): String =
//addString(new StringBuilder(), start, sep, end).toString

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
val triangle=new Array[Array[Int]](10)      //> triangle  : Array[Array[Int]] = Array(null, null, null, null, null, null, n
//| ull, null, null, null)

for(i<-0 until triangle.length)
triangle(i)=new Array[Int](i+1)
triangle                                    //> res16: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Arr
//| ay(0, 0, 0, 0), Array(0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(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
//| , 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: