zoukankan      html  css  js  c++  java
  • scala的多种集合的使用(5)之数组Array(ArrayBuffer)的操作

    1.创建和更新数组的不同方式

    1)定义一个数组的初始大小和类型,随后填充值。

    scala> val array = new Array[String](3)
    array: Array[String] = Array(null, null, null)
    
    scala> array(0) = "abc"
    
    scala> array(1) = "def"
    
    scala> array(2) = "hij"
    
    scala> array
    res66: Array[String] = Array(abc, def, hij)

    2)创建指向数组的var引用,然后赋值给它。

    scala> var fruits: Array[String] = _
    fruits: Array[String] = null
    
    scala> fruits = Array("apple","orange","banana")
    fruits: Array[String] = [Ljava.lang.String;@79a73e5e
    
    scala> fruits
    res67: Array[String] = Array(apple, orange, banana)

    3)集合混合类型组成。

    scala> val x =Array(1,2.0,33D,400L)
    x: Array[Double] = Array(1.0, 2.0, 33.0, 400.0)

    4)集合混合类型组成,可以有自己控制。下面的例子的数组保持了原有数组的类型。

    scala> val x =Array[Number](1,2.0,33D,400L)
    x: Array[Number] = Array(1, 2.0, 33.0, 400)

    5)range创建和填充数组。

    scala> val x = Array.range(1,10)
    x: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    
    scala> val x = Array.range(1,10,2)
    x: Array[Int] = Array(1, 3, 5, 7, 9)

    6)fill创建和填充数组。

    scala> val x = Array.fill(3)("hello")
    x: Array[String] = Array(hello, hello, hello)

    7)tabulate创建和填充数组。

    scala> val x = Array.tabulate(5)(i => i * i)
    x: Array[Int] = Array(0, 1, 4, 9, 16)

    8)将list集合转化为数组的形式。

    scala> val x = List(1,2,3,4,5).toArray
    x: Array[Int] = Array(1, 2, 3, 4, 5)

    9)将字符串转化为数组的形式。

    scala> "hello".toArray
    res68: Array[Char] = Array(h, e, l, l, o)

    10)通过()的索引,就可以访问对应的元素。

    scala> val x = Array(1,3,4,5,7,9)
    x: Array[Int] = Array(1, 3, 4, 5, 7, 9)
    
    scala> x(0)
    res70: Int = 1
    
    scala> x(3)
    res71: Int = 5

    11)更新数组就是把访问的元素给到一个值即可完成更新。

    scala> x(3) = 30
    
    scala> x
    res73: Array[Int] = Array(1, 3, 4, 30, 7, 9)

    2.创建大小可变的数组(ArrayBuffer)

    1)Array是可变的,因为它的内容可以改变,但是其大小是不能进行改变的。想要创建一个大小和值都可变的索引序列,可以使用ArrayBuffer。

    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer
    
    scala> var x = ArrayBuffer[String]()
    x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
    
    scala> x += "aa"
    res74: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa)
    
    scala> x += "bb"
    res75: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb)
    
    scala> x += "cc"
    res76: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb, cc)
    
    scala> x
    res77: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb, cc)

    2)可以在创建ArrayBuffer时就添加元素,之后继续添加。

    scala> val x = collection.mutable.ArrayBuffer("aa","bb")
    x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb)
    
    scala> x += "cc"
    res78: x.type = ArrayBuffer(aa, bb, cc)

    3)可以使用++=添加元素到数组中去。

    scala> val x = collection.mutable.ArrayBuffer("aa","bb")
    x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb)
    
    scala> x ++= Seq("dd","ee")
    res79: x.type = ArrayBuffer(aa, bb, dd, ee)

    4)可以使append方法添加元素到数组中去。

    scala> val x = collection.mutable.ArrayBuffer("aa","bb")
    x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb)
    
    scala> x.append("ww","oo","pp")
    
    scala> x
    res81: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb, ww, oo, pp)

    3.删除Array和ArrayBuffer中的元素

     1)使用-=删除一个或者多个元素。

    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer
    
    scala> val x = ArrayBuffer("c","d","a","e","f")
    x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(c, d, a, e, f)
    
    scala> x -= "a"
    res82: x.type = ArrayBuffer(c, d, e, f)
    
    scala> x -= ("c","d")
    res83: x.type = ArrayBuffer(e, f)

    2)使用--=删除定义在另一个集合中的多个元素。

    scala> val x = ArrayBuffer("c","d","a","e","f")
    x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(c, d, a, e, f)
    
    scala> x --= Seq("a","c")
    res84: x.type = ArrayBuffer(d, e, f)
    
    scala> x --= Array("d")
    res85: x.type = ArrayBuffer(e, f)
    
    scala> x --= Set("f")
    res86: x.type = ArrayBuffer(e)

    3)根据ArrayBuffer中元素的位置使用remove删除元素,或者根据开始位置删除一系列的元素。

    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer
    
    scala> val x = ArrayBuffer("c","d","a","e","f")
    x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(c, d, a, e, f)
    
    scala> x.remove(0)
    res0: String = c
    
    scala> x
    res1: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(d, a, e, f)
    
    scala> x.remove(1,3)
    
    scala> x
    res3: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(d)

    4)clear方法可以删除数组中的所有元素。

    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer
    
    scala> val x = ArrayBuffer("c","d","a","e","f")
    x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(c, d, a, e, f)
    
    scala> x.clear
    
    scala> x
    res5: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()

    5)定义数组变量var,通过过滤的方式删除元素,重新将结果赋给自己。

    scala> var a = Array("apple","orange","banana")
    a: Array[String] = Array(apple, orange, banana)
    
    scala> a = a.take(2)
    a: Array[String] = [Ljava.lang.String;@405223e4
    
    scala> a
    res6: Array[String] = Array(apple, orange)

    4.数组的排序

    将一个数组或者ArrayBuffer中的元素进行排序。如果正在使用的是一个包含隐式排序元素的Array,可以使用scala.util.Sorting.quickSort方法对该Array进行排序。

    scala> val x = Array("cherry","apple","banana")
    x: Array[String] = Array(cherry, apple, banana)
    
    scala> scala.util.Sorting.quickSort(x)
    
    scala> x
    res8: Array[String] = Array(apple, banana, cherry)
    
    scala> val x = Array(2,4,1,9,5,7)
    x: Array[Int] = Array(2, 4, 1, 9, 5, 7)
    
    scala> scala.util.Sorting.quickSort(x)
    
    scala> x
    res10: Array[Int] = Array(1, 2, 4, 5, 7, 9)

    5.创建多维数组

    1)使用Array.ofDim方法创建所需要的数组。

    scala> val rows = 2
    rows: Int = 2
    
    scala> val cols = 3
    cols: Int = 3
    
    scala> val x = Array.ofDim[String](rows,cols)

    2)给多维数组添加元素。

    scala> x(0)(0) = "a"
    
    scala> x(0)(1) = "b"
    
    scala> x(0)(2) = "c"
    
    scala> x(1)(0) = "d"
    
    scala> x(1)(1) = "e"
    
    scala> x(1)(2) = "f"
    
    scala> x
    res17: Array[Array[String]] = Array(Array(a, b, c), Array(d, e, f))

    3)根据索引的具体位置访问元素。

    scala> val a = x(1)(1)
    a: String = e

    用for循环遍历数组的元素:

    scala> for {
    	 | i <- 0 until rows
    	 | j <- 0 until cols
    	 | } println(s"($i)($j) = ${x(i)(j)}")
    (0)(0) = a
    (0)(1) = b
    (0)(2) = c
    (1)(0) = d
    (1)(1) = e
    (1)(2) = f

    4)创建三维数组。 

    scala> val x,y,z = 3
    x: Int = 3
    y: Int = 3
    z: Int = 3
    
    scala> val a = Array.ofDim[Int](x,y,z)
    a: Array[Array[Array[Int]]] = Array(Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0,0, 0), Array(0, 0, 0), Array(0, 0, 0)))
    
    scala> for {
         | i <- 0 until x
         | j <- 0 until y
         | z <- 0 until z
         | } println(s"($i)($j)($z) = ${a(i)(j)(z)}")
    (0)(0)(0) = 0
    (0)(0)(1) = 0
    (0)(0)(2) = 0
    (0)(1)(0) = 0
    (0)(1)(1) = 0
    (0)(1)(2) = 0
    (0)(2)(0) = 0
    (0)(2)(1) = 0
    (0)(2)(2) = 0
    (1)(0)(0) = 0
    (1)(0)(1) = 0
    (1)(0)(2) = 0
    (1)(1)(0) = 0
    (1)(1)(1) = 0
    (1)(1)(2) = 0
    (1)(2)(0) = 0
    (1)(2)(1) = 0
    (1)(2)(2) = 0
    (2)(0)(0) = 0
    (2)(0)(1) = 0
    (2)(0)(2) = 0
    (2)(1)(0) = 0
    (2)(1)(1) = 0
    (2)(1)(2) = 0
    (2)(2)(0) = 0
    (2)(2)(1) = 0
    (2)(2)(2) = 0
    

      

  • 相关阅读:
    笔记-归并排序
    Repeated Substring Pattern
    Assign Cookies
    Number of Boomerangs
    Paint Fence
    Path Sum III
    Valid Word Square
    Sum of Two Integers
    Find All Numbers Disappeared in an Array
    First Unique Character in a String
  • 原文地址:https://www.cnblogs.com/zhaohadoopone/p/9530913.html
Copyright © 2011-2022 走看看