scala(数组、序列)方法大全

Scala 专栏收录该内容
1 篇文章 0 订阅

Scala超详细方法的使用

Scala中所有的方法

1、addString

  • def addString(b: StringBuilder): StringBuilder
    -【将数组中元素逐个添加到可变字符串序列b中】
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)

scala> val b = new StringBuilder()
b: StringBuilder =

scala> val c = a.addString(b)
c: StringBuilder = 12345

scala> c
res10: StringBuilder = 12345

scala> b
res11: StringBuilder = 12345
  • def addString(b: StringBuilder,sep: String): StringBuilder
    -【同上,并且每个元素以逗号分隔】
scala> c.clear

scala> val c = a.addString(b,",")
c: StringBuilder = 1,2,3,4,5
  • def addString(b: StringBuilder,start: String,sep: String,end: String): StringBuilder
    -【同上,并且在b中的开头和末尾分别添加{}】
scala> c.clear

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

scala> c
res14: StringBuilder = {1,2,3,4,5}

2、aggregate

  • def aggregate[B](z: => B)(seqop: (B, Int) => B,combop: (B, B) => B): B
    -【聚合计算,aggregate是柯里化方法,参数是两个方法,为了方便理解,我们把aggregate的两个参数,分别封装成两个方法,并把计算过程打印出来】
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)

scala> def cha(a:Int,b:Int)={
     | println("cha:"+a+" "+b)
     | a-b
     | }
cha: (a: Int, b: Int)Int

scala> def he(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he: (a: Int, b: Int)Int

scala> c
res16: StringBuilder = {1,2,3,4,5}

scala> val c = a.par.aggregate(5)(cha,he)
cha:5 1
cha:5 3
cha:5 2
cha:5 5
cha:5 4
he:1 0
he:4 3
he:2 1
he:7 3
c: Int = 10

scala> val c = a.par.aggregate(5)(_-_,_+_)
c: Int = 0

scala> val c = a.par.aggregate(5)(_+_,_+_)
c: Int = 40

3、apply

  • def apply(i: Int): Int
    -【获取指定索引处的元素】
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)

scala> val c = a.apply(0)
c: Int = 1

4、charAt

  • def charAt(index: Int): Char
    -【获取指定索引处的元素(只有Char类型元素才行)】
scala> val chars = Array('a','b','c')
chars: Array[Char] = Array(a, b, c)

scala> chars.charAt(2)
res2: Char = c

5、array

  • def array: Array[Int]
    -【创建一个相同的数组】
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)

scala> a.array
res25: Array[Int] = Array(1, 2, 3, 4, 5)

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

6、canEqual

  • override def canEqual(that: Any): Boolean
    -【判断两个对象是否可以进行比较】
scala> a
res29: Array[Int] = Array(1, 2, 3, 4, 5)

scala> b
res30: StringBuilder = {1,2,3,4,5}

scala> a.canEqual(b)
res31: Boolean = true

7、clone

  • override def clone(): Array[Char]
    -【创建一个副本】
scala> val chars = Array('a','b','c')
chars: Array[Char] = Array(a, b, c)

scala> val k = chars.clone
k: Array[Char] = Array(a, b, c)

scala> k
res6: Array[Char] = Array(a, b, c)

8、collect

  • def collect[B](pf: PartialFunction[A, B]): Array[B]
    -【通过执行一个并行计算(偏函数fun),把小写a转换为A,从而得到一个新的数组对象】
scala> val chars = Array('a','b','c')
chars: Array[Char] = Array(a, b, c)

scala> val fun:PartialFunction[Char,Char]={
     | case 'a'=>'A'
     | case x=>x
     | }
fun: PartialFunction[Char,Char] = <function1>

scala> chars.collect(fun)
res7: Array[Char] = Array(A, b, c)

9、collectFirst

  • def collectFirst[B](pf: PartialFunction[T, B]): Option[B]
    -【在序列中查找第一个符合偏函数定义的元素,并执行偏函数计算】
scala> val arr = Array(1,'a','b')
arr: Array[Int] = Array(1, 97, 98)

scala> val fun:PartialFunction[Any,Int]={
     | case x:Int=>x+10
     | }
fun: PartialFunction[Any,Int] = <function1>

scala> arr
res8: Array[Int] = Array(1, 97, 98)

scala> arr.collectFirst(fun)
res10: Option[Int] = Some(11)

scala> println(res10)
Some(11)

10、combinations

  • def combinations(n: Int): Iterator[Array[String]]
    -【根据n(几个字符为一组),进行排列组合】
scala> val arr = Array("a","b","c","d","e")
arr: Array[String] = Array(a, b, c, d, e)

scala> arr.combinations(2).foreach(x=>{x.foreach(print);println})
ab
ac
ad
ae
bc
bd
be
cd
ce
de

scala> arr.combinations(2).foreach(x=>x.mkString(","))

scala> arr.combinations(2).foreach(x=>println(x.mkString(",")))
a,b
a,c
a,d
a,e
b,c
b,d
b,e
c,d
c,e
d,e

scala> arr.combinations(4).foreach(x=>println(x.mkString(",")))
a,b,c,d
a,b,c,e
a,b,d,e
a,c,d,e
b,c,d,e

11、contains

  • override def contains[A1 >: Int](elem: A1): Boolean
    -【判断序列中是否包含指定对象】
scala> val a = List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)

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

scala> val c = List(2,4)
c: List[Int] = List(2, 4)

scala> a.contains(b)
res18: Boolean = false

scala> a.contains(2)
res19: Boolean = true

12、containsSlice

  • def containsSlice[B](that: scala.collection.GenSeq[B]): Boolean
    -【判断当前序列中是否包含另一个序列(顺序要一致)】
scala> a.containsSlice(b)
res20: Boolean = true

scala> a.containsSlice(c)
res21: Boolean = false

13、copyToArray

  • def copyToArray[B >: Int](xs: Array[B]): Unit
    -【将数组中内容逐个拷贝到新数组中】
scala> val a = Array('a','b','c','d')
a: Array[Char] = Array(a, b, c, d)

scala> val b:Array[Char]=new Array(5)
b: Array[Char] = Array(?, ?, ?, ?, ?)

scala> a.copyToArray(b)

scala> b
res23: Array[Char] = Array(a, b, c, d, ?)
  • def copyToArray[B >: Int](xs: Array[B],start: Int): Unit
    -【将数组中内容从新数组的起始索引处逐个拷贝到新数组中】
scala> val b:Array[Char]=new Array(5)
b: Array[Char] = Array(?, ?, ?, ?, ?)

scala> a.copyToArray(b,1)

scala> b
res34: Array[Char] = Array(?, a, b, c, d)
  • def copyToArray[B >: Int](xs: Array[B],start: Int,len: Int): Unit
    -【将数组中内容从新数组的起始索引处拷贝len个元素到新数组中】
scala> val b:Array[Char]=new Array(5)
b: Array[Char] = Array(?, ?, ?, ?, ?)

scala> a.copyToArray(b,1,2)

scala> b
res36: Array[Char] = Array(?, a, b, ?, ?)

14、copyToBuffer

  • def copyToBuffer[B >: Int](dest: scala.collection.mutable.Buffer[B]): Unit
    -【将数组中的内容拷贝到Buffer中】
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> a
res37: Array[Char] = Array(a, b, c, d)

scala> val b:ArrayBuffer[Char] = ArrayBuffer()
b: scala.collection.mutable.ArrayBuffer[Char] = ArrayBuffer()

scala> a.copyToBuffer(b)

scala> b
res39: scala.collection.mutable.ArrayBuffer[Char] = ArrayBuffer(a, b, c, d)

15、corresponds

  • final override def corresponds[B](that: scala.collection.GenSeq[B])(p: (Int, B) => Boolean): Boolean
    -【判断两个序列长度以及对应位置元素是否全部符合判断条件】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> a.corresponds(b)(_<_)
res41: Boolean = true

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

scala> val c = Array(4,5,7)
c: Array[Int] = Array(4, 5, 7)

scala> b.corresponds(c)(_<_)
res43: Boolean = false

scala> val d = Array(0,0,1,2)
d: Array[Int] = Array(0, 0, 1, 2)

scala> a.corresponds(d)(_<_)
res44: Boolean = false

16、count

  • def count(p: Int => Boolean): Int
    -【计算符合条件的元素个数】
scala> a.count(_>2)
res45: Int = 1

17、diff

  • def diff[B >: Int](that: scala.collection.GenSeq[B]): List[Int]
    -【获取第一个数组不同于第二个数组的所有元素】
scala> a
res47: Array[Int] = Array(1, 2, 3)

scala> b
res48: Array[Int] = Array(4, 5, 6)

scala> c
res49: Array[Int] = Array(4, 5, 7)

scala> d
res50: Array[Int] = Array(0, 0, 1, 2)

scala> a.diff(b)
res51: Array[Int] = Array(1, 2, 3)

scala> b.diff(c)
res52: Array[Int] = Array(6)

18、distinct

  • def distinct: List[Int]
    -【去除数组中的重复的元素,只保留一个并返回】
scala> d
res50: Array[Int] = Array(0, 0, 1, 2)

scala> d.distinct
res53: Array[Int] = Array(0, 1, 2)

19、drop

  • override def drop(n: Int): List[Int]
    -【删除数组中指定索引前的所有元素】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.drop(3)
res55: Array[Int] = Array(4, 5, 6, 7, 8)

scala> e
res56: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

20、dropRight

  • override def dropRight(n: Int): List[Int]
    -【从右边数,删除数组中指定索引前的所有元素】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

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

scala> e
res58: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

21、dropWhile

  • final override def dropWhile(p: Int => Boolean): List[Int]
    -【从当前数组的第一个元素起,判断是否满足指定条件,直到碰到第一个不满足条件的元素结束(即使后面还有符合条件的元素),删除结束前的所有元素。】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.dropWhile(_>4)
res59: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.dropWhile(_<4)
res60: Array[Int] = Array(4, 5, 6, 7, 8)

scala> e
res61: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

22、endsWith

  • def endsWith[B](that: scala.collection.GenSeq[B]): Boolean
    -【判断是否以某个序列结尾】
scala> val f = Array(6,7,8)
f: Array[Int] = Array(6, 7, 8)

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

scala> e.endsWith(f)
res63: Boolean = true

scala> f.endsWith(e)
res64: Boolean = false

23、exists

  • override def exists(p: Int => Boolean): Boolean
    -【判断当前数组是否包含符合条件的元素】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.exists(_==1)
res66: Boolean = true

scala> e.exists(_==9)
res67: Boolean = false

24、filter

  • def filter(p: Int => Boolean): List[Int]
    -【过滤出符合条件的元素并组成一个新的数组】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.filter(_%2==0)
res68: Array[Int] = Array(2, 4, 6, 8)

scala> e.filter(_<5)
res69: Array[Int] = Array(1, 2, 3, 4)

scala> e.filterNot(_%2==0)
res70: Array[Int] = Array(1, 3, 5, 7)

25、find

  • override def find(p: Int => Boolean): Option[Int]
    -【查找第一个符合条件的元素】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.find(_>5)
res71: Option[Int] = Some(6)

scala> e.find(_%2==0)
res72: Option[Int] = Some(2)

scala> e.find(_==10)
res74: Option[Int] = None

26、flatMap

  • def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Array[B]
    -【对当前序列的每个元素进行操作,返回的结果放到新数组中】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val x = a.flatMap(1 to _)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> var arr = Array("hello world","hello java","hello scala")
arr: Array[String] = Array(hello world, hello java, hello scala)

scala> arr.flatMap(_.split(" "))
res76: Array[String] = Array(hello, world, hello, java, hello, scala)

27、flatten

  • def flatten[B](implicit asTraversable: Int => scala.collection.GenTraversableOnce[B]): List[B]
    -【将二维数组合并成一维数组】
scala> val arr2 =Array(Array(1,2,3),Array(4,5,6))
arr2: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))

scala> arr2(1)(2)
res77: Int = 6

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

28、fold

  • def fold[A1 >: Int](z: A1)(op: (A1, A1) => A1): A1
    -【对序列中的每个元素进行二元运算(只需要一个方法)】
scala> def he(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he: (a: Int, b: Int)Int

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

scala> a.fold(5)(he)
he:5 1
he:6 2
he:8 3
res80: Int = 11

//【需要两个方法】
scala> a.par.aggregate(5)(he,he)
he:5 1
he:5 2
he:5 3
he:7 8
he:6 15
res81: Int = 21

29、foldLeft

  • override def foldLeft[B](z: B)(op: (B, Int) => B): B
    -【从左到右计算】
scala> def he(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he: (a: Int, b: Int)Int

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

scala> a.foldLeft(5)(he)
he:5 1
he:6 2
he:8 3
res82: Int = 11

scala> (5 /: a)(he)
he:5 1
he:6 2
he:8 3
res83: Int = 11

30、foldRight

  • override def foldRight[B](z: B)(op: (Int, B) => B): B
    -【从右到左计算】
scala> def he(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he: (a: Int, b: Int)Int

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

scala> a.foldRight(5)(he)
he:3 5
he:2 8
he:1 10
res84: Int = 11

scala> (a :\ 5)(he)
he:3 5
he:2 8
he:1 10
res85: Int = 11

31、forall

  • override def forall(p: Int => Boolean): Boolean
    -【判断序列中的所有元素是否全部满足条件】
scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> b
res86: Array[Int] = Array(4, 5, 6)

scala> b.forall(_>3)
res87: Boolean = true

scala> b.forall(_>4)
res88: Boolean = false

32、foreach

  • final override def foreach[U](f: Int => U): Unit
    -【循环遍历】
scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> b.foreach(println)
4
5
6

scala> b.foreach(x=>println(x*3))
12
15
18

33、groupBy

  • def groupBy[K](f: Int => K): scala.collection.immutable.Map[K,List[Int]]
    -【按条件进行分组,并将元素存放到map映射中】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e
res98: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.groupBy(_%2==0)
res102: scala.collection.immutable.Map[Boolean,Array[Int]] = Map(false -> Array(1, 3, 5, 7), true -> Array(2, 4, 6, 8))

scala> e.groupBy(x => x match{case x if(x%2==0)=>"TWO" case _ => "ONE"})
res103: scala.collection.immutable.Map[String,Array[Int]] = Map(TWO -> Array(2, 4, 6, 8), ONE -> Array(1, 3, 5, 7))

34、grouped

  • def grouped(size: Int): Iterator[List[Int]]
    -【按指定个数的元素进行分组,并存到序列中】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.grouped(3).foreach(x=>{x.foreach(print);println})
123
456
78

scala> val k = e.grouped(3).toList
k: List[Array[Int]] = List(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8))

scala> k.foreach(x=>println("第"+(k.indexOf(x)+1)+"组:"+x.mkString(",")))1组:1,2,32组:4,5,63组:7,8

35、hasDefiniteSize

  • def hasDefiniteSize: Boolean
    -【检测数组是否存在有限的长度,对应Stream这样的流数据,返回false】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.hasDefiniteSize
res117: Boolean = true

36、head

  • override def head: Int
    -【获取数组的首个元素】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.head
res122: Int = 1

37、headOption

  • def headOption: Option[Int]
    -【返回Option类型对象】
scala> val o = ArrayBuffer()
o: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()

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

scala> a.headOption
res120: Option[Int] = Some(1)

scala> o.headOption
res121: Option[Nothing] = None

38、indexOf

  • def indexOf(elem: T): Int
    -【获取序列中首个出现elem对象的索引】
scala> val l = Array(1,2,3,1,2,3)
l: Array[Int] = Array(1, 2, 3, 1, 2, 3)

scala> l.indexOf(3)
res111: Int = 2

scala> l.indexOf(4)
res112: Int = -1
  • indexOf(elem: T, from: Int): Int
    -【从某个索引处开始,获取序列中首个出现elem对象的索引】
scala> l.indexOf(3,4)
res113: Int = 5

scala> l.indexOf(5,1)
res114: Int = -1

scala> l.indexOf(3,0)
res115: Int = 2

39、indexOfSlice

  • def indexOfSlice[B >: A](that: GenSeq[B]): Int
    -【检测当前数组中是否包含另一个数组(that),并返回第一个匹配出现的元素的索引】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> e
res128: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> b
res129: Array[Int] = Array(4, 5, 6)

scala> e.indexOfSlice(b,4)
res130: Int = -1

scala> e.indexOfSlice(b,2)
res131: Int = 3

40、indexWhere

  • def indexWhere(p: (T) ⇒ Boolean): Int
    -【返回当前数组中第一个满足 p 条件的元素的索引】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.indexWhere(_%2==0)
res133: Int = 1
  • def indexWhere(p: (T) ⇒ Boolean, from: Int): Int
    -【从指定索引处开始,返回当前序列中第一个满足 p 条件的元素的索引】
scala> e.indexWhere(_>6,2)
res134: Int = 6

41、indices

  • def indices: scala.collection.immutable.Range
    -【返回序列中每个元素的下标】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.indices
res135: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6, 7)

scala> e.indices.mkString(",")
res136: String = 0,1,2,3,4,5,6,7

42、init

  • def init: List[Int]
    -【返回原序列但不包含序列中最后一个元素】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

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

43、inits

  • def inits: Iterator[List[Int]]
    -【对集合中的元素进行 init 操作】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.inits
res138: Iterator[Array[Int]] = non-empty iterator

scala> e.inits.foreach(println)
[I@773f516f
[I@5428edf3
[I@7d7fb107
[I@49be70af
[I@4407f4de
[I@5753ded8
[I@5724babc
[I@6d2583ff
[I@167f2b81

scala> e.inits.foreach(x=>{x.foreach(print);println})
12345678
1234567
123456
12345
1234
123
12
1

scala> val o = e.inits.toList
o: List[Array[Int]] = List(Array(1, 2, 3, 4, 5, 6, 7, 8), Array(1, 2, 3, 4, 5, 6, 7), Array(1, 2, 3, 4, 5, 6), Array(1, 2, 3, 4, 5), Array(1, 2, 3, 4), Array(1, 2, 3), Array(1, 2), Array(1), Array())

scala> o.foreach(println)
[I@18f04884
[I@be65fd6
[I@6140ffc8
[I@15944f1a
[I@fba8144
[I@369b778a
[I@50c3702e
[I@671819b3
[I@3c7314b5

scala> for(i <- 1 to o.length){
     | val s = "第%d个值:%s"
     | println(s.format(i,o(i-1).mkString(",")))
     | }1个值:1,2,3,4,5,6,7,82个值:1,2,3,4,5,6,73个值:1,2,3,4,5,64个值:1,2,3,4,55个值:1,2,3,46个值:1,2,37个值:1,28个值:19个值:

44、intersect

  • def intersect[B >: Int](that: scala.collection.GenSeq[B]): List[Int]
    -【求两个数组的交集】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e
res146: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> val d = Array(0,0,1,2)
d: Array[Int] = Array(0, 0, 1, 2)

scala> d
res147: Array[Int] = Array(0, 0, 1, 2)

scala> e.intersect(d)
res148: Array[Int] = Array(1, 2)

scala> d.intersect(e)
res149: Array[Int] = Array(1, 2)

45、isDefinedAt

  • override def isDefinedAt(x: Int): Boolean
    -【判断序列中是否存在指定索引】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.isDefinedAt(3)
res150: Boolean = true

scala> e.isDefinedAt(8)
res151: Boolean = false

46、isEmpty

  • override def isEmpty: Boolean
    -【判断序列是否为空】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.isEmpty
res152: Boolean = false

scala> val n = new ArrayBuffer()
n: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()

scala> n.isEmpty
res156: Boolean = true

47、isTraversableAgain

  • final def isTraversableAgain: Boolean
    -【判断数组是否可以反复遍历】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.isTraversableAgain
res160: Boolean = true

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

48、iterator

  • override def iterator: Iterator[Int]
    -【对数组中的每个元素生成一个迭代器】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

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

scala> m.foreach(println)
1
2
3
4
5
6
7
8

49、last

  • def last: T
    -【返回数组中最后一个元素】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

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

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

scala> e.last
res164: Int = 8

50、lastIndexOf

  • def lastIndexOf(elem: T): Int
    -【返回数组中最后一个等于elem的元素索引】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x
res169: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.lastIndexOf(1)
res170: Int = 3
  • def lastIndexOf(elem: T, end: Int): Int
    -【在指定索引end前,返回数组中最后一个等于elem的元素索引】
scala> x.lastIndexOf(1,2)
res171: Int = 1

51、lastIndexOfSlice

  • def lastIndexOfSlice[B >: A](that: GenSeq[B]): Int
    -【判断当前数组中是否包含数组that,并返回最后一次出现该数组的位置处的索引】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.lastIndexOfSlice(a)
res173: Int = 3
  • def lastIndexOfSlice[B >: A](that: GenSeq[B], end: Int): Int
    -【在指定索引end前,判断当前数组中是否包含数组that,并返回最后一次出现该数组的位置处的索引】
scala> x.lastIndexOfSlice(a,4)
res175: Int = 3

scala> x.lastIndexOfSlice(a,3)
res176: Int = 3

scala> x.lastIndexOfSlice(a,2)
res177: Int = -1

52、lastIndexWhere

  • def lastIndexWhere(p: (T) ⇒ Boolean): Int
    -【返回当前数组中最后一个满足条件 p 的元素的索引】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.lastIndexWhere(_<3)
res181: Int = 4
  • def lastIndexWhere(p: (T) ⇒ Boolean, end: Int): Int
    -【截止到下标为4,返回当前数组中最后一个满足条件 p 的元素的索引】
scala> x.lastIndexWhere(_==1,4)
res182: Int = 3

scala> x.lastIndexWhere(_==1,3)
res183: Int = 3

53、lastOption

  • def lastOption: Option[T]
    -【返回当前数组中最后一个对象】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.lastOption
res184: Option[Int] = Some(3)

54、length

  • def length: Int
    -【返回数组的长度】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.length
res185: Int = 6

55、lengthCompare

  • def lengthCompare(len: Int): Int
    -【返回数组的长度和参数len的差值】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.lengthCompare(12)
res203: Int = -6

56、lift

  • def lift: Int => Option[Int]
    -【返回数组中指定索引的对象】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.lift(0)
res209: Option[Int] = Some(1)

scala> x.lift(6)
res210: Option[Int] = None

57、map

  • def map[B](f: (A) ⇒ B): Array[B]
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> val n = x.map(_*2)
n: Array[Int] = Array(2, 2, 4, 2, 4, 6)

58、max

  • def max[B >: Int](implicit cmp: Ordering[B]): Int
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.max
res220: Int = 3

59、maxBy

  • def maxBy[B](f: Int => B)(implicit cmp: Ordering[B]): Int
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.maxBy(_%2)
res247: Int = 1

60、min

  • def min[B >: Int](implicit cmp: Ordering[B]): Int
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.min
res249: Int = 1

61、minBy

  • def minBy[B](f: Int => B)(implicit cmp: Ordering[B]): Int
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.minBy(_%2)
res248: Int = 2

62、mkString

  • def mkString: String
    -【转换成字符串】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.mkString
res251: String = 112123
  • def mkString(sep: String): String
scala> x.mkString(",")
res252: String = 1,1,2,1,2,3
  • def mkString(start: String, sep: String, end: String): String
scala> x.mkString("{",",","}")
res253: String = {1,1,2,1,2,3}

63、nonEmpty

  • def nonEmpty: Boolean
    -【判断序列是否不为空】
scala> val n = ArrayBuffer()
n: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()

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

scala> x.nonEmpty
res289: Boolean = true

scala> n.nonEmpty
res290: Boolean = false

64、padTo

  • def padTo(len: Int, elem: A): Array[A]
    -【后补齐序列,如果当前数组长度小于 len,那么新产生的数组长度是 len,多出的几个位值填充 elem,如果当前数组大于等于 len ,则返回当前数组】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.padTo(10,null)
res297: Array[Any] = Array(1, 1, 2, 1, 2, 3, null, null, null, null)

scala> x.padTo(4,6)
res299: Array[Int] = Array(1, 1, 2, 1, 2, 3)

65、par

def par: ParArray[T]
【返回一个并行实现,产生的并行数组,不能被修改】

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

scala> val m = a.par
m: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3)

scala> m.foreach(println)
1
3
2

66、partition

  • def partition(p: (T) ⇒ Boolean): (Array[T], Array[T])
    -【把一维数组按条件分为二维数组】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e
res328: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> val m = e.partition(_%2==0)
m: (Array[Int], Array[Int]) = (Array(2, 4, 6, 8),Array(1, 3, 5, 7))

67、patch

  • def patch(from: Int, that: GenSeq[A], replaced: Int): Array[A]
    -【批量替换,从原序列的 from 处开始,使用 replaced 数量个元素,替换成序列 that】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> a.patch(1,b,1)
res334: Array[Int] = Array(1, 4, 5, 6, 3)

68、permutations

  • def permutations: collection.Iterator[Array[T]]
    -【排列组合,组合中的内容可以相同,但是顺序不能相同】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

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

scala> val m = a.permutations.toList
m: 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> m.foreach(x=>println(x.mkString(",")))
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1

-【combinations不允许包含的内容相同,即使顺序不一样】

scala> val n = a.combinations(3).toList
n: List[Array[Int]] = List(Array(1, 2, 3))

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

69、prefixLength

  • def prefixLength(p: (T) ⇒ Boolean): Int
    -【返回符合条件p的数组总长度】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x
res347: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.prefixLength(_<2)
res348: Int = 2

scala> x.prefixLength(_>1)
res350: Int = 0

70、product

  • def product: A
    -【返回序列中所有元素的乘积】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.product
res353: Int = 12

scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

71、reduce

  • def reduce[A1 >: Int](op: (A1, A1) => A1): A1
    -【对序列中的每个元素进行二元运算,同fold,不需要初始值】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

scala> x.reduce(he2)
he:1 1
he:2 2
he:4 1
he:5 2
he:7 3
res354: Int = 10

72、reduceLeft

  • override def reduceLeft[B >: Int](f: (B, Int) => B): B
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

scala> x.reduceLeft(he2)
he:1 1
he:2 2
he:4 1
he:5 2
he:7 3
res357: Int = 10

73、reduceRight

  • override def reduceRight[B >: Int](op: (Int, B) => B): B
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

scala> x.reduceRight(he2)
he:2 3
he:1 5
he:2 6
he:1 8
he:1 9
res358: Int = 10

74、reduceOption

  • def reduceOption[A1 >: Int](op: (A1, A1) => A1): Option[A1]
  • 【返回数组中元素的总和】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

scala> x.reduceOption(he2)
he:1 1
he:2 2
he:4 1
he:5 2
he:7 3
res383: Option[Int] = Some(10)

75、reduceLeftOption

  • def reduceLeftOption[B >: Int](op: (B, Int) => B): Option[B]
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

scala> x.reduceLeftOption(he2)
he:1 1
he:2 2
he:4 1
he:5 2
he:7 3
res359: Option[Int] = Some(10)

76、reduceRightOption

  • def reduceRightOption[B >: Int](op: (Int, B) => B): Option[B]
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int
scala> x.reduceRightOption(he2)
he:2 3
he:1 5
he:2 6
he:1 8
he:1 9
res360: Option[Int] = Some(10)

77、reverse

  • override def reverse: List[Int]
    -【反转序列】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.reverse
res361: Array[Int] = Array(3, 2, 1, 2, 1, 1)

78、reverseIterator

  • def reverseIterator: Iterator[Int]
    -【反向生成迭代】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.reverseIterator
res362: Iterator[Int] = non-empty iterator

scala> res362
res363: Iterator[Int] = non-empty iterator

scala> res362.foreach(println)
3
2
1
2
1
1

79、reverseMap

def reverseMap[B](f: (A) ⇒ B): Array[B]
【反向生成Map】

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

scala> x.reverseMap(_+2)
res365: Array[Int] = Array(5, 4, 3, 4, 3, 3)

scala> val o = Array(4,5,6)
o: Array[Int] = Array(4, 5, 6)

scala> val c = Array(4,5,7)
c: Array[Int] = Array(4, 5, 7)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

80、sameElements

  • def sameElements(that: GenIterable[A]): Boolean
    -【判断两个数组是否顺序和对应位置上的元素都一样】
scala> val o = Array(4,5,6)
o: Array[Int] = Array(4, 5, 6)

scala> val c = Array(4,5,7)
c: Array[Int] = Array(4, 5, 7)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> o.sameElements(b)
res377: Boolean = true

scala> o.sameElements(c)
res378: Boolean = false

81、scan

  • def scan[B >: Int, That](z: B)(op: (B, B) => B)(implicit cbf:scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
    -【用法同 fold,scan会把每一步的计算结果放到一个新的集合中返回,而 fold 返回的是单一的值】
scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

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

scala> e
res390: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.scan(8)(he2)
he:8 1
he:9 2
he:11 3
he:14 4
he:18 5
he:23 6
he:29 7
he:36 8
res391: Array[Int] = Array(8, 9, 11, 14, 18, 23, 29, 36, 44)

82、scanLeft

  • def scanLeft[B, That](z: B)(op: (B, Int) => B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

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

scala> e.scanLeft(8)(he2)
he:8 1
he:9 2
he:11 3
he:14 4
he:18 5
he:23 6
he:29 7
he:36 8
res392: Array[Int] = Array(8, 9, 11, 14, 18, 23, 29, 36, 44)

83、scanRight

  • def scanRight[B, That](z: B)(op: (Int, B) => B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
scala> def he2(a:Int,b:Int)={
     | println("he:"+a+" "+b)
     | a+b
     | }
he2: (a: Int, b: Int)Int

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

scala> e.scanRight(8)(he2)
he:8 8
he:7 16
he:6 23
he:5 29
he:4 34
he:3 38
he:2 41
he:1 43
res393: Array[Int] = Array(44, 43, 41, 38, 34, 29, 23, 16, 8)

84、segmentLength

  • override def segmentLength(p: Int => Boolean,from: Int): Int
    -【从from开始,统计数组中符合条件p的连续元素长度】
scala> val o = Array(1,2,3,1,1,1,1,1,4,5)
o: Array[Int] = Array(1, 2, 3, 1, 1, 1, 1, 1, 4, 5)

scala> o.segmentLength(_<3,3)
res394: Int = 5

85、seq

  • def seq: scala.collection.mutable.IndexedSeq[Int]
    -【产生一个引用当前数组的 sequential 视图】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.seq
res396: scala.collection.mutable.IndexedSeq[Int] = WrappedArray(1, 2, 3)

scala> res396.foreach(println)
1
2
3

86、size

  • override def size: Int
    -【返回数组的元素个数】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.size
res398: Int = 3

scala> e
res399: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

87、slice

  • def slice(from: Int, until: Int): Array[T]
    -【取出当前数组中,from 到 until 之间的片段】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.slice(1,2)
res400: Array[Int] = Array(2)

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

88、sliding

  • def sliding(size: Int): collection.Iterator[Array[T]]
    -【从第一个元素开始,每个元素和它后面的 size - 1 个元素组成一个数组,
    最终组成一个新的集合返回,当剩余元素不够 size 数,则停止】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.sliding(3)
res402: Iterator[Array[Int]] = non-empty iterator

scala> e.sliding(3).foreach(println)
[I@3252bc88
[I@7e020fc3
[I@7c72f99
[I@44ef97c8
[I@637ed521
[I@d6cb29a

scala> e.sliding(3).foreach(x=>{x.foreach(print);println})
123
234
345
456
567
678
  • def sliding(size: Int, step: Int): collection.Iterator[Array[T]]
    -【从第一个元素开始,每个元素和它后面的(size-1)个元素组成一个数组,最终组成一个新的集合返回,当剩余元素不够 size 数,则停止。第一个元素组合完后,下一个从上一个元素位置+step步进后的位置处的元素开始。】
scala> e.sliding(3,2).foreach(x=>{x.foreach(print);println})
123
345
567
78

89、sortBy

  • def sortBy[B](f: (T) ⇒ B)(implicit ord: math.Ordering[B]): Array[T]
    -【按指定排序规则排序】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x
res408: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.sortBy(x=>x)
res411: Array[Int] = Array(1, 1, 1, 2, 2, 3)

90、sortWith

  • def sortWith(lt: (T, T) ⇒ Boolean): Array[T]
    -【自定义排序方法lt】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.sortWith(_.compareTo(_) > 0)
res413: Array[Int] = Array(3, 2, 2, 1, 1, 1)

91、sorted

  • def sorted[B >: Int](implicit ord: scala.math.Ordering[B]): Array[Int]
    -【使用默认的排序规则排序】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.sorted
res416: Array[Int] = Array(1, 1, 1, 2, 2, 3)

92、span

  • def span(p: (T) ⇒ Boolean): (Array[T], Array[T])
    -【分割数组为两个集合,从第一个元素开始,直到找到第一个不满足条件的元素为止,之前的元素放到第一个数组,其它的放到第二个数组】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.span(_>4)
res418: (Array[Int], Array[Int]) = (Array(),Array(1, 2, 3, 4, 5, 6, 7, 8))

scala> e.span(_<4)
res419: (Array[Int], Array[Int]) = (Array(1, 2, 3),Array(4, 5, 6, 7, 8))

93、splitAt

  • def splitAt(n: Int): (Array[T], Array[T])
    -【把数组从起始位置,分割到n的位置,不包括n,分别放入到两个数组中】
scala> val x = Array(1, 1, 2, 1, 2, 3)
x: Array[Int] = Array(1, 1, 2, 1, 2, 3)

scala> x.splitAt(4)
res424: (Array[Int], Array[Int]) = (Array(1, 1, 2, 1),Array(2, 3))

94、startsWith

  • def startsWith[B](that: GenSeq[B]): Boolean
    -【判断是否以某个数组开始】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

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

scala> e.startsWith(a)
res427: Boolean = true
  • def startsWith[B](that: GenSeq[B], offset: Int): Boolean
    -【从指定偏移处,判断是否以某个数组开始】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> e.startsWith(b,3)
res429: Boolean = true

scala> e.startsWith(b,0)
res430: Boolean = false

scala> e.toString()
res433: String = [I@2e46fd18

95、stringPrefix

  • def stringPrefix: String
    -【返回 toString 结果的前缀】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.stringPrefix
res434: String = [I

96、subSequence

  • def subSequence(start: Int, end: Int): CharSequence
    -【返回 start 和 end 间的字符数组】
scala> val chars = Array('a','b','c','d')
chars: Array[Char] = Array(a, b, c, d)

scala> chars.subSequence(1,3)
res436: CharSequence = bc

scala> chars.subSequence(1,2)
res437: CharSequence = b

97、sum

  • def sum: A
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.sum
res440: Int = 6

98、tail

  • def tail: Array[T]
    -【返回除了当前数组第一个元素的其它元素组成的数组】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.tail
res441: Array[Int] = Array(2, 3)

99、take

  • def take(n: Int): Array[T]
    -【返回当前数组中前n个元素组成的数组】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.take(2)
res442: Array[Int] = Array(1, 2)

100、takeRight

  • def takeRight(n: Int): Array[T]
    -【从右边开始,返回n个元素组成的数组】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.takeRight(2)
res443: Array[Int] = Array(2, 3)

101、takeWhile

  • def takeWhile(p: (T) ⇒ Boolean): Array[T]
    -【返回当前数组中,从第一个元素开始,满足条件的连续元素组成的数组】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.takeWhile(_<5)
res448: Array[Int] = Array(1, 2, 3, 4)

scala> e.takeWhile(_>5)
res449: Array[Int] = Array()

102、数组的转化方法

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

scala> e.toArray
res450: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.toBuffer
res451: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.toIndexedSeq
res452: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.toIterable
res453: Iterable[Int] = WrappedArray(1, 2, 3, 4, 5, 6, 7, 8)

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

scala> e.toList
res455: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)

103、集合的转化方法

scala> val n = Array(("name","gjy"),("age",23),("sex","male"))
n: Array[(String, Any)] = Array((name,gjy), (age,23), (sex,male))

scala> n.toMap
res456: scala.collection.immutable.Map[String,Any] = Map(name -> gjy, age -> 23, sex -> male)

scala> n.toSeq
res457: Seq[(String, Any)] = WrappedArray((name,gjy), (age,23), (sex,male))

scala> n.toSet
res458: scala.collection.immutable.Set[(String, Any)] = Set((name,gjy), (age,23), (sex,male))

scala> n.toStream
res459: scala.collection.immutable.Stream[(String, Any)] = Stream((name,gjy), ?)

scala> n.toVector
res460: Vector[(String, Any)] = Vector((name,gjy), (age,23), (sex,male))

104、transpose

  • def transpose[U](implicit asArray: (T) ⇒ Array[U]): Array[Array[U]]
    -【矩阵转换,二维数组行列转换】
scala> val m = Array(Array("a","b"),Array("c","d"),Array("e","f"))
m: Array[Array[String]] = Array(Array(a, b), Array(c, d), Array(e, f))

scala> m.transpose
res463: Array[Array[String]] = Array(Array(a, c, e), Array(b, d, f))

105、union

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

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> a.union(b)
res466: Array[Int] = Array(1, 2, 3, 4, 5, 6)

106、unzip

  • def unzip[T1, T2](implicit asPair: (T) ⇒ (T1, T2), ct1: ClassTag[T1], ct2: ClassTag[T2]): (Array[T1], Array[T2])
scala> val b1 = Array(("a","b"),("d","e"))
b1: Array[(String, String)] = Array((a,b), (d,e))

scala> b1.unzip
res469: (Array[String], Array[String]) = (Array(a, d),Array(b, e))

107、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 b2 = Array(("a","b","c"),("d","e","f"))
b2: Array[(String, String, String)] = Array((a,b,c), (d,e,f))

scala> b2.unzip3
res470: (Array[String], Array[String], Array[String]) = (Array(a, d),Array(b, e),Array(c, f))

scala> val b3 = Array(("a","b","c"),("d","e","f"),("j","h","i"))
b3: Array[(String, String, String)] = Array((a,b,c), (d,e,f), (j,h,i))

scala> b3.unzip3
res472: (Array[String], Array[String], Array[String]) = (Array(a, d, j),Array(b, e, h),Array(c, f, i))

108、update

  • def update(i: Int, x: T): Unit
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.update(1,0)

scala> e
res475: Array[Int] = Array(1, 0, 3, 4, 5, 6, 7, 8)

109、updated

  • def updated(index: Int, elem: A): Array[A]
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.updated(1,0)
res478: Array[Int] = Array(1, 0, 3, 4, 5, 6, 7, 8)

110、view

  • def view(from: Int, until: Int): IndexedSeqView[T, Array[T]]
    -【返回 from 到 until 间的数组,不包括 until 处的元素】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.view(1,6).mkString(",")
res481: String = 2,3,4,5,6

111、withFilter

  • def withFilter(p: (T) ⇒ Boolean): FilterMonadic[T, Array[T]]
    -【根据条件 p 过滤元素】
scala> val e = Array(1,2,3,4,5,6,7,8)
e: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> e.withFilter(_>5).map(x=>x)
res482: Array[Int] = Array(6, 7, 8)

112、zip

  • def zip[B](that: GenIterable[B]): Array[(A, B)]
    -【将两个数组对应位置上的元素组成一个pair数组】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val c = Array(4,5,7)
c: Array[Int] = Array(4, 5, 7)

scala> a.zip(c)
res488: Array[(Int, Int)] = Array((1,4), (2,5), (3,7))

113、zipAll

  • zipAll[B, A1 >: Int, That](that: scala.collection.GenIterable[B],thisElem: A1,thatElem: B)(implicit bf:scala.collection.generic.CanBuildFrom[Array[Int],(A1, B),That]): That
    -【同zip,但是允许两个序列长度不一样,不足的自动填充,如果当前序列端,空出的填充为 thisElem,如果 that 短,填充为 thatElem】
scala> val d = Array(7,3,2,1,5)
d: Array[Int] = Array(7, 3, 2, 1, 5)

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

scala> d.zipAll(a,8,4)
res491: Array[(Int, Int)] = Array((7,1), (3,2), (2,3), (1,4), (5,4))

scala> a.zipAll(d,8,4)
res492: Array[(Int, Int)] = Array((1,7), (2,3), (3,2), (8,1), (8,5))

114、zipWithIndex

  • def zipWithIndex[A1 >: Int, That](implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],(A1, Int),That]): That
    -【数组中的每个元素和它的索引组成一个数组】
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.zipWithIndex
res493: Array[(Int, Int)] = Array((1,0), (2,1), (3,2))
  • 4
    点赞
  • 0
    评论
  • 7
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值