• 3、scala数组


    一、Array 、Array Buffer

    1、Array

    在Scala中,Array代表的含义与Java中类似,也是长度不可改变的数组。
    
    此外,由于Scala与Java都是运行在JVM中,双方可以互相调用,因此Scala数组的底层实际上是Java数组。例如字符串数组在底层就是Java的String[],
    整数数组在底层就是JaVa的Int[]。
    
    数组初始化后,长度就固定下来了,而且元素全部根据其类型初始化:
        val a=new Array[Int](10)
        val a=new Array[String](10)
    可以直接使用Array()创建数组,元素类型自动推断:
        val a=Array("hello","world")
        a(0)= "hi"
    
    
    
    
    ######
    scala> val a = new Array[Int](10)
    a: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    
    scala> a(0) = 1    #scala 用() 访问元素
    
    scala> a
    res57: Array[Int] = Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    
    scala> a(1)
    res58: Int = 0
    
    
    ######
    scala> val a = new Array[String](10)
    a: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
    
    scala> a(1)
    res60: String = null
    
    scala> a(1) = "leo"
    
    scala> a(1)
    res62: String = leo
    
    
    ######
    scala> val a = Array("hello", "word")
    a: Array[String] = Array(hello, word)
    
    scala> a
    res63: Array[String] = Array(hello, word)
    
    
    scala> a(0) = "Hi"
    
    scala> a
    res66: Array[String] = Array(Hi, word)
    
    
    ######
    scala> val a = Array("leo", 30)
    a: Array[Any] = Array(leo, 30)        #自动类型推断
    
    scala> a.length
    res67: Int = 2


    2、Array Buffer

    在Scala中,如果需要类似于Java中的ArrayList这种长度可变的集合类,则可以使用ArrayBuffer。
    
    //如果不想每次都使用全限定名,则可以预先导入ArrayBuffer类
      import scala.collection.mutable.ArrayBuffer
    
    //使用ArrayBufer()的方式可以创建一个空的ArrayBuffer
      val b=ArrayBuffertlnty()
    
    //使用+=操作符,可以添加一个元素,或者多个元素
      b += 1
      b +=(2,3,4,5)
    
    /使用++=操作符,可以添加其他集合中的所有元素
      b ++= Array(6,7,8,9,10)
    
    //使用trimEnd()函数,可以从尾部截断指定个数的元素
      b.trimEnd(5)
    
    
    
    ######
    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer
    
    scala> val b = ArrayBuffer[Int]()
    b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
    
    scala> b += 1        #scala特有语法,往数组中加元素
    res68: b.type = ArrayBuffer(1)
    
    scala> b += (2,3,4,5)
    res69: b.type = ArrayBuffer(1, 2, 3, 4, 5)
    
    scala> b ++= Array(6,7,8,9,10)
    res70: b.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    
    scala> b.trimEnd(5)
    
    scala> b
    res72: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
    
    
    
    ######
    /使用insert()函数可以在指定位置插入元素;
    
    /但是这种操作效率很低,因为需要移动指定位置后的所有元素
      b.insert(5,6)
      b.insert(6,7,8,9,10)
    
    /使用remove()函数可以移除指定位置的元素
      b.remove(1)
      b.remove(1,3)
    
    //Array与ArrayBuffer可以互相进行转换
      b.toArray 
      a.toBuffer
    
    
    ######
    scala> b
    res73: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
    
    scala> b.insert(5,6)
    
    scala> b
    res75: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6)
    
    scala> b.insert
    insert   insertAll
    
    scala> b.insert(6,7,8,9,10)
    
    scala> b
    res77: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    
    scala> b.remove(1)
    res78: Int = 2
    
    scala> b
    res79: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 4, 5, 6, 7, 8, 9, 10)
    
    scala> b.remove(1,3)
    
    scala> b
    res81: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 6, 7, 8, 9, 10)
    
    scala> var bArray = b.toArray
    bArray: Array[Int] = Array(1, 6, 7, 8, 9, 10)
    
    scala> var aArrayBuffer = a.toBuffer
    aArrayBuffer: scala.collection.mutable.Buffer[Any] = ArrayBuffer(leo, 30)


    3、遍历Array ArrayBuffer

    //使用for循环和until遍历Array/ArrayBufer
    
    /使until是Richlnt提供的函数
      for(i<-0 until b.length)
        printin(b(i))
    
    //跳跃遍历Array/ArrayBuffer 
      for(i <- 0 until(b.length,2))
        printin(b(i)
    
    //从尾部遍历Array/ArrayBuffer 
      for(i<-(0 until b.length).reverse)
        printin(bi))
    
    /使用“增强for循环”遍历Array/ArrayBuffer 
      for(e <- b)
        printin(e)
    
    
    
    ######
    scala> for (i <- 0 until b.length) println(b(i))
    1
    6
    7
    8
    9
    10
    
    scala> for (i <- 0 until (b.length, 2)) println(b(i))        #指定步长
    1
    7
    9
    
    scala> for (i <- (0 until b.length).reverse) println(b(i))        #反转
    10
    9
    8
    7
    6
    1
    
    scala> for(e <- b) println(e)        #增强for
    1
    6
    7
    8
    9
    10


    4、数组常见操作

    /数组元素求和
    val a=Array(1,2,3,4,5)
    val sum=a.sum
    
    /获取数组最大值
    val max=a.max
    
    /对数组进行排序
    scala.util.Sorting.quickSort(a)
    
    /获取数组中所有元素内容
    a.mkString 
    a.mkString(",")        #指定分隔符
    a.mkString("<",",",">")
    
    //toString函数
    a.toString
    b.toString


    二、数组转换

    1、使用yield和函数式编程转换数组

    //对Array进行转换,获取的还是Array 
     val a=Array(1,2,3,4,5)
     val a2=for(ele <- a)yield ele * ele
    
    //对ArayBuffer进行转换,获取的还是ArrayBuffer 
    val b=ArrayBuffer[In]()
    b +=(1,2,3,4,5)
    val b2=for(ele <-b)yield ele * ele
    
    /结合if守卫,仅转换需要的元素
    val a3 = for(ele <- if ele % 2 == 0)yield ele * ele
    
    /使用函数式编程转换数组(通常使用第一种方式)
    a.filter (_%2 == 0).map(2 * _)
    a.filter {_ % 2 == 0}map{2 * _}
    
    
    ##scala  yield
    对于for循环的每次迭代,yield都会生成一个将被记住的值。就像有一个你看不见的缓冲区,for循环的每一次迭代都会将另一个新的值添加到该缓冲区。
    
    
    当for循环结束运行时,它将返回所有已赋值的集合。返回的集合的类型与迭代产生的类型相同,因此Map会生成Map,List将生成List,等等。
    
    
    另外请注意,最初的集合没有改变。for / yield构造根据您指定的算法创建一个新的集合。
    
    
    ######
    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer
    
    scala> val b = ArrayBuffer[Int]()
    b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
     
    scala> b += (1,2,3,4,5)
    res1: b.type = ArrayBuffer(1, 2, 3, 4, 5)
    
    scala> val b2 = for(ele <- b) yield ele * ele
    b2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 4, 9, 16, 25)
    
    scala> val a3 = for(ele <- a if ele % 2 == 0) yield ele * ele        #yield 
    a3: Array[Int] = Array(4, 16)
    
    scala> a.filter(_ % 2 == 0).map(_ * 2)                #
    res2: Array[Int] = Array(4, 8)
    
    scala> a.filter { _ % 2 == 0} map{ _ * 2}
    res3: Array[Int] = Array(4, 8)


    2、算法案例

    移除第一个负数之后的所有负数;

    /构建数组
    val a = ArrayBuffer[inty()
    a +=(1,2,3,4,5,-1,-3,-5,-9)
    
    /每发现一个第一个负数之后的负数,就进行移除,性能较差,多次移动数组
    var foundFirstNegative = false 
    var arrayLength = a.length 
    var index = 0
    while(index < arrayLength){
      if(a(index)>= 0){
        index += 1
    }else{
      if(!foundFirstNegative){foundFirstNegative = true;index += 1}
      else{a.remove(index);arrayLength -= 1}
      }
    }
    
    
    
    ############
    //性能较高的

    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer

    scala> val a = ArrayBuffer[Int]()
    a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

    scala> a += (1,2,3,4,5,-1,-3,-5,-9)
    res0: a.type = ArrayBuffer(1, 2, 3, 4, 5, -1, -3, -5, -9)

    scala> :paste
    // Entering paste mode (ctrl-D to finish)

    var fountFirstNegative = false
    val keepIndexes = for (i <- 0 until a.length if !fountFirstNegative || a(i) >= 0
    ) yield {
       if (a(i) < 0) fountFirstNegative = true
       i
    }
    for (i <- 0 until keepIndexes.length) { a(i) = a(keepIndexes(i)) }
    a.trimEnd(a.length - keepIndexes.length)

    // Exiting paste mode, now interpreting.

    fountFirstNegative: Boolean = true
    keepIndexes: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 1, 2, 3, 4, 5)

    scala> a
    res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, -1)

  • 相关阅读:
    整合规则引擎urule
    vue学习
    发送put请求,get请求
    jpa自定义字段
    spring的3种配置方式
    netty
    springsercurity和shiro
    git报错
    Scrapy全站数据爬取
    python操作Excel模块openpyxl
  • 原文地址:https://www.cnblogs.com/weiyiming007/p/10971838.html
Copyright © 2020-2023  润新知