scala> def m1 ( x: Int, y: Int ) : Int = x + y
m1: (x: Int, y: Int)Int
scala> m1 ( 3 , 4 )
res0: Int = 7
scala> val f1 = ( x: Int, y: Int ) => x + y
f1: (Int, Int) => Int = <function2>
scala> val f1 = (x: Int) => x + x
f1: Int => Int = <function1>
scala> val f1 = (x: Int, y: Int ) => x + y
f1: (Int, Int) => Int = <function2>
scala> f1 ( 2,3)
res1: Int = 5
scala> def m2 ( f: (Int,Int) => Int) = f(3,4)
m2: (f: (Int, Int) => Int)Int
scala> val f1 = (x: Int, y: Int) => x + y
f1: (Int, Int) => Int = <function2>
scala> m2(f1)
res3: Int = 7
scala> def m1(x: Int,y: Int):Int = x + y
m1: (x: Int, y: Int)Int
scala> val f1 = m1 _
f1: (Int, Int) => Int = <function2>
scala> m2(f1)
res4: Int = 7
scala> m2(m1)
res5: Int = 7
scala> val arr1 = Array[Int](8)
arr1: Array[Int] = Array(8)
scala> val arr1 = new Array[String](8)
arr1: Array[String] = Array(null, null, null, null, null, null, null, nu
scala> println(arr1)
[Ljava.lang.String;@6ebd78d1
scala> println(arr1.toBuffer)
ArrayBuffer(null, null, null, null, null, null, null, null)
scala> val arr2 = Array("java","scala","python")
arr2: Array[String] = Array(java, scala, python)
scala> println(arr2(0))
java
scala> val arr3 = ArrayBuffer[Int]()
<console>:7: error: not found: value ArrayBuffer
val arr3 = ArrayBuffer[Int]()
^
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> val arr3 = ArrayBuffer[Int]()
arr3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> arr3 += 1
res3: arr3.type = ArrayBuffer(1)
scala> arr3 += (2,3,4)
res4: arr3.type = ArrayBuffer(1, 2, 3, 4)
scala> arr3 ++= Array(5,6)
res5: arr3.type = ArrayBuffer(1, 2, 3, 4, 5, 6)
scala> arr3 ++= ArrayBuffer(7,8)
res6: arr3.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)
scala> arr3.insert(0,-1,0)
scala> arr3.insert(0,-1,0,1,2)
scala> arr3
res9: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 2, -1, 0, 1, 2, 3, 4, 5, 6, 7,
scala> arr3.remove(2,2)
scala> val arr = Array(1,2,3,4,5,6,7,8,9)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> for ( i <- arr ) println(i)
1
2
3
4
5
6
7
8
9
scala> for ( i <- 0 until arr.length ) println(arr(i))
1
2
3
4
5
6
7
8
9
scala> for ( i <- (0 until arr.length).reverse ) println(arr(i))
9
8
7
6
5
4
3
2
1
scala> val res = for ( i <- arr ) yield i * 10
res: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
scala> res
res14: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
scala> arr.sum
res15: Int = 45
scala> arr.max
res16: Int = 9
scala> arr.sorted
res17: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
网友评论