object HelloArrayOps {
def main(args: Array[String]): Unit = {
// val array = new Array[Int](5)
// val array = Array[Int](1,2,3,4,5)
// val array = Array(1, 2, 3, 4, 5)
// val array = Array.apply(1,2,3,4,5)
val array = Array(1, 2, 3, 4, 5)
array(0) = 10
for (item <- array) println(item)
import scala.collection.mutable.ArrayBuffer
val arrayBuffer = new ArrayBuffer[Int]()
arrayBuffer += 1
arrayBuffer += 2
arrayBuffer += 3
arrayBuffer += (4, 5, 6, 7, 8, 9, 10)
arrayBuffer ++= Array(1, 2, 3)
arrayBuffer.insert(arrayBuffer.length - 1, 100, 1000)
for (item <- arrayBuffer) println(item)
for (i <- 0 until (array.length)) print(array(i) + " ")
println
for (i <- 0 until (array.length, 2)) print(array(i) + " ")
println
for (i <- (0 until array.length).reverse) print(array(i) + " ")
println
scala.util.Sorting.quickSort(array)
println(array.mkString("(", ",", ")"))
println(array.sum)
println(array.max)
//如果想在已有数组基础的基础上,通过操作每个元素来生成新的元素,可使用yield语法来完成。
val arrayAddedOne = for (item <- array) yield item + 1
println(arrayAddedOne.mkString(","))
val arrayEven = for (item <- array if item % 2 == 0) yield item
println(arrayEven.mkString(","))
println(array.filter { x => x % 2 == 0 }.mkString(" "))
println(array.filter { x => x % 2 == 0 }.map{ x=> x*10}.mkString(" "))
}
}