zoukankan      html  css  js  c++  java
  • Scala for the Impatients---(3)Working with Arrays

    Fixed-Length Arrays

    scala> val nums = new Array[Int](10)// An array of ten integers, all initialized with zero
    nums: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    
    scala> val a = new Array[String](10)// A string array with ten elements, all initialized with null
    a: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
    
    scala> val s = Array("Hello", "World")// An Array[String] of length 2—the type is inferred
    s: Array[String] = Array(Hello, World)

    Variable-Length Arrays: Array Buffers

    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer
    
    scala> val b = ArrayBuffer[Int]()
    b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
    
    scala> val b = new ArrayBuffer[Int]
    b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
    
    scala> b += 1
    res1: b.type = ArrayBuffer(1)
    
    scala> b += (1, 2, 3, 5)
    res2: b.type = ArrayBuffer(1, 1, 2, 3, 5)
    
    scala> b ++= Array(8, 13, 21)\you can append any collection with the ++= operator
    res3: b.type = ArrayBuffer(1, 1, 2, 3, 5, 8, 13, 21)
    
    scala> b.trimEnd(5)
    
    scala> b
    res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
    scala> val a=b.toArray//arraybuffer to array
    a: Array[Int] = Array(1, 1, 2)
    
    scala> a.toBuffer//array to arraybuffer
    res8: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 1, 2)

    Traversing Arrays and Array Buffers

    scala> for (i <- 0 until b.length)
         | println(i + ": " + b(i))
    0: 1
    1: 1
    2: 6
    3: 6
    4: 6
    5: 2

    The variable i goes from 0 to a.length - 1 .

    scala> 0 until 10
    res15: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    
    scala> 0 to 10
    res16: scala.collection.immutable.Range.Inclusive = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    To visit every second element, let i traverse this

    scala> for (i <- 0 until (b.length,2))
         | println(i + ": " + b(i))
    0: 1
    2: 6
    4: 6

    If you don’t need the array index in the loop body, visit the array elements directly, like this:

    scala> for (elem <- b)
         | println(elem)
    1
    1
    6
    6
    6
    2

    Transforming Arrays

    Transformations don’t modify the original array, but they yield a new one.

    scala> val a = Array(2, 3, 5, 7, 11)
    a: Array[Int] = Array(2, 3, 5, 7, 11)
    
    scala> val result = for (elem <- a) yield 2 * elem
    result: Array[Int] = Array(4, 6, 10, 14, 22)

    Oftentimes, when you traverse a collection, you only want to process the elements that match a particular condition. This is achieved with a guard: an if inside the for:

    scala> for (elem<-a if elem%2==0) yield 2 * elem
    res30: Array[Int] = Array(4)

    Given a sequence of integers, we want to remove all but the first negative number:

    val indexes = for (i <- 0 until a.length if a(i) < 0) yield i
    for (j <- (1 until indexes.length).reverse) a.remove(indexes(j))

    Common Algorithms

    scala> Array(1, 7, 2, 9).sum
    res31: Int = 19
    
    scala> ArrayBuffer("Mary", "had", "a", "little", "lamb").max
    res1: String = little
    
    scala> val b = ArrayBuffer(1, 7, 2, 9)
    b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 9)
    
    scala> val bSorted = b.sorted
    bSorted: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7, 9)
    
    scala> val bSorted = b.sortWith(_ > _)
    bSorted: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(9, 7, 2, 1)
    
    scala> b.mkString(" and ")
    res2: String = 1 and 7 and 2 and 9

    Multidimensional Arrays

    Multidimensional arrays are implemented as arrays of arrays. A two-dimensional array of Double values has the type Array[Array[Double]], to construct such an array, use the ofDim method:

    scala> val matrix = Array.ofDim[Double](3, 4) // Three rows, four columns
    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))

    Access an element: matrix(row)(column) = 42

    With varying row lengths:

    scala> val triangle = new Array[Array[Int]](10)\只指定了10列,和每一列的类型,即Array[Int],但是没有指定每一列的元素个数。
    triangle: Array[Array[Int]] = Array(null, null, null, null, null, null, null, null, null, null)
    
    scala> for (i <- 0 until triangle.length)
         | triangle(i) = new Array[Int](i + 1)
    
    scala> triangle
    res2: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Array(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))
  • 相关阅读:
    wpf
    UseFul Collection
    关于一些程序中用到的新方法与拓展
    冒泡排序
    学习java的第三天,猜字符的小程序
    ThinkPHP中,运用PHPExcel,将数据库导出到Excel中
    关于maven下,lombok的安装
    关于windows下activeMQ的安装
    关于windows下redis的安装
    find 练习
  • 原文地址:https://www.cnblogs.com/chaseblack/p/5410714.html
Copyright © 2011-2022 走看看