Scala-Array函数

++

定义:def ++[B](that: GenTraversableOnce[B]): Array[B]
描述:合并集合,并返回一个新的数组,新数组包含左右两个集合的内容,若类型不同合并之后一般为Array[Any]
示例:

val a = Array(1, 2)
val b = Array(3, 4)
val c = a ++ b
println(c.mkString(",")) // 1,2,3,4

++:

定义:def ++:[B >: A, That](that: collection.Traversable[B])(implicit bf: CanBuildFrom[Array[T], B, That]): That
描述:这个方法同上一个方法类似,两个加号后面多了一个冒号,但是不同的是右边的类型决定着返回结果的类型
示例:

val a = List(1, 2)
val b = scala.collection.mutable.LinkedList(3, 4)
val c = a ++: b
println(c.getClass().getName()) // c 的类型: scala.collection.mutable.LinkedList

+:

定义:def +:(elem: A): Array[A]
描述:在数组前面添加一个元素,并返回新的数组对象
示例:

a: Array[Int] = Array(1, 2, 3, 4)
scala> 0+:a
res0: Array[Int] = Array(0, 1, 2, 3, 4)

:+

定义:def :+(elem: A): Array[A]
描述:在数组后面添加一个元素,并返回新的数组对象
示例:

a: Array[Int] = Array(1, 2, 3, 4)
scala> a:+5
res2: Array[Int] = Array(1, 2, 3, 4, 5)

/:

定义:def /:[B](z: B)(op: (B, T) ⇒ B): B
描述:对数组中所有的元素从左向右遍历,进行相同的迭代操作,foldLeft 的简写
示例:

a: Array[Int] = Array(1, 2, 3, 4)
scala> (10/:a)(_+_)
res3: Int = 20    //((((10+1)+2)+3)+4

:\

定义:def :[B](z: B)(op: (T, B) ⇒ B): B
描述:对数组中所有的元素从右向左遍历,进行相同的迭代操作,foldRight 的简写
示例:

(a :\ 10)(_-_)    //1-(2-(3-(4-10)))
(a :\ 10)(_*_)
res12: Int = 240  //1*(2*(3*(4*10)))

addString(b)

定义:def addString(b: StringBuilder): StringBuilder
描述:将数组中的元素逐个添加到 StringBuilder 中
示例:

scala> var c = new StringBuilder
c: StringBuilder =

scala> a.addString(c)
res8: StringBuilder = 1234

addString(b, sep)

定义:def addString(b: StringBuilder, sep: String): StringBuilder
描述:将数组中的元素逐个添加到 StringBuilder 中,每个元素用 sep 分隔符分开
示例:

scala> var c = new StringBuilder
c: StringBuilder =

scala> a.addString(c,",")
res11: StringBuilder = 1,2,3,4

addString(b,start,sep,end)

定义:def addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder
描述:将数组中的元素逐个添加到b中,在首尾各加一个字符串,并指定sep分隔符
示例:

scala> var a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)

scala> var b = new StringBuilder
b: StringBuilder =

scala> a.addString(b,"{",",","}")
res3: StringBuilder = {1,2,3,4}

aggregate

定义:def aggregate[B](z : => B)(seqop : scala.Function2[B, A, B], combop : scala.Function2[B, B, B]) : B = { /* compiled code */ }
描述:aggregate是柯里化方法(将原来接受两个参数的函数变成新的接受一个参数的函数的过程。新的函数返回一个以原有第二个参数为参数的函数), aggregate 方法是一个聚合函数,接受多个输入,并按照一定的规则运算以后输出一个结果值
示例:为了方便理解,把 aggregate 的两个参数分别封装成两个方法,并把分区和不分区的计算过程分别打印出来

def seqno(m: Int, n: Int): Int = {
    val s = "seq_exp = %d + %d"
    println(s.format(m, n))
    m + n
  }

  def combine(m: Int, n: Int): Int = {
    val s = "com_exp = %d + %d"
    println(s.format(m, n))
    m + n
  }
  
  def main(args: Array[String]) {
    val a = List(1, 2, 3, 4)

    val b = a.aggregate(5)(seqno, combine) // 不分区
    println("b = " + b)
    /**
     * seq_exp = 5 + 1
     * seq_exp = 6 + 2
     * seq_exp = 8 + 3
     * seq_exp = 11 + 4
     * b = 15
     */
     
    val c = a.par.aggregate(5)(seqno, combine) // 分区
    println("c = " + c)
    /**
     * seq_exp = 5 + 3
     * seq_exp = 5 + 2
     * seq_exp = 5 + 4
     * seq_exp = 5 + 1
     * com_exp = 6 + 7
     * com_exp = 8 + 9
     * com_exp = 13 +17
     * c = 30
     */
  }

通过上面的运算不难发现,不分区时,seqno 是把初始值顺序和每个元素相加,把得到的结果与下一个元素进行运算。
分区时,seqno 是把初始值与每个元素相加,但结果不参与下一步运算,而是放到另一个序列中,由第二个方法 combine 进行处理。

andThen

定义:override def andThen[C](k : scala.Function1[B, C]) : scala.PartialFunction[A, C] = { /* compiled code */ }
描述:表示方法的连续调用,相当于嵌套函数 a(b(x)(需要满足第一个函数的返回值类型为第二个函数的输入值类型),与 compose 相反。
示例:

先执行第一个函数,将第一个函数的结果作为第二个函数的参数,进行操作

def f(a:Int) = {
 println("2*"+a)
 2*a
}

def g(b:Int) = {
 println("3*"+b)
 3*b
}

def  result1 = f _ andThen g _
println(result1(1))
/*
2*1
3*2
6
/
def  result2 = f _ compose  g _
println(result2(1))
/*
3*1
2*3
6
*/

apply

定义:def apply(i: Int): T
描述:获取指定索引处的元素
示例:

a: Array[Int] = Array(1, 2, 3, 4)
scala> a.apply(2)
res13: Int = 3
相当于
scala> a(2)
res4: Int = 3

applyOrElse

定义: def applyOrElse[A1 <: A, B1 >: B](x : A1, default : scala.Function1[A1, B1]) : B1 = { /* compiled code */ }
描述:接收2个参数,第一个是调用的参数,第二个是个回调函数。如果第一个调用的参数能匹配,返回匹配的值,否则调用回调函数。(回调函数的输入类型需与调用参数类型一致),就相当于if-else分支调用函数。
示例:

scala> val pf1:PartialFunction[Int,String] = {   		//定义一个偏函数,输入 1 返回 "one"
     | case i if i == 1 => "One"
     | }
pf1: PartialFunction[Int,String] = <function1>

scala> pf1.applyOrElse(1,{num:Int=>"two"})  		   //输入1匹配到"one"
res16: String = One

scala> pf1.applyOrElse(2,{num:Int=>"two"})  		  //输入2匹配失败则触发回调函数,返回 "Two" 字符串
res17: String = two

canEqual

定义:def canEqual(that: Any): Boolean
描述:判断两个对象是否可以进行比较
示例:基本上所有对象都可以进行比较,我不知道这个方法的意义何在

val a = List(1, 2, 3, 4)
val b = Array('a', 'b', 'c')
println(a.canEqual(b)) // true

charAt

定义:def charAt(index: Int): Char
描述:获取 index 索引处的字符,这个方法会执行一个隐式的转换,将 Array[T] 转换为 ArrayCharSequence,只有当 T 为 Char 类型时,这个转换才会发生
示例:

scala> var c = Array('a','b','c')
c: Array[Char] = Array(a, b, c)

scala> c.charAt(1)
res18: Char = b

clone

定义:def clone(): Array[T]
描述: 创建一个数组的副本,复制数组的值,但不会引用地址;就是说是浅克隆,不是深克隆。
返回值: 数组
示例:

val a = List(1, 2, 3, 4)
scala> a.clone()
res20: Array[Int] = Array(1, 2, 3, 4)

collect

定义:def collect[B](pf: PartialFunction[A, B]): Array[B]
描述:通过执行一个并行计算(偏函数),得到一个新的数组对象
示例:通过下面的偏函数,把数组中的小写的 a 转换为大写的 A

val fun: PartialFunction[Char, Char] = {
  case 'a' => 'A'
  case x => x
}
scala> c.collect(fun)
res30: Array[Char] = Array(A, b, c)

另一个示例

scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)

scala> val fun:PartialFunction[Int,Int]={
     | case x if x%2==0 => x+2
     | case x => x+1
     | }
fun: PartialFunction[Int,Int] = <function1>

scala> a.collect(fun)
res45: Array[Int] = Array(2, 4, 4, 6)

scala> val fun:PartialFunction[Int,Int]={
     | case x if x%2==0 => x+2
     | case _ => 1
     | }
fun: PartialFunction[Int,Int] = <function1>

scala> a.collect(fun)
res46: Array[Int] = Array(1, 4, 1, 6)

collectFirst

定义:def collectFirst[B](pf: PartialFunction[T, B]): Option[B]
描述:在序列中查找第一个符合偏函数定义的元素,并执行偏函数计算
示例:定义一个偏函数,当被执行对象为 Int 类型时,进行乘 100 的操作

scala> val a = Array(1, 'a', "b")
a: Array[Any] = Array(1, a, b)

val fun: PartialFunction[Any, Int] = {
  case x: Int => x * 100
}

scala> a.collectFirst(fun)
res32: Option[Int] = Some(100)

scala> b.get
res48: Int = 100

combinations

定义:def combinations(n: Int): collection.Iterator[Array[T]]
描述:combinations 表示组合,这个排列组合会选出所有包含字符不一样的组合,但不考虑顺序,对于 “abc”、“cba”,视为相同组合,参数 n 表示序列长度,就是几个字符为一组
示例:

scala> var a = Array("a","b","c")
a: Array[String] = Array(a, b, c)
scala> a.combinations(2).foreach(x=>println(x.mkString(",")))
a,b
a,c
b,c

companion

定义: override def companion:scala.collection.generic.GenericCompanion[scala.collection.mutable.IndexedSeq]
描述:伴生对象
示例:

scala> a
res35: Array[Int] = Array(3, 4, 5, 2)
scala> a.companion(2)
res32: scala.collection.mutable.IndexedSeq[Int] = ArrayBuffer(2)

scala> a.toList.companion(2)
res33: List[Int] = List(2)

compose

定义:def compose[A](g : scala.Function1[A, T1]) : scala.Function1[A, R] = { /* compiled code */ }
描述:表示方法的连续调用,与 andThen 相反,先执行g,再执行f ,不同的是需满足第二个函数的返回值类型是第一个函数的输入值类型
示例:

scala> def f(a:Int) = {
     |  println("2*"+a)
     |  2*a
     | }
f: (a: Int)Int

scala> def g(b:Int) = {
     |  println("3*"+b)
     |  3*b
     | }
g: (b: Int)Int

scala> def  result1 = f _ andThen g _
result1: Int => Int

scala> println(result1(1))
2*1
3*2
6
scala> def  result2 = f _ compose  g _
result2: Int => Int

scala> println(result2(1))
3*1
2*3
6

contains

定义:def contains[A1 >: A](elem: A1): Boolean
描述:判断序列中是否包含指定对象
示例:

b: Array[Int] = Array(1, 2, 3)
scala> b.contains(2)
res44: Boolean = true

containsSlice

定义:def containsSlice[B](that: GenSeq[B]): Boolean
描述:判断当前序列中是否包含另一个序列(数量和顺序相等)
示例:

val b = Array(1,2,3,4,5)
val c = Array(4,5,6)
scala> b.containsSlice(Array(2,3,4))
res49: Boolean = true
scala> b.containsSlice(Array(5,6,7))
res48: Boolean = false

copyToArray

定义:def copyToArray(xs: Array[A]): Unit
描述:将当前数组元素复制到另一个数组中
示例:

a: Array[Int] = Array(1, 2, 3)
scala> val c = new Array[Int](5)
c: Array[Int] = Array(0, 0, 0, 0, 0)

scala> a.copyToArray(c)
res6: Array[Int] = Array(1, 2, 3, 0, 0)

copyToArray(xs, start)

定义:def copyToArray(xs: Array[A], start: Int): Unit
描述:将当前数组元素复制到另一个数组中,从 start 位置开始复制
示例:

a: Array[Int] = Array(1, 2, 3)
c: Array[Int] = Array(0, 0, 0, 0, 0)
scala> a.copyToArray(c,2) 将a拷贝到c中,从2号角标开始写入
scala> c
res19: Array[Int] = Array(0, 0, 1, 2, 3)

copyToArray(xs, start, len)

定义:def copyToArray(xs: Array[A], start: Int, len: Int): Unit
描述:将当前数组元素复制到另一个数组中,从 start 位置开始复制,长度为 len
示例:

scala> val c = new Array[Int](5)
c: Array[Int] = Array(0, 0, 0, 0, 0)

scala> a.copyToArray(c,2,2)
scala> c
res11: Array[Int] = Array(0, 0, 1, 2, 0)

由此看出:第一个2是放在数组中的位置,第二个2是原始数组里取2个

copyToBuffer

定义:def copyToBuffer[B >: A](dest: Buffer[B]): Unit
描述:将数组中的元素复制到 Buffer 中
示例:

先导包 import scala.collection.mutable.ArrayBuffer
a: List[Int] = List(1, 2, 3)
var d = new ArrayBuffer[Int]()

scala>a.copyToBuffer(d)
res8: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)

corresponds

定义:def corresponds[B](that: GenSeq[B])(p: (T, B) ⇒ Boolean): Boolean
描述:判断两个序列的长度以及对应位置元素是否符合某个条件。如果两个序列具有相同的元素数量并且 p(x, y)=true,则返回 true,不满足返回false
示例:下面代码检查 a 和 b 长度是否相等,并且 a 中元素是否小于 b

scala> a
res9: List[Int] = List(1, 2, 3)

scala> b
res10: List[Int] = List(2, 3)

scala> a.corresponds(b)(_<_)
res11: Boolean = false

scala> var c = Array(1,2,3)
c: Array[Int] = Array(1, 2, 3)
scala> a.corresponds(c)(_==_)
res13: Boolean = true

count

定义:def count(p: (T) ⇒ Boolean): Int
描述:统计符合条件的元素个数
示例:下面代码统计数组中大于 2 的元素个数

scala> b
res153: Array[Int] = Array(1, 2, 3, 4)

scala> b.count(_>2)
res154: Int = 2

scala> e
res21: Array[String] = Array(hello, world)

scala> e.count(x=>x.indexOf("o")==1)
res27: Int = 1

scala> e.count(x=>x.indexOf("o")>0)
res32: Int = 2

scala> e.count(x=>x.indexOf("o")!= -1)
res33: Int = 2

deep

定义:def deep: IndexedSeq[Any]
描述:将提供序列转型为IndexedSeq[Any]类型,序列值类型自动提升
示例:

val a = Array(1,2,4,3)		//a: Array[Int] = Array(1, 2, 4, 3)
val b = a.deep		//b: IndexedSeq[Any] = Array(1, 2, 4, 3)
println(a)		//[I@2ac5ba7a
println(b)		//Array(1, 2, 4, 3)

diff

定义:def diff(that: collection.Seq[T]): Array[T]
描述:计算当前数组与另一个数组的差集,即将当前数组中没有在另一个数组中出现的元素返回
示例:

b: Array[Int] = Array(1, 2, 3, 4)
a: Array[Int] = Array(1, 2, 3)
scala> b.diff(a)
res163: Array[Int] = Array(4)
diff里如果前组有重复的,比如23  23  23
求差集,只能去除一个,再diff再去一个

distinct

定义:def distinct: Array[T]
描述:去除当前集合中重复的元素,只保留一个
示例:

scala> var e = Array(1,2,3,1,2,5,6)
e: Array[Int] = Array(1, 2, 3, 1, 2, 5, 6)

scala> e.distinct
res170: Array[Int] = Array(1, 2, 3, 5, 6)

drop

定义:def drop(n: Int): Array[T]
描述:将当前数组中前 n 个元素去除,返回一个新数组
示例:

e = Array(1,2,3,1,2,5,6)
scala> e.drop(2)
res172: Array[Int] = Array(3, 1, 2, 5, 6)

dropRight

定义:def dropRight(n: Int): Array[T]
描述:功能同 drop,去掉尾部的 n 个元素
示例:

e = Array(1,2,3,1,2,5,6)
scala> e.dropRight(2)
res173: Array[Int] = Array(1, 2, 3, 1, 2)

dropWhile

定义:def dropWhile(p: (T) ⇒ Boolean): Array[T]
描述:去除当前数组中符合条件的元素,返回剩余的数组,这个需要一个条件,就是从当前数组的第一个元素起,就要满足条件,直到碰到第一个不满足条件的元素结束(即使后面还有符合条件的元素),否则返回整个数组
示例:

e = Array(1,2,3,1,2,5,6)
scala> e.dropWhile(_<3)
res175: Array[Int] = Array(3, 1, 2, 5, 6)

elemMainfest

定义:def elemManifest: ClassManifest[AI]
描述:
示例:

val a = Array(1,2,3,4,5)
val b = a.elemManifest	// b:ClassManifest[Int] = Int
println(b)		// Int

val a = Array(1,2,3,"a")
val b = a.elemManifest	// b: ClassManifest[Any] = java.lang.Object
println(b)		// java.lang.Object

elemTag

定义:def elemTag: scala.reflect.ClassTag[A1]
描述:
示例:

val a = Array(1,2,3,4,5)
val b = a.elemTag	// scala.reflect.ClassTag[Int] = Int
println(b)		// Int

val a = Array(1,2,3,"a")
val b = a.elemTag	// b: scala.reflect.ClassTag[Any] = Object
println(b)		// Object

endsWith

定义:def endsWith[B](that: GenSeq[B]): Boolean
描述:判断当前序列是否以某个序列结尾
示例:

e = Array(1,2,3,1,2,5,6)
scala> e.endsWith(Array(2,5,6))
res177: Boolean = true

exists

定义:def exists(p: (T) ⇒ Boolean): Boolean
描述:判断当前数组是否包含符合条件的元素
示例:

e = Array(1,2,3,1,2,5,6)
scala> e.exists(x=>x == 3)
res178: Boolean = true

scala> e.exists(x=>x == 8)
res179: Boolean = false

filter

定义:def filter(p: (T) ⇒ Boolean): Array[T]
描述:取得当前数组中符合条件的元素,组成新的数组返回
示例:

res61: List[Int] = List(1, 2, 3, 3, 2, 1)
scala> res61.filter(_>2)
res63: List[Int] = List(3, 3)

filterNot

定义:def filterNot(p: (T) ⇒ Boolean): Array[T]
描述:取得当前数组中不符合条件的元素,组成新的数组返回
示例:

val a = Array(3, 2, 3,4)
val b = a.filterNot( {x:Int => x> 3} )
println(b.mkString(",")) //3,2,3

find

定义:def find(p: (T) ⇒ Boolean): Option[T]
描述:查找第一个符合条件的元素,返回 Option
示例:

b: Array[Int] = Array(1, 2, 3)
scala> b.find(_<1)
res52: Option[Int] = None

scala> b.find(_>2)
res71: Option[Int] = Some(3)

flatMap

定义:def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Array[B]
描述:先map后降维,对当前序列的每个元素进行操作,map之后的每个元素必须还是集合,结果放入新序列返回,参数要求是 GenTraversableOnce 及其子类
示例:

val d = Array(1, 2, 3, 4)
先map生成二维数组
scala> d.map(x=>1 to x)
res77: Array[scala.collection.immutable.Range.Inclusive] = Array(Range(1), Range(1, 2), Range(1, 2, 3), Range(1, 2, 3, 4))
再flat 扁平化
scala> d.flatMap(x=>1 to x)
res76: Array[Int] = Array(1, 1, 2, 1, 2, 3, 1, 2, 3, 4)

举例:

val rdd = sc.parallelize(Seq("123,345 567","234,567 567","456,"))
    rdd.flatMap(line=>{
      val ds = line.split(",",-1)   //-1表示逗号后面没有值也算一个空格
      ds(1).split(" ").map(a=>
        (ds(0),a))
    }).foreach(println) //先对数组进行map,然后对结果flatten
    结果:
(234,567)
(456,)
(123,345)
(123,567)
(234,567)

如果要对逗号后面是空值的过滤,并去重
val rdd = sc.parallelize(Seq("123,345 567","234,567 567","456,"))
    rdd.flatMap(line=>{
      val ds = line.split(",",-1)
      ds(1).split(" ").map(a=>
        (ds(0),a))//先对数组进行map,然后对结果flatten
    }).distinct().filter(_._2!="").foreach(println)
    sc.stop()
结果:
(123,567)
(234,567)
(123,345)

flatten

定义:def flatten[U](implicit asTrav: (T) ⇒ collection.Traversable[U], m: ClassTag[U]): Array[U]
描述:扁平化(降维),将二维数组的所有元素组合在一起,形成一个一维数组返回
示例:

scala> var a = Array(Array(1,2,3),Array(4,5,6))
a: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))

scala> a.flatten
res79: Array[Int] = Array(1, 2, 3, 4, 5, 6)

fold

定义:def fold[A1 >: A](z: A1)(op: (A1, A1) ⇒ A1): A1ClassTag[U]): Array[U]
描述:对序列中的每个元素进行二元运算,和 aggregate 有类似的语义,但执行过程有所不同
示例:

def seqno(m: Int, n: Int): Int = {
    val s = "seq_exp = %d + %d"
    println(s.format(m, n))
    m + n
  }

  def main(args: Array[String]) {
    val a = Array(1, 2, 3, 4)
    
    val b = a.fold(5)(seqno) // 不分区
    println("b = " + b)
    /**
     * seq_exp = 5 + 1
     * seq_exp = 6 + 2
     * seq_exp = 8 + 3
     * seq_exp = 11 + 4
     * b = 15
     */
      
    val c = a.par.fold(5)(seqno) // 分区
    println("c = " + c)
    /**
     * seq_exp = 5 + 3
     * seq_exp = 5 + 2
     * seq_exp = 5 + 4
     * seq_exp = 5 + 1
     * com_exp = 6 + 7
     * com_exp = 8 + 9
     * com_exp = 13 + 17
     * c = 30
     */

看上面的运算过程发现,fold中,seqno是把初始值顺序和每个元素相加,把得到的结果与下一个元素进行运算
而aggregate中,seqno是把初始值与每个元素相加,但结果不参与下一步运算,而是放到另一个序列中,由第二个方法combine进行处理。

foldLeft

定义:def foldLeft[B](z: B)(op: (B, T) ⇒ B): BClassTag[U]): Array[U]
描述:从左到右计算,简写方式:def /:[B](z: B)(op: (B, T) ⇒ B): B
示例:

def seqno(m: Int, n: Int): Int = {
    val s = "seq_exp = %d + %d"
    println(s.format(m, n))
    m + n
  }

  def main(args: Array[String]): Unit = {
    val a = Array(1, 2, 3, 4)

    val b = a.foldLeft(5)(seqno) // 简写: (5 /: a)(_ + _)
    println("b = " + b)
    /**
     * seq_exp = 5 + 1
     * seq_exp = 6 + 2
     * seq_exp = 8 + 3
     * seq_exp = 11 + 4
     * b = 15
     */

foldRight

定义:foldRight[B](z: B)(op: (B, T) ⇒ B): B
描述:从右到左计算,简写方式:def :[B](z: B)(op: (T, B) ⇒ B): B
示例:

def seqno(m: Int, n: Int): Int = {
    val s = "seq_exp = %d + %d"
    println(s.format(m, n))
    m + n
  }

  def main(args: Array[String]): Unit = {
    val a = Array(1, 2, 3, 4)

    val b = a.foldRight(5)(seqno) // 简写: (a :\ 5)(_ + _)
    println("b = " + b)
    /**
     * seq_exp = 4 + 5
     * seq_exp = 3 + 9
     * seq_exp = 2 + 12
     * seq_exp = 1 + 14
     * b = 15
     */
  	val c = a.par.foldRight(5)(seqno) // 分区
  	println("c = " + c)
   	/*
   	* seq_exp = 4 + 5
   	* seq_exp = 3 + 9
   	* seq_exp = 2 + 12
   	* seq_exp = 1 + 14
   	* c = 15
   	*/
	}

forall

定义:def forall(p: (T) ⇒ Boolean): Boolean
描述:检测序列中的元素是否都满足条件 p,如果序列为空,则返回 true
示例:

val a = Array(1, 2, 3, 4)
println(a.forall(x => x > 0)) // true
println(a.forall(x => x > 2)) // false

foreach

定义:def foreach(f: (A) ⇒ Unit): Unit
描述:遍历序列中的元素,进行 f 操作
示例:

val a = Array(1, 2, 3, 4)
a.foreach(x => println(x * 10))
/**
 * 10
 * 20
 * 30
 * 40

groupBy

定义:def groupBy[K](f: (T) ⇒ K): Map[K, Array[T]]
描述:按条件分组,条件由 f 匹配,返回值是 Map 类型,每个 key 对应一个数组
示例:把数组中的数分奇偶

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

scala> e.groupBy(x=>x match{
     | case x if (x%2==0)=> "ou"
     | case _=> "ji"
     | });
res3: scala.collection.immutable.Map[String,Array[Int]] = Map(ou -> Array(2), ji -> Array(1, 3))
把HasuMap里的值拿出来,两种:apply,get
scala> res3.apply("ji")
res4: Array[Int] = Array(1, 3)

scala> res3.get("ou")
res5: Option[Array[Int]] = Some([I@43255f91)
scala> res3.get("ou").get
res6: Array[Int] = Array(2)

scala> val words = Array("hello world","hello scala","spark scala")
words: Array[String] = Array(hello world, hello scala, spark scala)

scala> words.flatMap(_.split(" ")).groupBy(x=>x)
res3: scala.collection.immutable.Map[String,Array[String]] = Map(spark -> Array(spark), scala -> Array(scala, scala), world -> Array(world), hello -> Array(hello, hello))

scala> words.flatMap(_.split(" ")).groupBy(x => x).foreach(f=>println(f._1,f._2.length))
(spark,1)
(scala,2)
(world,1)
(hello,2)

genericBuilder

定义:def genericBuilder[B]: scala.collection.mutable.Builder[B,scala.collection.mutable.IndexedSeq[B]]
描述:
示例:
groupBy
定义:def groupBy[K](f: (T) ⇒ K): Map[K, Array[T]]
描述:按条件分组,条件由 f 匹配,返回值是Map类型,每个key对应一个序列。(条件最多23个)
示例:下面代码实现的是,把小于3的数字放到一组,大于3的放到一组,返回Map[String,Array[Int]]

val a = Array(1, 2, 3,4)
val b = a.groupBy( x => x match {
  case x if (x < 3) => "small"
  case _ => "big"
})
//结果:Map(small -> Array(1,2,3), big -> Array(4))

//取small的值
b.apply("small")	// Array(1,2,3)
或
b.get("small")	//Some(地址值)
b.get("small").get	// Array(1,2,3)

// 分组后,取分组内的值
b.apply("small").apply(0)	//结果:Int = 1

// 统计单词数量
val words = Array("hello world","hello scala","spark scala")
val word = words.flatMap(x => x.split(" "))  //val word = words.flatMap(_.split(" "))
word.groupBy(x=>x).foreach(f=>println(f._1,f._2.length))
/** 结果
(spark,1)
(scala,2)
(world,1)
(hello,2)
*/

grouped

定义:def grouped(size: Int): collection.Iterator[Array[T]]
描述:按指定数量分组,每组有 size 个元素,返回一个迭代器
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.grouped(2)
res7: Iterator[Array[Int]] = non-empty iterator
按两个一组分
grouped(2)里的2表示,每个数组里面最多2个
scala> e.grouped(2).foreach(x=> println(x.mkString(",")))
1,2
3

hasDefiniteSize

定义:def hasDefiniteSize: Boolean
描述:检测序列是否存在有限的长度,对应 Stream 这样的流数据则返回 false
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.hasDefiniteSize
res9: Boolean = true

scala> a
res174: Array[Int] = Array()
scala> a.hasDefiniteSize
res173: Boolean = true

head

定义:def head: T
描述:返回序列的第一个元素,如果序列为空,将引发错误
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.head
res10: Int = 1

var a=Array()
a: Array[Nothing] = Array()
a.head
<console>:13: error: value head is not a member of Array[Nothing]
       a.head

tail

定义:def tail: Array[T]
描述:返回当前序列中不包含第一个元素的序列,除了头都是尾
示例:

scala> a
res6: Array[Int] = Array(1, 2, 3, 4)
scala> a.tail
res8: Array[Int] = Array(2, 3, 4)
如果要取最后一个,用last

last

定义:def last: T
描述:返回序列的最后一个元素,如果序列为空,将引发错误
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.last
res41: Int = 3

headOption

定义:def headOption: Option[T]
描述:返回序列的第一个元素的 Option 类型对象,如果序列为空,则返回 None
示例:

scala> val a:Array[Int]=Array()
a: Array[Int] = Array()
scala> a.head报错
序列为空会报错
scala> a.headOption
res14: Option[Int] = None

lastOption

定义:def lastOption: Option[T]
描述:返回序列的最后一个元素的 Option 类型对象,如果序列为空,则返回 None
示例:

a: Array[Int] = Array()
scala> a.lastOption
res160: Option[Int] = None

indexOf(elem)

定义:def indexOf(elem: T): Int
描述:返回元素 elem 在序列中第一次出现的索引
示例:

scala> val e = Array(1,2,3)
e: Array[Int] = Array(1, 2, 3)
scala> e.indexOf(3)
res15: Int = 2

indexOf(elem, from)

定义:def indexOf(elem: T, from: Int): Int
描述:返回元素 elem 在序列中第一次出现的索引,指定从索引 from 开始查找
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.indexOf(3,3)
res16: Int = -1
数组中都没有3这个索引,所以找不到,
找不到返回-1,并且指定索引可以超出元素索引的范围
scala> e.indexOf(3,1)
res162: Int = 2

indexOfSlice(that)

定义:def indexOfSlice[B >: A](that: GenSeq[B]): Int
描述:检测当前序列中是否包含序列 that,并返回第一次出现该序列的索引,找不到返回-1;并且指定索引可以超出元素索引的范围
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.indexOfSlice(Array(2,3))
res17: Int = 1
就是说查看e里有没有Array(2,3)这个数组,出现的下标

indexOfSlice(that, from)

定义:def indexOfSlice[B >: A](that: GenSeq[B], from: Int): Int
描述:检测当前序列中是否包含另一个序列 that,指定从索引 from 开始查找,并返回第一次出现该序列的索引
示例:

scala>val a = Array(1, 2, 3, 2, 3, 4)
scala>val b = Array(2, 3)
scala>a.indexOfSlice(b, 2))
scala>Int =3

indexWhere§

定义:def indexWhere(p: (T) ⇒ Boolean): Int
描述:返回当前序列中第一个满足条件 p 的元素的索引
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.indexWhere(_>1)
res18: Int = 1

indexWhere(p, from)

定义:def indexWhere(p: (T) ⇒ Boolean, from: Int): Int
描述:返回当前序列中第一个满足条件 p 的元素的索引,指定从索引 from 开始查找
示例:

scala> var b = Array(1,2,3,4)
b: Array[Int] = Array(1, 2, 3, 4)

scala> b.indexWhere(_>2,3)
res185: Int = 3

indices

定义:def indices: collection.immutable.Range
描述:返回当前序列索引集合
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.indices
res19: scala.collection.immutable.Range = Range(0, 1, 2)

init

定义:def init: Array[T]
描述:返回当前序列中不包含最后一个元素的序列
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.init
res188: Array[Int] = Array(1, 2)

inits

定义:def inits: collection.Iterator[Array[T]]
描述:对集合中的元素进行 init 迭代操作,该操作的返回值中, 第一个值是当前序列的副本,最后一个值为空,每一步都进行 init 操作,上一步的结果作为下一步的操作对象
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.inits.foreach(x=>println(x.mkString(",")))
第一个值1,2,3
第二个值1,2
第三个值1
第四个值

intersect

定义:def intersect(that: collection.Seq[T]): Array[T]
描述:取两个集合的交集
示例:

scala> b
res190: Array[Int] = Array(1, 2, 3, 4)

scala> var c = Array(3,4,5)
c: Array[Int] = Array(3, 4, 5)

scala> b.intersect(c)
res193: Array[Int] = Array(3, 4)

isDefinedAt

定义:def isDefinedAt(idx: Int): Boolean
描述:判断序列中是否存在指定索引
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.isDefinedAt(3)
res21: Boolean = false

scala> e.isDefinedAt(2)
res22: Boolean = true

isEmpty

定义:def isEmpty: Boolean
描述:判断序列是否为空
示例:

scala> a
res194: Array[Int] = Array()

scala> a.isEmpty
res25: Boolean = true

isTraversableAgain

定义:def isTraversableAgain: Boolean
描述:判断序列是否可以反复遍历,该方法是 GenTraversableOnce 中的方法,对于 Traversables 一般返回 true,对于 Iterators 返回 false,除非被复写 或者说判断是否为迭代器(只能遍历一次)
示例:

scala> e.inits.isTraversableAgain
res27: Boolean = false

scala> e.iterator
res30: Iterator[Int] = non-empty iterator

scala> e.isTraversableAgain
res30: Boolean = true

iterator

定义:def iterator: collection.Iterator[T]
描述:生成当前序列的迭代器
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.iterator
res30: Iterator[Int] = non-empty iterator
把一个数组变成迭代器
scala> res37
res37: scala.collection.immutable.Map[String,Array[Int]] = Map(ou -> Array(2), ji -> Array(1, 3))

scala> res3.iterator
res38: Iterator[(String, Array[Int])] = non-empty iterator

怎么把值取出来呢(可以用mkString/addString)
scala> res3.iterator.foreach(x=>println(x._1,x._2))//x是map里的数组
(ou,[I@43255f91)
(ji,[I@65e2b654)

scala> res3.iterator.foreach(x=>println(x._1,x._2.mkString(",")))
(ou,2)
(ji,1,3)

last

取得序列中最后一个元素

e: Array[Int] = Array(1, 2, 3)
scala> e.last
res41: Int = 3

lastIndexOf(elem)

定义:def lastIndexOf(elem: T): Int
描述:返回元素 elem 在序列中最后一次出现的索引
示例:

scala> a
res47: Array[Int] = Array(1, 3, 2, 3, 4)
scala> a.lastIndexOf(3)
res199: Int = 3

lastIndexOf(elem, end)

定义:def lastIndexOf(elem: T, end: Int): Int
描述:返回元素 elem 在序列中最后一次出现的索引,指定在索引 end 之前(包括)的元素中查找
示例:

scala> a
res47: Array[Int] = Array(1, 3, 2, 3, 4)、

scala> a.lastIndexOf(3,3)//表示3最后一次出现的索引,范围是在角标3(包括3 )之前的元素中
res201: Int = 3

scala> a.lastIndexOf(3,2)//表示3最后一次出现的索引,范围是在角标2(包括2 )之前的元素中
res202: Int = 1

lastIndexOfSlice(that)

定义:def lastIndexOfSlice[B >: A](that: GenSeq[B]): Int
描述:检测当前序列中是否包含序列 that,并返回最后一次出现该序列的索引
示例:

scala> var f = Array(1,2,3,2,3,4)
f: Array[Int] = Array(1, 2, 3, 2, 3, 4)

scala> f.lastIndexOfSlice(Array(2,3))
res203: Int = 3

lastIndexOfSlice(that, end)

定义:def lastIndexOfSlice[B >: A](that: GenSeq[B], end: Int): Int
描述:检测当前序列中是否包含序列 that,并返回最后一次出现该序列的索引,指定在索引 end 之前(包括)的元素中查找
示例:

scala> var f = Array(1,2,3,2,3,4)
f: Array[Int] = Array(1, 2, 3, 2, 3, 4)

scala> f.lastIndexOfSlice(Array(2,3),2)
res204: Int = 1

lastIndexWhere§

定义:def lastIndexWhere(p: (T) ⇒ Boolean): Int
描述:返回当前序列中最后一个满足条件 p 的元素的索引
示例:

scala> var b = Array(1,2,3,4)
b: Array[Int] = Array(1, 2, 3, 4)
scala> b.lastIndexWhere(_<4)
res20: Int = 2

lastIndexWhere(p, end)

定义:def lastIndexWhere(p: (T) ⇒ Boolean, end: Int): Int
描述:返回当前序列中最后一个满足条件 p 的元素的索引,指定在索引 end 之前(包括)的元素中查找
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.lastIndexWhere(_<3,3)
res23: Int = 1

length

定义:def length: Int
描述:返回序列元素个数
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.length
res24: Int = 4

lengthCompare

定义:def lengthCompare(len: Int): Int
描述:比较序列的长度和参数 len,返回序列的长度 - len,根据二者的关系返回不同的值,比较规则是
x>0, length > len
x=0, length = len
x<0, length < len
示例:

scala> e
res47: Array[Int] = Array(1, 2, 3)

scala> e.lengthCompare(3)
res48: Int = 0

scala> e.lengthCompare(2)
res49: Int = 1

scala> e.lengthCompare(4)
res50: Int = -1

scala> e.lengthCompare(5)
res51: Int = -2

lift

定义:def lift: A1 => Option[B1]
描述:按给定索引取值,返回Optition类型的值。
示例:

val a = Array(1,2,3)
val b = a.lift(1)		//b: Option[Int] = Some(2)

val a = Array(1, 2, 3, "a")	//a: Array[Any] = Array(1, 2, 3, a)
val b = a.lift(3)		//b: Option[Any] = Some(a)
val b = a.lift(5)		//b: Option[Any] = None

map

定义:def map[B](f: (A) ⇒ B): Array[B]
描述:对序列中的元素进行 f 操作,返回生成的新序列
示例:

scala> b
res47: Array[Int] = Array(1, 2, 3, 4)
scala> b.map(_+1)
res28: Array[Int] = Array(2, 3, 4, 5)

max

定义:def max: A
描述:返回序列中最大的元素
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.max
res30: Int = 4

min

定义:def max: A
描述:返回序列中最小的元素
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.min
res32: Int = 1

maxBy

定义:def maxBy[B](f: (A) ⇒ B): A
描述:返回序列中符合条件的第一个元素
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.maxBy(_>2)
res33: Int = 3

minBy

定义:def minBy[B](f: (A) ⇒ B): A
描述:返回序列中不符合条件的第一个元素
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.minBy(_<2)
res34: Int = 2

mkString

定义:def mkString: String
描述:将序列中所有元素拼接成一个字符串
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.mkString
res35: String = 1234

mkString(sep)

定义:def mkString: String
描述:将序列中所有元素拼接成一个字符串,以 sep 作为元素间的分隔符
示例:

scala> b.mkString(",")
res36: String = 1,2,3,4

mkString(start, sep, end)

定义:def mkString(start: String, sep: String, end: String): String
描述:将序列中所有元素拼接成一个字符串,以 start 开头,以 sep 作为元素间的分隔符,以 end 结尾
示例:

scala> b.mkString("{",",","}")
res37: String = {1,2,3,4}

nonEmpty

定义:def nonEmpty: Boolean
描述:判断序列是否不为空
示例:

scala> b
res54: Array[Int] = Array(1, 2, 3, 4)
scala> b.nonEmpty
res38: Boolean = true

scala> val c = new Array[Int](0)
c: Array[Int] = Array()
scala> c.nonEmpty
res53: Boolean = false

orElse

定义:def orElse[A1 <: Int, B1 >: Any](that: PartialFunction[A1,B1]): PartialFunction[A1,B1]
描述:参数:Some;返回:如果Option不为None返回Some否则返回参数的Some
示例:

padTo

定义:def padTo(len: Int, elem: A): Array[A]
描述:填充序列,如果当前序列长度小于 len,那么新产生的序列长度是 len,多出的几个位值填充 elem,如果当前序列大于等于 len ,则返回当前序列
示例:填充一个长度为 6 的序列,不足位补 8

scala> b
res59: Array[Int] = Array(1, 2, 3, 4)
scala> b.padTo(6,8)
res60: Array[Int] = Array(1, 2, 3, 4, 8, 8)

par

定义:def par: ParArray[T]
描述:返回一个并行实现,产生的并行序列不能被修改,其作用相当于hashcode进行分区
示例:

scala> b
res59: Array[Int] = Array(1, 2, 3, 4)
scala> b.par
res62: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3, 4)
scala> a.par.foreach(print)
4132							//多线程执行,所以无序的

partition

定义:def partition(p: (T) ⇒ Boolean): (Array[T], Array[T])
描述:按条件将序列拆分成两个数组,满足条件的放到第一个数组,其余的放到第二个数组,返回的是包含这两个数组的元组
示例:

b:Array[Int] = Array(1, 2, 3, 4)
scala> b.partition(_>2)
res64: (Array[Int], Array[Int]) = (Array(3, 4),Array(1, 2))

val a = Array(1, 2, 3, 4)
val b: (Array[Int], Array[Int]) = a.partition(x => x % 2 == 0)
println("偶数: " + b._1.mkString(",")) // 偶数: 2,4
println("奇数: " + b._2.mkString(",")) // 奇数: 1,3

patch

定义:def patch(from: Int, that: GenSeq[A], replaced: Int): Array[A]
描述:批量替换,从原序列的 from 处开始,后面的 replaced 个元素,将被替换成序列 that
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.patch(1,Array(4,5,6),1)
res57: Array[Int] = Array(1, 4, 5, 6, 3)

permutations

定义:def permutations: collection.Iterator[Array[T]]
描述:permutations 表示排列,这个排列组合会选出所有排列顺序不同的字符组合,permutations 与 combinations 不同的是,相同的组合考虑排列,对于 “abc”、“cba”,视为不同的组合
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.permutations.toList
res60: List[Array[Int]] = List(Array(1, 2, 3), Array(1, 3, 2), Array(2, 1, 3), Array(2, 3, 1), Array(3, 1, 2), Array(3, 2, 1))

scala> e.permutations.toArray
res61: Array[Array[Int]] = Array(Array(1, 2, 3), Array(1, 3, 2), Array(2, 1, 3), Array(2, 3, 1), Array(3, 1, 2), Array(3, 2, 1))

scala> e.permutations.foreach(x=>println(x.mkString(",")))
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1

prefixLength

定义:def prefixLength(p: (T) ⇒ Boolean): Int
描述:由第一个索引开始,返回序列中满足条件p的元素数量,直到有不满足的则结束
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.prefixLength(_<3)
res65: Int = 2

product

定义:def product: A
描述:返回所有元素乘积的值
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.product
res69: Int = 6

reduce

定义:def reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1
描述:同 fold,对序列中的每个元素进行二元运算,不需要初始值
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.reduce(_+_)
res70: Int = 6

reduceLeft

定义:def reduceLeft[B >: A](op: (B, T) ⇒ B): B
描述:同 foldLeft,从左向右计算,不需要初始值
示例:

val a = Array(1, 2, 3, 4)
val b = a.reduceLeft(seqno)
println("b = " + b)
/**
 * seq_exp = 1 + 2
 * seq_exp = 3 + 3
 * seq_exp = 6 + 4
 * b = 10
 */

reduceRight

定义:def reduceRight[B >: A](op: (B, T) ⇒ B): B
描述:同 foldRight,从右向左计算,不需要初始值
示例:

val a = Array(1, 2, 3, 4)
val b = a.reduceRight(seqno)
println("b = " + b)

/**
 * seq_exp = 3 + 4
 * seq_exp = 2 + 7
 * seq_exp = 1 + 9
 * b = 10
 */

reduceOption

定义:def reduceOptionA1>:A: scala.Option[A1]
描述: 同 reduceLeftOption,计算Option,返回 Option
示例:

def seqno(m: Int, n: Int): Int = {
val s = "seq_exp = %d + %d"
println(s.format(m, n))
m + n
}

def main(args: Array[String]) {
	val a = Array(1, 2, 3, 4)
	val b = a.reduceOption(seqno)
	println("b = " + b)	
} 
/*
 * seq_exp = 1 + 2
 * seq_exp = 3 + 3
 * seq_exp = 6 + 4
 * b = Some(10)
 */

reduceLeftOption

定义:def reduceLeftOption[B >: A](op: (B, T) ⇒ B): Option[B]
描述:同 reduceLeft,返回 Option
示例:

val a = Array(1, 2, 3, 4)
val b = a.reduceLeft(seqno)
println("b = " + b)
/**
 * seq_exp = 1 + 2
 * seq_exp = 3 + 3
 * seq_exp = 6 + 4
 * b = Some(10)
 */

reduceRightOption

定义:def reduceRightOption[B >: A](op: (T, B) ⇒ B): Option[B]
描述:同 reduceRight,返回 Option
示例:

val a = Array(1, 2, 3, 4)
val b = a.reduceRight(seqno)
println("b = " + b)
/**
 * seq_exp = 3 + 4
 * seq_exp = 2 + 7
 * seq_exp = 1 + 9
 * b = Some(10)
 */

repr

定义:def repr : Repr
描述:repr() 函数将对象转化为供解释器读取的形式
示例:

var a = Map(1 -> "a",2 -> "b")
println(a.repr)

Map(1 -> a, 2 -> b)
 val a = Map(1 -> "a",2 -> "b")
println(a.repr) //等于 println(a)  Map(1 -> a, 2 -> b)

val b = Array("java","scala")
println(b.repr) // 等于 println(b)  打印地址[Ljava.lang.String;@71423665

reverse

定义:def reverse: Array[T]
描述:反转序列
示例:

val a = Array(1,2,3,4,5)
val b = a.reverse
println(b.mkString(","))    //5,4,3,2,1

reverseIterator

定义:def reverseIterator: collection.Iterator[T]
描述:生成反向迭代器
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.reverseIterator.foreach(println)
3
2
1

reverseMap

定义:def reverseMap[B](f: (A) ⇒ B): Array[B]
描述:同 map,方向相反
示例:

val a=Array(1,2,3)
a.reverseMap(x=>x+2)
//Array[Int] = Array(5, 4, 3)

runWith

定义:def runWith[U](action : scala.Function1[B, U]) : scala.Function1[A, scala.Boolean] = { /* compiled code */ }
描述: 执行偏函数,当参数不在定义域内时,返回false,否则返回true,并执行action
示例:

val pf: PartialFunction[Int, Int] = { 
case m: Int if m % 2 == 1=> m * 2 }
pf.runWith(println)(3)	输出6	// return: Boolean = true
pf.runWith(println)(2)			// return: Boolean = false

sameElements

定义:def sameElements(that: GenIterable[A]): Boolean
描述:判断两个序列是否顺序和对应位置上的元素都一样
示例:

val e=Array(1,2,3)
scala> e.sameElements(Array(1,2,3))
res100: Boolean = true

scala> e.sameElements(Array(1,2))
res101: Boolean = false

scala> e.sameElements(Array(1,2,4))
res102: Boolean = false

scan

定义:def scan[B >: A, That](z: B)(op: (B, B) ⇒ B)(implicit cbf: CanBuildFrom[Array[T], B, That]): That
描述:同 fold,scan 会把每一步的计算结果放到一个新的集合中返回,而 fold 返回的是最后的结果
示例:

val a = Array(1, 2, 3, 4)
val b = a.scanLeft(5)(_ + _)
println(b.mkString(",")) // 5,6,8,11,15

scanLeft

定义:def scanLeft[B, That](z: B)(op: (B, T) ⇒ B)(implicit bf: CanBuildFrom[Array[T], B, That]): That
描述:同 foldLeft,从左向右计算,每一步的计算结果放到一个新的集合中返回
示例:

val a = Array(1, 2, 3, 4)
val b = a.scanLeft(5)(_ + _)
println(b.mkString(",")) // 5,6,8,11,15

scanRight

定义:def scanRight[B, That](z: B)(op: (T, B) ⇒ B)(implicit bf: CanBuildFrom[Array[T], B, That]): That
描述:同 foldRight,从右向左计算,每一步的计算结果放到(从右向左放)一个新的集合中返回
示例:

val a = Array(1, 2, 3, 4)
val b = a.scanRight(5)(_ + _)
println(b.mkString(",")) // 15,14,12,9,5

segmentLength

定义:def segmentLength(p: (T) ⇒ Boolean, from: Int): Int
描述:从序列的 from 开始向后查找,返回满足条件 p 的连续元素的长度,只返回第一个
示例:

res109: Array[Int] = Array(3, 2, 1, 1, 1, 1, 5, 6, 1, 2, 3)
scala> res109.segmentLength(_<3,2)
res110: Int = 4

seq

定义:def seq: collection.mutable.IndexedSeq[T]
描述:产生一个引用当前序列的 sequential 视图,就是一个特殊的集合
示例:

e: Array[Int] = Array(1, 2, 3)
scala> e.seq
res112: scala.collection.mutable.IndexedSeq[Int] = WrappedArray(1, 2, 3)

size

定义:def size: Int
描述:返回序列元素个数,同 length
示例:

scala> b
res82: Array[Int] = Array(1, 2, 3, 4)
scala> b.size
res83: Int = 4

slice

定义:def slice(from: Int, until: Int): Array[T]
描述:返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.slice(1,3)
res86: Array[Int] = Array(2, 3)

sliding(size)

定义:def sliding(size: Int): collection.Iterator[Array[T]]
描述:滑动,从第一个元素开始,每个元素和它后面的 size个元素组成一个数组,最终组成一个新的集合返回,当剩余元素个数不够size时,则结束
示例:

scala> b
res88: Array[Int] = Array(1, 2, 3, 4)
scala> b.sliding(2).toList
res92: List[Array[Int]] = List(Array(1, 2), Array(2, 3), Array(3, 4))

sliding(size, step)

定义:def sliding(size: Int): collection.Iterator[Array[T]]
描述:从第一个元素开始,每个元素和它后面的 size个元素组成一个数组,最终组成一个新的集合返回,当剩余元素个数不够 size 时,则结束。该方法可以设置步长 step,每一组元素组合完后,下一组从上一组起始元素位置 + step 后的位置处开始
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.sliding(2,2).toList
res93: List[Array[Int]] = List(Array(1, 2), Array(3, 4))

sortBy

定义:def sortBy[B](f: (T) ⇒ B)(implicit ord: math.Ordering[B]): Array[T]
描述:按指定的排序规则对序列排序
示例:

scala> res109
res125: Array[Int] = Array(3, 2, 1, 1, 1, 1, 5, 6, 1, 2, 3)

scala> res109.sortBy(x=>x)
res126: Array[Int] = Array(1, 1, 1, 1, 1, 2, 2, 3, 3, 5, 6)

scala> res109.sortBy(x => -x)
res128: Array[Int] = Array(6, 5, 3, 3, 2, 2, 1, 1, 1, 1, 1)

sorted

定义:def sorted[B >: A](implicit ord: math.Ordering[B]): Array[T]]
描述:使用默认的排序规则对序列排序,默认升序排列,无参数
示例:

scala> a
res95: Array[Int] = Array(2, 3, 5, 2, 5, 6, 2)

scala> a.sorted
res96: Array[Int] = Array(2, 2, 2, 3, 5, 5, 6)

sortWith

定义:def sortWith(t:(T,T)=>boolean):Array[T]
描述:自定义排序方法
示例:

f: Array[Int] = Array(3, 2, 1, 1, 1, 1, 5, 6, 1, 2, 3)
scala> f.sortWith(_.compareTo(_)>0)//倒序
res131: Array[Int] = Array(6, 5, 3, 3, 2, 2, 1, 1, 1, 1, 1)

scala> f.sortWith(_.compareTo(_)<0)
res132: Array[Int] = Array(1, 1, 1, 1, 1, 2, 2, 3, 3, 5, 6)

span

定义:def span(p: (T) ⇒ Boolean): (Array[T], Array[T])
描述:将序列拆分成两个数组,从第一个元素开始,直到第一个不满足条件的元素为止,其中的元素放到第一个数组,其余的放到第二个数组,返回的是包含这两个数组的元组
示例:

scala> a
res97: Array[Int] = Array(2, 3, 5, 2, 5, 6, 2)
scala> a.span(_<5)
res99: (Array[Int], Array[Int]) = (Array(2, 3),Array(5, 2, 5, 6, 2))

splitAt

定义:def splitAt(n: Int): (Array[T], Array[T])
描述:从指定位置开始,把序列拆分成两个数组,指定索引位置元素放入第二个数组中
示例:

scala> a
res100: Array[Int] = Array(2, 3, 5, 2, 5, 6, 2)
scala> a.splitAt(4)
res102: (Array[Int], Array[Int]) = (Array(2, 3, 5, 2),Array(5, 6, 2))

startsWith(that)

定义:def startsWith[B](that: GenSeq[B]): Boolean
描述:判断序列是否以某个序列开始
示例:

a: Array[Int] = Array(2, 3, 5, 2, 5, 6, 2)
scala> a.startsWith(Array(2,3))
res105: Boolean = true

startsWith(that, offset)

定义:def startsWith[B](that: GenSeq[B], offset: Int): Boolean
描述:判断序列从指定偏移处是否以某个序列开始
示例:

a: Array[Int] = Array(2, 3, 5, 2, 5, 6, 2)
d: Array[Int] = Array(2, 5)

scala> a.startsWith(d,3)
res111: Boolean = true

stringPrefix

定义:def stringPrefix: String
描述:返回 toString 结果的前缀
示例:

scala> a
res12: Array[Int] = Array(1, 2, 3, 4)

scala> a.stringPrefix
res13: String = [I[I

subSequence

定义:def subSequence(start: Int, end: Int): CharSequence
描述:返回 start 和 end 间的字符序列,不包含 end 处的元素
(返回 start 和 end 间的字符序列 只针对char类型(前包后不包))
示例:

scala> var c = Array('a','b','c')
c: Array[Char] = Array(a, b, c)

scala> c.subSequence(1,2)
res121: CharSequence = b

sum

定义:def sum: A
描述:序列求和,元素需为 Numeric[T] 类型
示例:

scala> b
res123: Array[Int] = Array(1, 2, 3, 4)

scala> b.sum
res124: Int = 10

tail

定义:def tail: Array[T]
描述:返回当前序列中不包含第一个元素的序列
示例:除了头都是尾

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.tail
res125: Array[Int] = Array(2, 3, 4)

tails

定义:def tails: collection.Iterator[Array[T]]
描述:同 inits,每一步都进行 tail 操作
示例:

var b= Array(1, 2, 3, 4)
b.tails.toArray
res22: Array[Array[Int]] = Array(Array(1, 2, 3, 4), Array(2, 3, 4), Array(3, 4), Array(4), Array())

take

定义:def take(n: Int): Array[T]
描述:返回当前序列中,前 n 个元素组成的序列
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.take(3)
res131: Array[Int] = Array(1, 2, 3)

takeRight

定义:def takeRight(n: Int): Array[T]
描述:返回当前序列中,从右边开始,后 n 个元素组成的序列
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.takeRight(3)
res132: Array[Int] = Array(2, 3, 4)

takeWhile

定义:def takeWhile(p: (T) ⇒ Boolean): Array[T]
描述:返回当前序列中,从第一个元素开始,满足条件的连续元素组成的序列
示例:

b: Array[Int] = Array(1, 2, 3, 4)
scala> b.takeWhile(_<3)
res133: Array[Int] = Array(1, 2)

val a = Array(1, 2, 3, 4, 1, 2)
val b = a.takeWhile(x => x < 3)
print(b.mkString(",")) // 1,2

toArray

定义:def toArray: Array[A]
描述:将序列转换成 Array 类型

toBuffer

定义:def toBuffer[A1 >: A]: Buffer[A1]
描述:将序列转换成 Buffer 类型

toIndexedSeq

定义:def toIndexedSeq: collection.immutable.IndexedSeq[T]
描述:将序列转换成 IndexedSeq 类型

toIterable

定义:def toIterable: collection.Iterable[T]
描述:将序列转换成可迭代的类型

toIterator

定义:def toIterator: collection.Iterator[T]
描述:将序列转换成迭代器,同 iterator 方法

toList

定义:def toList: List[T]
描述:将序列转换成 List 类型
示例:

scala> val e = Array(1,2,3,4,5)
e: Array[Int] = Array(1, 2, 3, 4, 5)

scala> e.toList
res1: List[Int] = List(1, 2, 3, 4, 5)

(存储数据和ArrayList一样,但这种List转出来就不能变大了,像长度固定的ArrayList)
转出来长度不固定的,有,就是:toBuffer(真正意义上的ArrayList)

toBuffer

定义:def toBuffer[A1 >: A]: Buffer[A1]
描述:将序列转换成 Buffer 类型
示例:

e: Array[Int] = Array(1, 2, 3, 4, 5)
scala> e.toBuffer
res2: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)

toMap

定义:def toMap[T, U]: Map[T, U]
描述:将序列转转换成 Map 类型,需要被转化序列中包含的元素是 Tuple2 类型,Tuple2 类型意思是:元组必须是2元的,以元组的第一个元素做键,第二个元素做值
示例:

scala> var a = Array((1,2),(3,4),(5,8))
a: Array[(Int, Int)] = Array((1,2), (3,4), (5,8))

scala> a.toMap
res5: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4, 5 -> 8) 

补充:
现有e: Array[Int] = Array(1, 2, 3, 4, 5),如何变成一个元组;5个数字,如果用切割的话,会有一个剩下来,那就每一个都生成元组,(每个都补个值)

scala> e.map((_,1))
res3: Array[(Int, Int)] = Array((1,1), (2,1), (3,1), (4,1), (5,1))

scala> e.map((_,1)).toMap
res4: scala.collection.immutable.Map[Int,Int] = Map(5 -> 1, 1 -> 1, 2 -> 1, 3 -> 1, 4 -> 1)

toSeq

定义:def toSeq: collection.Seq[T]
描述:将序列转换成Seq类型

toSet

定义:def toSet[B >: A]: Set[B]
描述:将序列转换成 Set 类型

toStream

定义:def toStream: collection.immutable.Stream[T]
描述:将序列转换成 Stream 类型(流类型)
举例:

e: Array[Int] = Array(1, 2, 3, 4, 5)
scala> e.toStream
res5: scala.collection.immutable.Stream[Int] = Stream(1, ?)

toVector

定义:def toVector: Vector[T]
描述:将序列转换成 Vector 类型,内部线程安全,但效率低

transform

定义:def transform(f: Int => Int): scala.collection.mutable.WrappedArray[Int]
描述:将序列中的元素转换成指定的元素(同类型)
举例:

val a = Array(1,2,3,4,5)
val b = a.transform(x => 6)	// b: scala.collection.mutable.WrappedArray[Int] = WrappedArray(6, 6, 6, 6, 6)
val c = Array(1,3,"a",Array(1,2))
val d = c.transform(x => 8)	//d: scala.collection.mutable.WrappedArray[Any] = WrappedArray(8, 8, 8, 8)

transpose

定义:def transpose[U](implicit asArray: (T) ⇒ Array[U]): Array[Array[U]]
描述:矩阵转置,二维数组行列转换
举例:

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

scala> k.transpose
res6: Array[Array[Int]] = Array(Array(1, 3, 5), Array(2, 4, 6))

如果数组内元素多一个呢?
scala> val g = Array(Array(1, 3, 5,4),Array(3, 4, 5),Array(6, 7, 8))
g: Array[Array[Int]] = Array(Array(1, 3, 5, 4), Array(3, 4, 5), Array(6, 7, 8))

scala> g.transpose
res7: Array[Array[Int]] = Array(Array(1, 3, 6), Array(3, 4, 7), Array(5, 5, 8), Array(4))   剩下的会单独生成一个

union

定义:def union(that: collection.Seq[T]): Array[T]
描述:合并两个序列,同操作符 ++
示例:

scala> val e = Array(1,2,3,4,5)
e: Array[Int] = Array(1, 2, 3, 4, 5)

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

scala> e.union(d)
res8: Array[Int] = Array(1, 2, 3, 4, 5, 2, 3, 4)

unzip

定义:def unzip[T1, T2](implicit asPair: (T) ⇒ (T1, T2), ct1: ClassTag[T1], ct2: ClassTag[T2]): (Array[T1], Array[T2])
描述:将含有两个二元组的数组,每个元组的第一个元素组成一个数组,第二个元素组成一个数组,返回包含这两个数组的元组
举例:

scala> val k = Array((1,2),(4,5),(6,7))
k: Array[(Int, Int)] = Array((1,2), (4,5), (6,7))
scala> k.unzip
res13: (Array[Int], Array[Int]) = (Array(1, 4, 6),Array(2, 5, 7))

unzip3

定义:def unzip3[T1, T2, T3](implicit asTriple: (T) ⇒ (T1, T2, T3), ct1: ClassTag[T1], ct2: ClassTag[T2], ct3: ClassTag[T3]): (Array[T1], Array[T2], Array[T3])
描述:将含有三个三元组的数组,每个元组的第一个元素组成一个数组,第二个元素组成一个数组,第三个元素组成一个数组,返回包含这三个数组的元组
示例:

scala> val k = Array((1,2,3),(4,5,6),(6,7,8))
k: Array[(Int, Int, Int)] = Array((1,2,3), (4,5,6), (6,7,8))
scala> k.unzip3
res18: (Array[Int], Array[Int], Array[Int]) = (Array(1, 4, 6),Array(2, 5, 7),Array(3, 6, 8))

update

定义:def update(i: Int, x: T): Unit
描述:将序列中 i 索引处的元素更新为 x
示例:

scala> e
res20: Array[Int] = Array(1, 2, 3, 4, 5)
e.update(1, 7)
scala> e
res2: Array[Int] = Array(1, 7, 3, 4, 5)

updated

定义:def updated(index: Int, elem: A): Array[A]
描述:将序列中 i 索引处的元素更新为 x,并返回替换后的数组
示例:

scala> e.updated(1,7)
res0: Array[Int] = Array(1, 7, 3, 4, 5)

view

定义:def view(from: Int, until: Int): IndexedSeqView[T, Array[T]]
描述:返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素
示例:

e: Array[Int] = Array(1, 7, 3, 4, 5)
scala> e.view(0,4).foreach(x=>print(x))
1734
scala> e.view(0,4).toArray
res2: Array[Int] = Array(1, 7, 3, 4)

withFilter

定义:def withFilter(p: (T) ⇒ Boolean): FilterMonadic[T, Array[T]]
描述:根据条件 p 过滤元素,不生成新的集合
示例:

e: Array[Int] = Array(1, 7, 3, 4, 5)
scala> e.withFilter(_>2).map(x=>x)
res6: Array[Int] = Array(7, 3, 4, 5)

zip

定义:def zip[B](that: GenIterable[B]): Array[(A, B)]
描述:将两个序列对应位置上的元素组成一个元组数组,要求两个序列长度相同
示例:

scala> e
res28: Array[Int] = Array(1, 2, 100, 4, 5)
scala> val b = Array(2,3,4,5)
b: Array[Int] = Array(2, 3, 4, 5)

scala> e.zip(b)
res29: Array[(Int, Int)] = Array((1,2), (2,3), (100,4), (4,5))

zipAll

定义:def zipAll[B](that: collection.Iterable[B], thisElem: A, thatElem: B): Array[(A, B)]
描述:同 zip ,但是允许两个序列长度不同,不足的自动填充,如果当前序列短,不足的元素填充为 thisElem,如果 that 序列短,填充为 thatElem
示例:

scala> var a = Array(2,3,5,2,5,6,2)
a: Array[Int] = Array(2, 3, 5, 2, 5, 6, 2)
scala> e
res11: Array[Int] = Array(1, 7, 3, 4, 5)

scala> a.zipAll(e,1,2)
res14: Array[(Int, Int)] = Array((2,1), (3,7), (5,3), (2,4), (5,5), (6,2), (2,2))

zipWithIndex

定义:def zipWithIndex: Array[(A, Int)]
描述:序列中的每个元素和它的索引组成一个元组数组
示例:

scala> a
res15: Array[Int] = Array(2, 3, 5, 2, 5, 6, 2)
scala> a.zipWithIndex
res16: Array[(Int, Int)] = Array((2,0), (3,1), (5,2), (2,3), (5,4), (6,5), (2,6))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值