数组操作实战详解与worksheet初体验

场景

数组的语法与使用


实验

package com.scode.scala
import scala.collection.mutable.ArrayBuffer

/**
 * author: Ivy Peng
 * function: 数组操作实战详解
 * date:2016/02/22 22.37
 * sum:
 * Eclipse worksheet具有实时初步编译的功能,相当好用啊
 */
object ArraysInAction
{
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  /**
     * 数组的定义 语法:
     * new [Array|ArrayBuffer][DType](size)
     */
    val nums = new Array[Int](10)                 //> nums  : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    val strs = new Array[String](10)              //> strs  : Array[String] = Array(null, null, null, null, null, null, null, null
                                                  //| , null, null)
    val s = Array("Hello","World")                //> s  : Array[String] = Array(Hello, World)
    s(0) = "Good Bye"
    
    /**
     * 数组的基本操作 与 遍历
     * 语法: arrayName(i)
     */
    val b = ArrayBuffer[Int]()                    //> b  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
    b+=1                                          //> res0: com.scode.scala.ArraysInAction.b.type = ArrayBuffer(1)
    b+=(1,2,3,5)                                  //> res1: com.scode.scala.ArraysInAction.b.type = ArrayBuffer(1, 1, 2, 3, 5)
    b++=Array(3,5,5)                              //> res2: com.scode.scala.ArraysInAction.b.type = ArrayBuffer(1, 1, 2, 3, 5, 3, 
                                                  //| 5, 5)
    
    b.trimEnd(3)
    b.insert(0, 99)
    b.remove(0)                                   //> res3: Int = 99
    b.insert(0, 2,22,222)
    b.remove(2, 3)
    b.toArray                                     //> res4: Array[Int] = Array(2, 22, 2, 3, 5)
    for(i <- 0 until b.length)
    {
      println(i +":" + b(i))                      //> 0:2
                                                  //| 1:22
                                                  //| 2:2
                                                  //| 3:3
                                                  //| 4:5
    }
    
    /**
     * 数组的高级操作 与 多维数组
     */
    val c = Array(2,3,58)                         //> c  : Array[Int] = Array(2, 3, 58)
    val result = for(elem <- c) yield elem*3      //> result  : Array[Int] = Array(6, 9, 174)
    for(elem <- c if elem % 2 == 0) yield elem*2  //> res5: Array[Int] = Array(4, 116)
    c.filter(_%2 == 0).map(2*_)                   //> res6: Array[Int] = Array(4, 116)
    
    Array(1,2,3).sum                              //> res7: Int = 6
    ArrayBuffer("Ivy","Daisy","Shasha").max       //> res8: String = Shasha
    val d = ArrayBuffer(1,2,7,9)                  //> d  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7, 9)
    val dSorted = d.sorted                        //> dSorted  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7,
                                                  //|  9)
    
    val e = Array(1,3,5,3,4)                      //> e  : Array[Int] = Array(1, 3, 5, 3, 4)
    scala.util.Sorting.quickSort(e)
    e.mkString(" and ")                           //> res9: String = 1 and 3 and 3 and 4 and 5
    e.mkString("{", ":", "}")                     //> res10: String = {1:3:3:4:5}
    
    //多维数组
    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)
     }
}

参考文献

scala 深入浅出实战经典 . 王家林

你可能感兴趣的:(数组操作实战详解与worksheet初体验)