scala Array 介绍

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jslijb/article/details/79968085

目录 scala Array

创建 Array

scala> val a = Array("abc","def","hijk")
a: Array[String] = Array(abc, def, hijk)

scala> val b: Array[String] = new Array[String](3)
b: Array[String] = Array(null, null, null)

scala> b(0) = "alibaba"

scala> b(1) = "baidu"

scala> b(2) = "qq"

scala> b
res3: Array[String] = Array(alibaba, baidu, qq)

scala> val c = new Array[String](4)
c: Array[String] = Array(null, null, null, null)

scala> c(0) = "id"

scala> c(1) = "age"

scala> c(2) = "class"

scala> c(3) = "sore"

scala> c
res16: Array[String] = Array(id, age, class, sore)

scala> import Array._
import Array._

scala> val myMatrix = ofDim(3)
myMatrix: Array[Nothing] = Array(null, null, null)

遍历 Array

  • for 循环
  • foreach
scala> for (x <- c) { println(x) }
id
age
class
sore

scala> c foreach println
id
age
class
sore

Array 最大值

// 字符串只比较首字母对应ascII 值
scala> c.max
res23: String = sore

scala> c
res24: Array[String] = Array(id, age, class, sore)

Array 的长度

scala> c
res24: Array[String] = Array(id, age, class, sore)

scala> c.length
res21: Int = 4

创建多维数组

import Array._

object ofDim1 {

    def main(args: Array[String]) {
        val myMatrix = ofDim[Int](3,3)

        for( i <- 0 to 2) {
            for (j <- 0 to 2) {
                myMatrix(i)(j) = j
            }
        }

        for( i <- 0 to 2) {
            for (j <- 0 to 2) {
                print(" " + myMatrix(i)(j))
            }
        println()
        }
    }
}

/*
output:
[root@spark1 Array]# scalac ofDim.scala 
[root@spark1 Array]# scala ofDim1
 0 1 2
 0 1 2
 0 1 2
*/

Array 包中的函数 需要导入 import Array._

def apply( x: T, xs: T* ): Array[T] 创建数组

  • 可创建Int,Float,Short,Double,Long,Byte,Char,Unit,Boolean 等类型是数组
  • 可以访问已经存在数组中的某个元素
scala> val b = Array.apply("abc",123,23.1,34.5F,'b',"ture","false")
b: Array[Any] = Array(abc, 123, 23.1, 34.5, b, ture, false)

scala> b.apply(3)
res44: Any = 34.5

scala> b.apply(5)
res45: Any = ture

scala> b.apply((3,5))
<console>:16: error: type mismatch;
 found   : (Int, Int)
 required: Int
       b.apply((3,5))
               ^

scala> b.apply(3,5)
<console>:16: error: too many arguments for method apply: (i: Int)Any in class Array
       b.apply(3,5)
              ^

def concat[T]( xss: Array[T]* ): Array[T] 合并数组,所有的数组必须是同一类型


scala> a
res48: Array[String] = Array(abc, def, hijk)

scala> b
res49: Array[Any] = Array(abc, 123, 23.1, 34.5, b, ture, false)
scala> concat(a,b)
<console>:17: error: type mismatch;
 found   : Array[String]
 required: Array[Any]
Note: String <: Any, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
       concat(a,b)
              ^

scala> a
res53: Array[String] = Array(abc, def, hijk)

scala> b
res54: Array[Any] = Array(abc, 123, 23.1, 34.5, b, ture, false)

scala> val b = Array("baidu","ali","qq")
b: Array[String] = Array(baidu, ali, qq)

scala> concat(a,b)
res55: Array[String] = Array(abc, def, hijk, baidu, ali, qq)

scala> val b = Array("baidu","ali","qq","souhu")
b: Array[String] = Array(baidu, ali, qq, souhu)

scala> concat(a,b)
res56: Array[String] = Array(abc, def, hijk, baidu, ali, qq, souhu)

scala> val a = Array("key1",1,"key2",2.4,"key3",3.5F,"key4",34L,"key5",'c')
a: Array[Any] = Array(key1, 1, key2, 2.4, key3, 3.5, key4, 34, key5, c)

scala> val b = Array("k1",4,"k2",5.4,"k3",7.5F,"k4",4L,"k5",'b')
b: Array[Any] = Array(k1, 4, k2, 5.4, k3, 7.5, k4, 4, k5, b)

scala> concat(a,b)
res65: Array[Any] = Array(key1, 1, key2, 2.4, key3, 3.5, key4, 34, key5, c, k1, 4, k2, 5.4, k3, 7.5, k4, 4, k5, b)

scala> val b = Array("baidu","ali","qq",12)
b: Array[Any] = Array(baidu, ali, qq, 12)

scala> concat(a,b)
<console>:17: error: type mismatch;
 found   : Array[String]
 required: Array[Any]
Note: String <: Any, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
       concat(a,b)
              ^

scala> val a1 = Array(1,2,3,6,3)
a1: Array[Int] = Array(1, 2, 3, 6, 3)

scala> val a2 = Array(3,4)
a2: Array[Int] = Array(3, 4)

scala> contact(a1,a2)
<console>:17: error: not found: value contact
       contact(a1,a2)
       ^

scala> concat(a1,a2)
res59: Array[Int] = Array(1, 2, 3, 6, 3, 3, 4)

scala> concat(a2,a1)
res60: Array[Int] = Array(3, 4, 1, 2, 3, 6, 3)

scala> val a2 = Array(3.3,3.5)
a2: Array[Double] = Array(3.3, 3.5)

scala> concat(a2,a1)
<console>:17: error: type mismatch;
 found   : Array[Double]
 required: Array[AnyVal]
Note: Double <: AnyVal, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: AnyVal`. (SLS 3.2.10)
       concat(a2,a1)
              ^
<console>:17: error: type mismatch;
 found   : Array[Int]
 required: Array[AnyVal]
Note: Int <: AnyVal, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: AnyVal`. (SLS 3.2.10)
       concat(a2,a1)
                 ^

scala> concat(a1,a2)
<console>:17: error: type mismatch;
 found   : Array[Int]
 required: Array[AnyVal]
Note: Int <: AnyVal, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: AnyVal`. (SLS 3.2.10)
       concat(a1,a2)
              ^
<console>:17: error: type mismatch;
 found   : Array[Double]
 required: Array[AnyVal]
Note: Double <: AnyVal, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: AnyVal`. (SLS 3.2.10)
       concat(a1,a2)
                 ^

scala> val a3 = Array(2.3,3.5,4.6,5.7)
a3: Array[Double] = Array(2.3, 3.5, 4.6, 5.7)

scala> val a4 = Array(1.2F,3.2F)
a4: Array[Float] = Array(1.2, 3.2)

scala> concat(a3,a4)
<console>:17: error: type mismatch;
 found   : Array[Double]
 required: Array[AnyVal]
Note: Double <: AnyVal, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: AnyVal`. (SLS 3.2.10)
       concat(a3,a4)
              ^
<console>:17: error: type mismatch;
 found   : Array[Float]
 required: Array[AnyVal]
Note: Float <: AnyVal, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: AnyVal`. (SLS 3.2.10)
       concat(a3,a4)
                 ^

scala> concat(a4,a3)
<console>:17: error: type mismatch;
 found   : Array[Float]
 required: Array[AnyVal]
Note: Float <: AnyVal, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: AnyVal`. (SLS 3.2.10)
       concat(a4,a3)
              ^
<console>:17: error: type mismatch;
 found   : Array[Double]
 required: Array[AnyVal]
Note: Double <: AnyVal, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: AnyVal`. (SLS 3.2.10)
       concat(a4,a3)
                 ^
// 连接3个及以上
scala> val a1 = Array(1,2,3)
a1: Array[Int] = Array(1, 2, 3)

scala> val a2 = Array(1,4,5)
a2: Array[Int] = Array(1, 4, 5)

scala> val a3 = Array(9,0)
a3: Array[Int] = Array(9, 0)

scala> val a4 = Array(12,7)
a4: Array[Int] = Array(12, 7)

scala> val a5 = concat(a1,a2,a3)
a5: Array[Int] = Array(1, 2, 3, 1, 4, 5, 9, 0)

scala> val a6 = concat(a1,a2,a3,a4,a5)
a6: Array[Int] = Array(1, 2, 3, 1, 4, 5, 9, 0, 12, 7, 1, 2, 3, 1, 4, 5, 9, 0)

def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit

  • copy(srcArray,srcPos,destArray,destPos,length)
  • srcArray : 原数组
  • destArray : 目标数组
  • srcPos: 从原数组的什么位置开始复制
  • destPos: 复制到目标数组的什么位置
  • length: 复制的长度(元素的个数)
    • length <= destArray.length - destPos
    • length <= srcArray.length - srcPos

scala> a4
res69: Array[Int] = Array(12, 7)

scala> a3
res70: Array[Int] = Array(9, 0)

scala> copy(a3,0,a4,0,2)

scala> a3
res72: Array[Int] = Array(9, 0)

scala> a4
res73: Array[Int] = Array(9, 0)

scala> a5
res75: Array[Int] = Array(1, 2, 3, 1, 4, 5, 9, 0)

scala> a6
res76: Array[Int] = Array(1, 2, 3, 1, 4, 5, 9, 0, 12, 7, 1, 2, 3, 1, 4, 5, 9, 0)

scala> copy(a5,2,a6,4,5)

scala> a5
res78: Array[Int] = Array(1, 2, 3, 1, 4, 5, 9, 0)

scala> a6
res79: Array[Int] = Array(1, 2, 3, 1, 3, 1, 4, 5, 9, 7, 1, 2, 3, 1, 4, 5, 9, 0)

// 超出范围会报错
scala> copy(a5,2,a6,4,7)
java.lang.ArrayIndexOutOfBoundsException
  at scala.Array$.copy(Array.scala:105)
  ... 32 elided

def empty[T]: Array[T] 创建空数组

scala> empty
res92: Array[Nothing] = Array()

def iterate[T]( start: T, len: Int )( f: (T) => T ): Array[T] 生成一个新的数组

  • start : 数组元素的起始值
  • len : 数组的长度
  • f:(T) : f函数产生数组中的元素。
    • 第一个输入:start,f函数的返回值为数组中的第1个元素Array(0)
    • 第二个输入: Array(0) f函数的返回值为数组中的第2个元素Array(1)
    • 第三个,第4个输入和返回值依次类推 ……
scala> def squre(a: Int) = a * a + 2
squre: (a: Int)Int

scala> iterate(0,5)(a => squre(a))
res93: Array[Int] = Array(0, 2, 6, 38, 1446)

scala> iterate(0,5)(a => a * a)
res94: Array[Int] = Array(0, 0, 0, 0, 0)

def fill[T]( n: Int )(elem: => T): Array[T] 快速创建具有相同元素的数组

  • n: 数组的长度
  • elem: 数组中的元素
  • 创建n个相同的元素为item的数组
scala> fill(2)(3)
res96: Array[Int] = Array(3, 3)

def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]] 和上一个函数类似,只是这里创建的是个2维数组,类似于矩阵

  • n1: 外层数组的长度,可以看做矩阵的行数
  • n2: 内层数组的长度,可以看做矩阵的列数
  • elem: elem 数组中的元素
  • 创建了n1 行和n2 列的2维数组,使用元素elem将其填充满
scala> fill(3,4)("abc")
res97: Array[Array[String]] = Array(Array(abc, abc, abc, abc), Array(abc, abc, abc, abc), Array(abc, abc, abc, abc))

def ofDim[T]( n1: Int ): Array[T] 创建一维空数组

def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]] 创建一个二维空数组

def ofDim[T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]] 创建一个3维空数组

scala> Array.ofDim(1)
res101: Array[Nothing] = Array(null)

scala> ofDim(2)
res103: Array[Nothing] = Array(null, null)

scala> ofDim(3)
res104: Array[Nothing] = Array(null, null, null)

scala> ofDim(4)
res105: Array[Nothing] = Array(null, null, null, null)

// ofDim(n)  创建n维空数组

def range( start: Int, end: Int, step: Int ): Array[Int] 创建区间数组,step 为步长

def range( start: Int, end: Int ): Array[Int]

// 默认步长为1
scala> range(3,6)
res110: Array[Int] = Array(3, 4, 5)

scala> range(0,20,2)
res108: Array[Int] = Array(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)

scala> range(1,20,2)
res109: Array[Int] = Array(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)

def tabulate[T]( n: Int )(f: (Int)=> T): Array[T] 使用函数f创建一维数组

def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]] 使用函数f创建2维数组

  • n: 一维数组的长度
  • n1: 二维数组的长度,也是外层数组的长度,矩阵的行
  • n2: 内层数组的长度,矩阵的列
  • f: 函数,1个参数,类型Int
    • 第1个输入:0,返回值就是数组的第一个元素Array(0)
    • 第2个输入:1,返回值就是数组的第二个元素Array(1)
    • 第3个输入:2, 返回值就是数组的第三个元素Array(2)
    • ……………………………………….

scala> tabulate(3) (a => a + 1)
res111: Array[Int] = Array(1, 2, 3)

scala> tabulate(3) (a => a * a)
res112: Array[Int] = Array(0, 1, 4)

scala> tabulate(4,3) ((x,y) => (x+1,y+2))
res119: Array[Array[(Int, Int)]] = Array(Array((1,2), (1,3), (1,4)), Array((2,2), (2,3), (2,4)), Array((3,2), (3,3), (3,4)), Array((4,2), (4,3), (4,4)))
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页