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)) }
相关文章推荐
- HDU-4521 小明系列问题――小明序列(线段树)
- 【C程序设计语言(第二版)学习】单词计数 方法
- 第十三周周记
- 第12周表格
- 并查集+最大流-HDU-3038-Marriage Match II
- mybatis异常
- Iterable(迭代器)的用法
- zend studio 12汉化和破解
- stl中的tuple(tie)
- nginx 跨域配置
- Android中的通知Notification
- JAVA 大整数类
- jquery动画-animate()
- Multiple dex files define Landroid/support/annotation/AnimRes
- the server ssl certificate failed to verify
- U盘为什么还有剩余空间,但却提示说空间不够
- 集合
- play-maven脚本
- Leetcode 345. Reverse Vowels of a String
- 第十二周周记