1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| package lagou.cn.part03
object OperatorDemo { def main(args: Array[String]): Unit = { val nums = (1 to 10).toArray val result1 = for (elem <- nums if elem % 2 == 0) yield elem * 2 val result2 = for (elem <- nums) yield if (elem % 2 == 0) elem * 2 else 0 result1.foreach(println(_)) println("=============================") result2.foreach(println(_))
nums.filter(_ % 2 == 0).map(_ * 2).foreach(println(_))
println("=============================") println(nums.head) println(nums.last) println(nums.tail.toBuffer) println(nums.init.toBuffer)
println(nums.sum) println(nums.max) println(nums.min)
val nums2 = Array(2, 1, 4, 3) println(nums2.sorted.toBuffer) println(nums2.product)
println("====================================") val nums3 = Array(1, 2, 3, 4, 3, 2, 1) println(nums3.map(_ * 2).toBuffer) println(nums3.reduce(_ + _)) println(nums3.distinct.toBuffer) println(nums3.length) println(nums3.size) println(nums3.indices.toBuffer)
println(nums3.mkString(" & ")) println(nums3.mkString("<", " & ", ">"))
println(nums3.count(_ > 2)) println(nums3.count(_ % 2 == 0))
println(nums3.filter(_ > 2).toBuffer) println(nums3.filterNot(_ % 2 == 0).toBuffer)
println("========================================") println(nums3.take(3).toBuffer) println(nums3.takeRight(4).toBuffer) println(nums3.takeWhile(_ < 4).toBuffer)
println("========================================") println(nums3.drop(3).toBuffer) println(nums3.dropRight(3).toBuffer) println(nums3.dropWhile(_ < 4).toBuffer)
println("========================================") val tuple: (Array[Int], Array[Int]) = nums3.splitAt(3) println(tuple._1.toBuffer + " " + tuple._2.toBuffer) println(nums3.slice(2, 5).toBuffer)
println("========================================") val array1 = Array("A", "B", "C") val array2 = Array(1, 2, 3, 4) val z1: Array[(String, Int)] = array1.zip(array2) println(z1.toBuffer)
val z2 = array1.zipAll(array2, "*", -1) val z3 = array2.zipAll(array1, "*", -1) val z4 = array1.zipWithIndex println(z2.toBuffer) println(z3.toBuffer) println(z4.toBuffer)
println("========================================") val (l1, l2) = z4.unzip println(l1.toBuffer) println(l2.toBuffer)
val (l3, l4, l5) = Array((1, "one", '1'), (2, "two", '2'), (3, "three", '3')).unzip3 println(l3.toBuffer) println(l4.toBuffer) println(l5.toBuffer)
val num1 = (1 to 4).toArray val num2 = (5 to 8).toArray
val num3 = 10 +: num1 val num4 = num2 :+ 9 val num5 = num1 ++ num2 println("========================================") println(num3.toBuffer) println(num4.toBuffer) println(num5.toBuffer)
println("===========数组排序==============") val sortNums=Array(1,3,5,2,7,8,6,9) println(sortNums.sorted.toBuffer) println(sortNums.sorted.reverse.toBuffer) println(sortNums.sortWith(_>_).toBuffer) println(sortNums.sortWith(_<_).toBuffer)
} }
|