集合元素操作返回单个元素
elemManifest elemTag
返回集合元素的类型
elemManifest过期了
scala> a
res87: Array[Int] = Array(1, 2, 3)
//过期
scala> a.elemManifest
<console>:16: warning: method elemManifest in class WrappedArray is deprecated (since 2.10.0): use elemTag instead
a.elemManifest
^
res88: ClassManifest[Int] = Int
scala> a.elemTag
res89: scala.reflect.ClassTag[Int] = Int
scala> c
res90: Array[Any] = Array(a, b, 3)
scala> c.elemTag
res91: scala.reflect.ClassTag[Any] = Object
head
定义:def head: T
描述:返回序列的第一个元素,如果序列为空,将引发错误
scala> a3
res16: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2)
scala> a3.head
res17: Int = 1
scala> var a4:Array[Int]=Array()
a4: Array[Int] = Array()
scala> a4.head
java.util.NoSuchElementException: next on empty iterator
at scala.collection.Iterator$$anon$2.next(Iterator.scala:41)
at scala.collection.Iterator$$anon$2.next(Iterator.scala:39)
at scala.collection.IndexedSeqLike$Elements.next(IndexedSeqLike.scala:63)
at scala.collection.IterableLike.head(IterableLike.scala:109)
headOption
定义:def headOption: Option[T]
描述:返回序列的第一个元素的 Option 类型对象,如果序列为空,则返回 None
scala> a3
res19: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2)
scala> a4
res20: Array[Int] = Array()
scala> a3.head
head headOption
scala> a3.headOption
res21: Option[Int] = Some(1)
scala> a4.headOption
res22: Option[Int] = None
last
定义:def last: T
描述:返回序列的第一个元素,如果序列为空,将引发错误
scala> a3
res16: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2)
scala> a3.last
res17: Int = 2
scala> var a4:Array[Int]=Array()
a4: Array[Int] = Array()
scala> a4.last
java.util.NoSuchElementException: next on empty iterator
at scala.collection.Iterator$$anon$2.next(Iterator.scala:41)
at scala.collection.Iterator$$anon$2.next(Iterator.scala:39)
at scala.collection.IndexedSeqLike$Elements.next(IndexedSeqLike.scala:63)
at scala.collection.IterableLike.head(IterableLike.scala:109)
lastOption
定义:def lastOption: Option[T]
描述:返回序列的第一个元素的 Option 类型对象,如果序列为空,则返回 None
scala> a3
res19: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2)
scala> a4
res20: Array[Int] = Array()
scala> a3.headOption
res21: Option[Int] = Some(2)
scala> a4.lastOption
res22: Option[Int] = None
max min
描述:返回序列中最大/小的元素
scala> a3
res11: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.max
res12: Int = 5
scala> a3.min
res13: Int = 1
maxBy
定义:def maxBy[B](f: (A) ⇒ B): A
描述:返回序列中符合条件的第一个元素
如果所有元素都不满足,返回第一个元素
scala> a3
res11: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.maxBy(_>3)
res14: Int = 4
scala> a3.maxBy(_<0)
res15: Int = 1
find
定义:def find(p: (T) ⇒ Boolean): Option[T]
描述:查找第一个符合条件的元素,返回 Option
如果有,用some包装再返回
没有返回None
scala> var a3=Array(1,2,3,4,5)
a3: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.find(_>3)
res0: Option[Int] = Some(4)
scala> a3.find(_<0)
res1: Option[Int] = None
minBy
定义:def minBy[B](f: (A) ⇒ B): A
描述:返回序列中不符合条件的第一个元素
没有满足条件的就返回第一个值
scala> a3
res11: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.minBy(_<3)
res15: Int = 3
scala> a3.minBy(_>6)
res15: Int = 1
update
定义:def update(i: Int, x: T): Unit
描述:将序列中 i 索引处的元素更新为 x
使用后原数组改变
scala> a1
res37: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.update(1,10)
scala> a1
res39: Array[Int] = Array(1, 10, 3, 4, 5, 6, 7, 8, 9)
集合元素操作后返回集合
genericBuilder
返回一个以目标数组对应的可变类型为类型的空集合
scala> a
res97: Array[Int] = Array(1, 2, 3)
scala> a.genericBuilder
res98: scala.collection.mutable.Builder[Nothing,scala.collection.mutable.IndexedSeq[Nothing]] = ArrayBuffer()
companion
返回一个以目标数组对应的可变类型为类型,以参数为元素的集合
scala> var a=Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> a.companion(3,4,5)
res74: scala.collection.mutable.IndexedSeq[Int] = ArrayBuffer(3, 4, 5)
+:
定义:def +:(elem: A): Array[A]
描述:在数组前面添加一个元素,并返回新的数组对象
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1:+10
res0: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
:+
定义:def :+(elem: A): Array[A]
描述:在数组后面添加一个元素,并返回新的数组对象
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> 11+:a1
res1: Array[Int] = Array(11, 1, 2, 3, 4, 5, 6, 7, 8, 9)
repr
返回自身,暂时不明白啥意思
scala> a
res134: Array[Int] = Array(1, 2, 3)
scala> a.repr
res135: Array[Int] = Array(1, 2, 3)
clone
定义:def clone(): Array[T]
描述: 创建一个数组的副本,复制数组的值,但不会引用地址;就是说是浅克隆,不是深克隆。
scala> var a=Array(1,3,5,2,5,6,7)
a: Array[Int] = Array(1, 3, 5, 2, 5, 6, 7)
scala> a.clone
res0: Array[Int] = Array(1, 3, 5, 2, 5, 6, 7)
combinations 组合
定义:def combinations(n: Int): collection.Iterator[Array[T]]
描述:combinations 表示组合,这个排列组合会选出所有包含字符不一样的组合,但不考虑顺序,对于 “abc”、“cba”,视为相同组合,参数 n 表示序列长度,就是几个字符为一组
scala> var a2= Array(1,2,3,4,5)
a2: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a2.combinations(2)
res6: Iterator[Array[Int]] = <iterator>
//返回值是一个Array[Int]的迭代器,意思是返回一个二维数组,只是只能使用一次
scala> a2.combinations(2).foreach(x=>println(x.mkString(",")))
1,2
1,3
1,4
1,5
2,3
2,4
2,5
3,4
3,5
4,5
permutations 排列
定义:def permutations: collection.Iterator[Array[T]]
描述:permutations 表示排列,这个排列组合会选出所有排列顺序不同的字符组合,permutations 与 combinations 不同的是,相同的组合考虑排列,对于 “abc”、“cba”,视为不同的组合
scala> var a6=Array(1,2,3)
a6: Array[Int] = Array(1, 2, 3)
scala> a6.permutations
res29: Iterator[Array[Int]] = <iterator>
scala> a6.permutations.toList
res30: 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))
distinct
定义:def distinct: Array[T]
描述:去除当前集合中重复的元素,只保留一个
scala> var same=Array(1,3,5,7,1,3,5,7)
same: Array[Int] = Array(1, 3, 5, 7, 1, 3, 5, 7)
scala> same.distinct
res15: Array[Int] = Array(1, 3, 5, 7)
drop
定义:def drop(n: Int): Array[T]
描述:将当前数组中前 n 个元素去除,返回一个新数组
scala> a2
res11: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a2.drop(2)
res12: Array[Int] = Array(3, 4, 5)
dropRight
定义:def dropRight(n: Int): Array[T]
描述:功能同 drop,去掉尾部的 n 个元素
scala> a2
res11: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a2.dropRight(2)
res13: Array[Int] = Array(1, 2, 3)
dropWhile
定义:def dropWhile(p: (T) ⇒ Boolean): Array[T]
描述:去除当前数组中符合条件的元素,返回剩余的数组,这个需要一个条件,就是从当前数组的第一个元素起,就要满足条件,直到碰到第一个不满足条件的元素结束(即使后面还有符合条件的元素),否则返回整个数组
scala> var a3=Array(1,2,3,4,5,1,2)
a3: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2)
scala> a3.dropWhile(_<3)
res15: Array[Int] = Array(3, 4, 5, 1, 2)
take takeRight
定义:def take(n: Int): Array[T]
描述:返回当前序列中,前 n 个元素组成的序列
定义:def takeRight(n: Int): Array[T]
描述:返回当前序列中,从右边开始,后 n 个元素组成的序列
scala> a3
res6: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.take(3)
res7: Array[Int] = Array(1, 2, 3)
scala> a3.takeRight(3)
res8: Array[Int] = Array(3, 4, 5)
takeWhile filter
定义:def takeWhile(p: (T) ⇒ Boolean): Array[T]
描述:返回当前序列中,从第一个元素开始,满足条件的连续元素组成的序列,当遇到第一个不满足,结束
定义:def filter(p: (T) ⇒ Boolean): Array[T]
描述:取得当前数组中符合条件的元素,组成新的数组返回
scala> var a4=Array(1,2,3,4,5,1,2)
a4: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2)
scala> a4.takeWhile(_<3)
res9: Array[Int] = Array(1, 2)
scala> a4.filter(_<3)
res22: Array[Int] = Array(1, 2, 1, 2)
filterNot
定义:def filter(p: (T) ⇒ Boolean): Array[T]
描述:取得当前数组中不符合条件的元素,组成新的数组返回
scala> a4
res32: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2)
scala> a4.filterNot(_>=3)
res33: Array[Int] = Array(1, 2, 1, 2)
withFilter
定义:def withFilter(p: (T) ⇒ Boolean): FilterMonadic[T, Array[T]]
描述:根据条件 p 过滤元素,不生成新的集合
不太懂啥意思,反正还是可以遍历,尽量少用吧
scala> a4
res28: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2)
scala> a4.withFilter(_<3)
res29: scala.collection.generic.FilterMonadic[Int,Array[Int]] = scala.collection.TraversableLike$WithFilter@2da333d
scala> a4.withFilter(_<3).foreach(println)
1
2
1
2
tail
定义:def tail: Array[T]
描述:返回当前序列中不包含第一个元素的序列,除了头都是尾
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.tail
res0: Array[Int] = Array(2, 3, 4, 5, 6, 7, 8, 9)
init
定义:def init: Array[T]
描述:返回当前序列中不包含最后一个元素的序列,除了尾都是头
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.init
res0: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)
tails
定义:def tails: collection.Iterator[Array[T]]
描述:对集合中的元素进行 tail 迭代操作,该操作的返回值中, 第一个值是当前序列的副本,最后一个值为空,每一步都进行tail 操作,上一步的结果作为下一步的操作对象
scala> var a3=Array(1,2,3,4,5)
a3: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.tails
res2: Iterator[Array[Int]] = <iterator>
scala> a3.tails.toArray
res3: Array[Array[Int]] = Array(Array(1, 2, 3, 4, 5), Array(2, 3, 4, 5), Array(3, 4, 5), Array(4, 5), Array(5), Array())
scala> a3.inits
res4: Iterator[Array[Int]] = <iterator>
scala> a3.inits.toArray
res5: Array[Array[Int]] = Array(Array(1, 2, 3, 4, 5),
Array(1, 2, 3, 4), Array(1, 2, 3), Array(1, 2),
Array(1), Array())
inits
描述:对集合中的元素进行 init 迭代操作,该操作的返回值中, 第一个值是当前序列的副本,最后一个值为空,每一步都进行 init 操作,上一步的结果作为下一步的操作对象
scala> var a3=Array(1,2,3,4,5)
a3: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.inits
res4: Iterator[Array[Int]] = <iterator>
scala> a3.inits.toArray
res5: Array[Array[Int]] = Array(Array(1, 2, 3, 4, 5),
Array(1, 2, 3, 4), Array(1, 2, 3), Array(1, 2),
Array(1), Array())
padTo
定义:def padTo(len: Int, elem: A): Array[A]
描述:填充序列,如果当前序列长度小于 len,那么新产生的序列长度是 len,多出的几个位值填充 elem,如果当前序列大于等于 len ,则返回当前序列
scala> a3
res11: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.padTo(8,"-")
res17: Array[Any] = Array(1, 2, 3, 4, 5, -, -, -)
patch
定义:def patch(from: Int, that: GenSeq[A], replaced: Int): Array[A]
描述:批量替换,从原序列的 from 处开始,后面的 replaced 个元素,将被替换成序列 that
scala> a3
res11: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.patch(2,Array(7,8),3)
res20: Array[Int] = Array(1, 2, 7, 8)
scala> a3.patch(2,Array(7,8),2)
res21: Array[Int] = Array(1, 2, 7, 8, 5)
scala> a2
res24: Array[Int] = Array()
scala> a3.patch(2,a2,2)
res25: Array[Int] = Array(1, 2, 5)
//用空集合替换相当于删除
slice
定义:def slice(from: Int, until: Int): Array[T]
描述:返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素
允许下标越界
scala> a
res35: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.slice(1,3)
res36: Array[Int] = Array(2, 3)
view
定义:def view(from: Int, until: Int): IndexedSeqView[T, Array[T]]
描述:返回当前序列中从 from 到 until 之间的可变序列,不包括 until 处的元素
允许下标越界
view后面不加参数返回全部
view(index:int) 和apply一样,取下标对应的值
scala> a
res35: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.view(1,3)
res38: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewS(...)
scala> a.view(1,3).toArray
res39: Array[Int] = Array(2, 3)
scala> a.view
res86: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqView(...)
scala> a.view.toArray
res87: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.view(1)
res88: Int = 2
subSequence
定义:def subSequence(start: Int, end: Int): CharSequence
描述:返回 start 和 end 间的字符序列,不包含 end 处的元素
(返回 start 和 end 间的字符序列 只针对char类型(前包后不包))
允许下标越界
scala> var c = Array('a','b','c','d','e')
c: Array[Char] = Array(a, b, c, d, e)
scala> c.subSequence(1,3)
res33: CharSequence = bc
scala> c.subSequence(-1,7)
res34: CharSequence = abcde
seq
定义:def seq: collection.mutable.IndexedSeq[T]
描述:产生一个引用当前序列的 sequential 视图,就是一个特殊的集合
scala> a
res89: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.seq
res90: scala.collection.mutable.IndexedSeq[Int] = WrappedArray(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.seq.toArray
res91: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.seq(1)
res92: Int = 2
//没有和view一样的这功能
scala> a.seq(1,3)
<console>:13: error: too many arguments (2) for method apply: (idx: Int)Int in trait SeqLike
a.seq(1,3)
updated
定义:def updated(index: Int, elem: A): Array[A]
描述:将序列中 i 索引处的元素更新为 x,并返回替换后的数组
原序列不变
scala> a1
res39: Array[Int] = Array(1, 10, 3, 4, 5, 6, 7, 8, 9)
scala> a1.updated(2,11)
res40: Array[Int] = Array(1, 10, 11, 4, 5, 6, 7, 8, 9)
数组中的计算
fold
/: foldLeft
:\ foldRight
fold
scala> var a1=Array.range(1,5)
a1: Array[Int] = Array(1, 2, 3, 4)
scala> var add=(x:Int,y:Int)=>{print(s"$x+$y=${x+y}\n");x+y}
add: (Int, Int) => Int = $$Lambda$1071/980326486@4863c8ac
1.fold
scala> a1.fold(0)(_+_)
res9: Int = 10
scala> a1.fold(0)(add)
0+1=1
1+2=3
3+3=6
6+4=10
res2: Int = 10
2.par.fold
scala> a1.par.fold(0)(add)
0+3=3
0+1=1
0+4=4
0+2=2
3+4=7
1+2=3
3+7=10
res3: Int = 10
3.foldLeft
scala> a1.foldLeft(0)(add)
0+1=1
1+2=3
3+3=6
6+4=10
res4: Int = 10
scala> (0/:a1)(_+_)
<console>:13: warning: method /: in trait TraversableOnce is deprecated (since 2.12.10): Use foldLeft instead of /:
(0/:a1)(_+_)
res9: Int = 10
4.foldRight
scala> a1.foldRight(0)(add)
4+0=4
3+4=7
2+7=9
1+9=10
res5: Int = 10
scala> (a1:\0)(_+_)
<console>:13: warning: method :\ in trait TraversableOnce is deprecated (since 2.12.10): Use foldRight instead of :\
(a1:\0)(_+_)
^
res11: Int = 10
scan
scan
scanLeft
scanRight
定义:def scan[B >: A, That](z: B)(op: (B, B) ⇒ B)(implicit cbf: CanBuildFrom[Array[T], B, That]): That
描述:同 fold,scan 会把每一步的计算结果放到一个新的集合中返回,而 fold 返回的是最后的结果
scala> a1
res15: Array[Int] = Array(1, 2, 3, 4)
scala> a1.scan(0)(_+_)
res12: Array[Int] = Array(0, 1, 3, 6, 10)
scala> a1.scanLeft(0)(_+_)
res13: Array[Int] = Array(0, 1, 3, 6, 10)
scala> a1.scanRight(0)(_+_)
res14: Array[Int] = Array(10, 9, 7, 4, 0)
aggregate
定义:def aggregate[B](z : => B)(seqop : scala.Function2[B, A, B], combop : scala.Function2[B, B, B]) : B = { /* compiled code */ }
描述:aggregate是柯里化方法(将原来接受两个参数的函数变成新的接受一个参数的函数的过程。新的函数返回一个以原有第二个参数为参数的函数), aggregate 方法是一个聚合函数,接受多个输入,并按照一定的规则运算以后输出一个结果值
var a1=Array.range(1,6)
var mapAdd=(x:Int,y:Int)=>{
var z=x+y
println(s"map: $x+$y=$z")
z
}
var reAdd=(x:Int,y:Int)=>{
var z=x+y
println(s"red: $x+$y=$z")
z
}
1.aggregate
scala> a1.aggregate(0)(_+_,_+_)
res10: Int = 15
println(a1.aggregate(0)(mapAdd,reAdd))
#没有走reAdd
map: 0+1=1
map: 1+2=3
map: 3+3=6
map: 6+4=10
map: 10+5=15
15
2.par.aggregate
println(a1.par.aggregate(0)(mapAdd,reAdd))
#走reAdd
map: 0+4=4
map: 0+2=2
map: 0+5=5
map: 0+1=1
map: 0+3=3
red: 4+5=9
red: 1+2=3
red: 3+9=12
red: 3+12=15
15
reduce
reduce
reduceLeft
reduceRight
定义:def reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1
描述:同 fold,对序列中的每个元素进行二元运算,不需要初始值
当序列为空时,会报异常
scala> a1
res18: Array[Int] = Array(1, 2, 3, 4)
scala> a1.reduce(_+_)
res12: Int = 10
scala> var a2:Array[Int]=Array()
a2: Array[Int] = Array()
scala> a2.reduce(_+_)
java.lang.UnsupportedOperationException: empty.reduceLeft
at scala.collection.TraversableOnce.reduceLeft(TraversableOnce.scala:185)
at scala.collection.TraversableOnce.reduceLeft$(TraversableOnce.scala:183)
at scala.collection.mutable.ArrayOps$ofInt.scala$collection$IndexedSeqOptimized$$super$reduceLeft(ArrayOps.scala:246)
at scala.collection.IndexedSeqOptimized.reduceLeft(IndexedSeqOptimized.scala:77)
reduceOption
reduceOption
reduceLeftOption
reduceRightOption
定义:def reduceOptionA1>:A: scala.Option[A1]
描述: 同 reduceLeftOption,计算Option,返回 Option
序列不为空,返回some,为空返回none
scala> a1
res18: Array[Int] = Array(1, 2, 3, 4)
scala> a2
res19: Array[Int] = Array()
scala> a1.reduceOption(_+_)
res20: Option[Int] = Some(10)
scala> a2.reduceOption(_+_)
res21: Option[Int] = None
scala> a1.reduceLeftOption(_+_)
res22: Option[Int] = Some(10)
scala> a2.reduceLeftOption(_+_)
res23: Option[Int] = None
scala> a1.reduceRightOption(_+_)
res24: Option[Int] = Some(10)
scala> a2.reduceRightOption(_+_)
res25: Option[Int] = None
sum
定义:def sum: A
描述:序列求和,元素需为 Numeric[T] 类型
scala> a1
res26: Array[Int] = Array(1, 2, 3, 4)
scala> a1.sum
res27: Int = 10
product
定义:def product: A
描述:返回所有元素乘积的值
scala> a1
res26: Array[Int] = Array(1, 2, 3, 4)
scala> a1.product
res29: Int = 24
字符串相关操作
addString
addString(b)
addString(b, sep)
addString(b,start,sep,end)
定义:def addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder
描述:将数组中的元素逐个添加到b中,在首尾各加一个字符串,并指定sep分隔符
scala> var a1=Array(1,2,3,4)
a1: Array[Int] = Array(1, 2, 3, 4)
scala> val builder = new scala.collection.mutable.StringBuilder
builder: StringBuilder =
scala> a1.addString(builder,"[","-","]")
res0: StringBuilder = [1-2-3-4]
scala> a1
res1: Array[Int] = Array(1, 2, 3, 4)
scala> builder
res2: StringBuilder = [1-2-3-4]
mkString
mkString(b)
mkString(b, sep)
mkString(b,start,sep,end)
定义:def mkString(start: String, sep: String, end: String): String
描述:将序列中所有元素拼接成一个字符串,以 start 开头,以 sep 作为元素间的分隔符,以 end 结尾
scala> a1
res3: Array[Int] = Array(1, 2, 3, 4)
scala> a1.mkString("[","-","]")
res4: String = [1-2-3-4]
stringPrefix
定义:def stringPrefix: String
描述:返回 toString 结果的前缀
scala> a1
res5: Array[Int] = Array(1, 2, 3, 4)
scala> a1.stringPrefix
res6: String = [I
方法操作
柯里化andThen compose
scala> val f1=(x:Int)=>x+3
f1: Int => Int = $$Lambda$1243/1765776053@77ef7b2b
scala> val f2=(x:Int)=>x*3
f2: Int => Int = $$Lambda$1244/853482026@4fecdf0b
scala> val f3=(x:Int)=>x-1
f3: Int => Int = $$Lambda$1245/849575186@1054864f
scala> def f=f1 andThen f2 andThen f3//f1先执行f2后执行
f: Int => Int
scala> def g=f1 compose f2 compose f3 //f3先执行f2后执行
g: Int => Int
scala> f(2)
res14: Int = 14
scala> g(2)
res15: Int = 6
//andThen前面是方法def时,要加“_”
scala> def f1(x:Int)=x*2
f1: (x: Int)Int
scala> def g1(x:Int)=x+2
g1: (x: Int)Int
scala> def rst=f1 _ andThen g1
rst: Int => Int
scala> rst(3)
res14: Int = 8
现在再看数组用
下标不存在报错,下标存在就把对应元素传入偏函数中,输出偏函数的结果,总算是有点用处吧
scala> a1
res38: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.andThen(x=>{if (x%3==0) x else x+2})(2)
res39: Int = 3
scala> a1.andThen(x=>{if (x%3==0) x else x+2})(3)
res40: Int = 6
scala> a1.andThen(x=>{if (x%3==0) x else x+2})(10)
java.lang.ArrayIndexOutOfBoundsException: 10
at scala.collection.mutable.WrappedArray$ofInt.apply$mcII$sp(WrappedArray.scala:174)
at scala.collection.mutable.WrappedArray$ofInt.apply(WrappedArray.scala:174)
at scala.collection.mutable.WrappedArray$ofInt.apply(WrappedArray.scala:171)
collect collectFirst
collect
定义:def collect[B](pf: PartialFunction[A, B]): Array[B]
描述:通过执行一个并行计算(偏函数),得到一个新的数组对象
collectFirst
定义:def collectFirst[B](pf: PartialFunction[T, B]): Option[B]
描述:在序列中查找第一个符合偏函数定义的元素,并执行偏函数计算
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.collect({case i if i%2==0 =>i*2})
res33: Array[Int] = Array(4, 8, 12, 16)
scala> a1.collectFirst({case i if i%2==0 =>i*2})
res34: Option[Int] = Some(4)
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
iterator
定义:def iterator: collection.Iterator[T]
描述:生成当前序列的迭代器
只能使用一次
一般很少使用,当用其他方法得到iterator时,经常还要转成序列
scala> a1
res40: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> var it=a1.iterator
it: Iterator[Int] = <iterator>
scala> it.foreach(println)
1
2
3
4
5
6
7
8
9
scala> it.foreach(println)
//没有了
runWith
第二个参数为下标,不存在返回false
下表存在则将下标下的元素传入偏函数中
//偏函数用match case 且最后一行不加case i=>i之类的,好歹不满足条件会报错,让我知道下标对应的元素不满足我的条件
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.runWith(x=>x match{case i if i%2==0 =>i*2})(3)
res0: Boolean = true
scala> a1.runWith(x=>x match{case i if i%2==0 =>i*2})(2)
scala.MatchError: 3 (of class java.lang.Integer)
at .$anonfun$res1$1(<console>:13)
at scala.runtime.java8.JFunction1$mcII$sp.apply(JFunction1$mcII$sp.java:23)
at scala.PartialFunction.$anonfun$runWith$1$adapted(PartialFunction.scala:146)
at scala.Function1.apply$mcZI$sp(Function1.scala:41)
... 28 elided
scala> a1.runWith(x=>x match{case i if i%2==0 =>i*2})(10)
res2: Boolean = false
//如果不用match,跟偏函数有啥关系啊,毫无关系好不好,我要它有何用,判断下标是否存在???
scala> a1.runWith(x=>{if (x%3==0) "match"})(1)
res15: Boolean = true
scala> a1.runWith(x=>{if (x%3==0) "match"})(10)
res16: Boolean = false
scala> a1.runWith(x=>{if (x%3==0) "match"})(2)
res17: Boolean = true
scala> a1.runWith(x=>{if (x%3==0) "match"})(3)
res18: Boolean = true
说实话感觉没啥用,正宗用法是这样的
定义:def runWith[U](action : scala.Function1[B, U]) : scala.Function1[A, scala.Boolean] = { /* compiled code */ }
描述: 执行偏函数,当参数不在定义域内时,返回false,否则返回true,并执行action
scala> val pf: PartialFunction[Int, Int] = {
| case m: Int if m % 2 == 1=> m * 2 }
pf: PartialFunction[Int,Int] = <function1>
scala> pf.runWith(println)(4)
res4: Boolean = false
scala> pf.runWith(println)(5)
10
res5: Boolean = true
map
定义:def map[B](f: (A) ⇒ B): Array[B]
描述:对序列中的元素进行 f 操作,返回生成的新序列
(1)最简单的用法:改变每个元素
scala> var arr=Array(1,2,3)
arr: Array[Int] = Array(1, 2, 3)
scala> arr.map(_+1)
res36: Array[Int] = Array(2, 3, 4)
scala> arr.map(x=>x+1)
res38: Array[Int] = Array(2, 3, 4)
scala> arr
res37: Array[Int] = Array(1, 2, 3)
(2)原集合的元素也为集合,只取每个集合的某个下标或某些下标的元素组成新的集合
如果不加类型,拼接的集合元素变为了元组(因为元组就是())
加类型,就会直接变成我想要的类型
scala> a
res39: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8, 9))
scala> a.map(x=>(x(1),x(2)))
res40: Array[(Int, Int)] = Array((2,3), (5,6), (8,9))
scala> a.map(x=>Array(x(1),x(2)))
res49: Array[Array[Int]] = Array(Array(2, 3), Array(5, 6), Array(8, 9))
map可以一层一层的转变,案例:
scala> var a=Array(Array(1,2,3),Array(4,5,6),Array(7,8))
a: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8))
//每个元素都+1
scala> a.map(_.map(_+1))
res8: Array[Array[Int]] = Array(Array(2, 3, 4), Array(5, 6, 7), Array(8, 9))
//第二种写法
scala> a.map(x=>x.map(x=>x+1))
res9: Array[Array[Int]] = Array(Array(2, 3, 4), Array(5, 6, 7), Array(8, 9))
var arr = Array("henry","male","nanjing",28,12000,
"pola","female","shanghai",22,18000,"ariel","female","nanjing",
20,22000,"angela","male","nanjing",28,12000,"xiaoming","female",
"shanghai",25,19500)
求不同性别的平均薪资
scala> arr.sliding(5,5).map(x=>(x(1),x(4).asInstanceOf[Int])).toArray.
| groupBy(x=>x._1).map(x=>(x._1,x._2.map(x=>x._2).sum/x._2.length))
res63: scala.collection.immutable.Map[Any,Int] = Map(male -> 12000, female -> 19833)
transform
定义:def transform(f: Int => Int): scala.collection.mutable.WrappedArray[Int]
描述:将序列中的元素转换成指定的元素(同类型)
transform必须同类型转换,且会改变数组,因此大部分情况还是用map
使用后原数组改变
scala> a1
res30: Array[Int] = Array(2, 3, 35)
scala> a1.transform(_+1)
res31: scala.collection.mutable.WrappedArray[Int] = WrappedArray(3, 4, 36)
scala> a1
res32: Array[Int] = Array(3, 4, 36)
//原数组改变
返回boolean的判断
contains
定义:def contains[A1 >: A](elem: A1): Boolean
描述:判断序列中是否包含指定对象
scala> a1
res64: Array[Int] = Array(3, 3, 4)
scala> a1.contains(3)
res65: Boolean = true
scala> a1.contains(5)
res66: Boolean = false
containsSlice
定义:def containsSlice[B](that: GenSeq[B]): Boolean
描述:判断当前序列中是否包含另一个序列(数量和顺序相等)
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.containsSlice(Array(3,6,9))
res68: Boolean = false
scala> a1.containsSlice(List(1,2,3))
res71: Boolean = true
startsWith
startsWith(that)
定义:def startsWith[B](that: GenSeq[B]): Boolean
描述:判断序列是否以某个序列开始
startsWith(that, offset)
定义:def startsWith[B](that: GenSeq[B], offset: Int): Boolean
描述:判断序列从指定偏移处是否以某个序列开始
scala> a1
res74: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.startsWith(Array(1,2))
res75: Boolean = true
scala> a1.startsWith(Array(2,3))
res76: Boolean = false
scala> a1.startsWith(Array(1,2),1)
res77: Boolean = false
scala> a1.startsWith(Array(2,3),1)
res78: Boolean = true
endsWith
定义:def endsWith[B](that: GenSeq[B]): Boolean
描述:判断当前序列是否以某个序列结尾
scala> a1
res79: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.endsWith(Array(7,8,9))
res81: Boolean = true
scala> a1.endsWith(Array(1,2))
res82: Boolean = false
exists
定义:def exists(p: (T) ⇒ Boolean): Boolean
描述:判断当前数组是否包含符合条件的元素
scala> a1
res79: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.exists(_<6)
res84: Boolean = true
scala> a1.exists(_>10)
res85: Boolean = false
forall
定义:def forall(p: (T) ⇒ Boolean): Boolean
描述:检测序列中的元素是否都满足条件 p,如果序列为空,则返回 true
scala> a1
res86: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.forall(_>0)
res88: Boolean = true
scala> a1.forall(_>10)
res89: Boolean = false
scala> var b:Array[Int]=Array()
b: Array[Int] = Array()
scala> b.forall(_>0)
res10: Boolean = true
hasDefiniteSize
定义:def hasDefiniteSize: Boolean
描述:检测序列是否存在有限的长度,对应 Stream 这样的流数据则返回 false
scala> var a=Array.range(1,10)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a.hasDefiniteSize
res0: Boolean = true
scala> a.toStream.hasDefiniteSize
res2: Boolean = false
isDefinedAt
定义:def isDefinedAt(idx: Int): Boolean
描述:判断序列中是否存在指定索引
scala> a
res3: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a.isDefinedAt(3)
res4: Boolean = true
scala> a.isDefinedAt(10)
res5: Boolean = false
isEmpty nonEmpty
定义:def isEmpty: Boolean
描述:判断序列是否为空
定义:def nonEmpty: Boolean
描述:判断序列是否不为空
scala> a
res7: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> b
res8: Array[Int] = Array()
scala> a.isEmpty
res9: Boolean = false
scala> b.isEmpty
res10: Boolean = true
scala> a.nonEmpty
res11: Boolean = true
scala> b.nonEmpty
res12: Boolean = false
isTraversableAgain
定义:def isTraversableAgain: Boolean
描述:判断序列是否可以反复遍历,该方法是 GenTraversableOnce 中的方法,对于 Traversables 一般返回 true,对于 Iterators 返回 false,除非被复写 或者说判断是否为迭代器(只能遍历一次)
scala> a
res17: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a.isTraversableAgain
res18: Boolean = true
scala> a.iterator.isTraversableAgain
res19: Boolean = false
下标(索引)操作
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
apply
定义:def apply(i: Int): T
描述:获取指定索引处的元素
下标不存在报错
view也有相同用法
scala> a1
res18: Array[Int] = Array(1, 2, 34)
scala> a1.apply(0)
res19: Int = 1
scala> a1.view(0)
res19: Int = 1
scala> a1.apply(-1)
java.lang.ArrayIndexOutOfBoundsException: -1
... 28 elided
applyOrElse
定义: def applyOrElse[A1 <: A, B1 >: B](x : A1, default : scala.Function1[A1, B1]) : B1 = { /* compiled code */ }
描述:接收2个参数,第一个是调用的参数,第二个是个回调函数。如果第一个调用的参数能匹配,返回匹配的值,否则调用回调函数。(回调函数的输入类型需与调用参数类型一致),就相当于if-else分支调用函数。
scala> a1
res21: Array[Int] = Array(1, 2, 34)
scala> a1.applyOrElse(1,{i:Int=>"None"})
res23: Any = 2
scala> a1.applyOrElse(-1,{i:Int=>"None"})
res24: Any = None
lift
取集合对应下标的值,返回Option类型
即:有值用some包装,没有返回None
scala> a.lift(2)
res110: Option[Int] = Some(3)
scala> a.lift(3)
res111: Option[Int] = None
indexOf(elem) lastIndexOf
indexOf(elem)
定义:def indexOf(elem: T): Int
描述:返回元素 elem 在序列中第一次出现的索引,没有返回-1
indexOf(elem, from)
定义:def indexOf(elem: T, from: Int): Int
描述:返回元素 elem 在序列中第一次出现的索引,指定从索引 from 开始查找,没有返回-1
lastIndexOf(elem)
定义:def lastIndexOf(elem: T): Int
描述:返回元素 elem 在序列中最后一次出现的索引
lastIndexOf(elem, end)
定义:def lastIndexOf(elem: T, end: Int): Int
描述:返回元素 elem 在序列中最后一次出现的索引,指定在索引 end 之前(包括)的元素中查找
scala> a
res2: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.indexOf(1)
res3: Int = 0
scala> a.indexOf(1,4)
res4: Int = 4
scala> a.indexOf(1,5)
res5: Int = -1
scala> a.lastIndexOf(1)
res14: Int = 4
scala> a.lastIndexOf(1,3)
res15: Int = 0
indexOfSlice lastIndexOfSlice
indexOfSlice(that)
定义:def indexOfSlice[B >: A](that: GenSeq[B]): Int
描述:检测当前序列中是否包含序列 that,并返回第一次出现该序列的索引,找不到返回-1;并且指定索引可以超出元素索引的范围
indexOfSlice(that, from)
定义:def indexOfSlice[B >: A](that: GenSeq[B], from: Int): Int
描述:检测当前序列中是否包含另一个序列 that,指定从索引 from 开始查找,并返回第一次出现该序列的索引
lastIndexOfSlice(that)
定义:def lastIndexOfSlice[B >: A](that: GenSeq[B]): Int
描述:检测当前序列中是否包含序列 that,并返回最后一次出现该序列的索引
lastIndexOfSlice(that, end)
定义:def lastIndexOfSlice[B >: A](that: GenSeq[B], end: Int): Int
描述:检测当前序列中是否包含序列 that,并返回最后一次出现该序列的索引,指定在索引 end 之前(包括)的元素中查找
scala> a
res6: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.indexOfSlice(List(1,2,3))
res7: Int = 0
scala> a.indexOfSlice(List(3,2,1))
res8: Int = -1
scala> a.indexOfSlice(List(3,4,5))
res9: Int = -1
scala> a.indexOfSlice(List(1,2,3),1)
res10: Int = -1
scala> a.lastIndexOfSlice(List(9,8))
res16: Int = 7
scala> a.lastIndexOfSlice(List(9,8),7)
res17: Int = 7
scala> a.lastIndexOfSlice(List(9,8),6)
res18: Int = -1
indexWhere lastIndexWhere
indexWhere§
定义:def indexWhere(p: (T) ⇒ Boolean): Int
描述:返回当前序列中第一个满足条件 p 的元素的索引
indexWhere(p, from)
定义:def indexWhere(p: (T) ⇒ Boolean, from: Int): Int
描述:返回当前序列中第一个满足条件 p 的元素的索引,指定从索引 from 开始查找
lastIndexWhere§
定义:def lastIndexWhere(p: (T) ⇒ Boolean): Int
描述:返回当前序列中最后一个满足条件 p 的元素的索引
lastIndexWhere(p, end)
定义:def lastIndexWhere(p: (T) ⇒ Boolean, end: Int): Int
描述:返回当前序列中最后一个满足条件 p 的元素的索引,指定在索引 end 之前(包括)的元素中查找
scala> a
res11: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.indexWhere(_>3)
res12: Int = 3
scala> a.indexWhere(_>3,4)
res13: Int = 5
scala> a.lastIndexWhere(_>7)
res22: Int = 8
scala> a.lastIndexWhere(_>7,7)
res23: Int = 7
scala> a.lastIndexWhere(_>7,5)
res24: Int = -1
indices
定义:def indices: collection.immutable.Range
描述:返回当前序列索引集合
scala> a
res30: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.indices
res31: scala.collection.immutable.Range = Range 0 until 9
scala> a.indices.toArray
res32: Array[Int] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8)
数组拆分
grouped
定义:def grouped(size: Int): collection.Iterator[Array[T]]
描述:按指定数量分组,每组有 size 个元素,返回一个迭代器
scala> a
res42: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.grouped(2)
res43: Iterator[Array[Int]] = <iterator>
scala> a.grouped(2).toArray
res44: Array[Array[Int]] = Array(Array(1, 2), Array(3, 5), Array(1, 6), Array(2, 9), Array(8))
sliding
sliding(size)
定义:def sliding(size: Int): collection.Iterator[Array[T]]
描述:滑动,从第一个元素开始,每个元素和它后面的 size个元素组成一个数组,最终组成一个新的集合返回,当剩余元素个数不够size时,则结束
sliding(size, step)
定义:def sliding(size: Int): collection.Iterator[Array[T]]
描述:从第一个元素开始,每个元素和它后面的 size个元素组成一个数组,最终组成一个新的集合返回,当剩余元素个数不够 size 时,则结束。该方法可以设置步长 step,每一组元素组合完后,下一组从上一组起始元素位置 + step 后的位置处开始
当size=step时,与grouped相同
scala> a
res50: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.sliding(2)
res51: Iterator[Array[Int]] = <iterator>
scala> a.sliding(2).toArray
res52: Array[Array[Int]] = Array(Array(1, 2), Array(2, 3), Array(3, 5), Array(5, 1), Array(1, 6), Array(6, 2), Array(2, 9), Array(9, 8))
scala> a.sliding(2,1).toArray
res53: Array[Array[Int]] = Array(Array(1, 2), Array(2, 3), Array(3, 5), Array(5, 1), Array(1, 6), Array(6, 2), Array(2, 9), Array(9, 8))
scala> a.sliding(2,2).toArray
res54: Array[Array[Int]] = Array(Array(1, 2), Array(3, 5), Array(1, 6), Array(2, 9), Array(8))
scala> a.grouped(2).toArray
res57: Array[Array[Int]] = Array(Array(1, 2), Array(3, 5), Array(1, 6), Array(2, 9), Array(8))
scala> a.sliding(2,3).toArray
res55: Array[Array[Int]] = Array(Array(1, 2), Array(5, 1), Array(2, 9))
groupBy
定义:def groupBy[K](f: (T) ⇒ K): Map[K, Array[T]]
描述:按条件分组,条件由 f 匹配,返回值是 Map 类型,每个 key 对应一个数组
var arr1=Array(("male",12000), ("female",18000),
("female",22000), ("male",12000), ("female",19500))
#以元素直接分组,用元素直接做键(也可以看作最简单的条件)
scala> arr1.groupBy(x=>x._1)
res58: scala.collection.immutable.Map[Any,Array[(Any, Int)]] =
Map(male -> Array((male,12000), (male,12000)),
female -> Array((female,18000), (female,22000), (female,19500)))
scala> arr1.groupBy(_._1)
res45: scala.collection.immutable.Map[String,Array[(String, Int)]] =
Map(male -> Array((male,12000), (male,12000)),
female -> Array((female,18000), (female,22000), (female,19500)))
#以条件分组,不自定义键,系统自动分配boolean值作为键,只能分为两个键值对
arr1.groupBy(_._2>14000)
res68: scala.collection.immutable.Map[Boolean,Array[(String, Int)]] =
Map(false -> Array((male,12000), (male,12000)),
true -> Array((female,18000), (female,22000), (female,19500)))
#以条件分组,且自定义键,可以分为多个键值对
#只分为两组,生成两个键值对
scala> arr1.groupBy(_._2 match{
case i if(i>14000) =>"high"
case _=>"low"
})
res56: scala.collection.immutable.Map[String,Array[(Any, Int)]] =
Map(high -> Array((female,18000), (female,22000), (female,19500)),
low -> Array((male,12000), (male,12000)))
#分为3组,生成三个键值对
scala> arr1.groupBy(_._2 match{
case i if(i>19000) =>"high"
case i if(i>17000) =>"middle"
case _=>"low"
})
res3: scala.collection.immutable.Map[String,Array[(String, Int)]] =
Map(high -> Array((female,22000), (female,19500)),
middle -> Array((female,18000)),
low -> Array((male,12000), (male,12000)))
把HashMap里的值拿出来,两种:apply,get
scala> res58.apply("male")
res60: Array[(Any, Int)] = Array((male,12000), (male,12000))
scala> res58.get("male")
res61: Option[Array[(Any, Int)]] = Some([Lscala.Tuple2;@1d7ed2ef)
scala> res58.get("male").get
res62: Array[(Any, Int)] = Array((male,12000), (male,12000))
partition
定义:def partition(p: (T) ⇒ Boolean): (Array[T], Array[T])
描述:按条件将序列拆分成两个数组,满足条件的放到第一个数组,其余的放到第二个数组,返回的是包含这两个数组的元组
和groupBy的差别就是返回的是元组,慎用
scala> a.partition(_>3)
res59: (Array[Int], Array[Int]) = (Array(5, 6, 9, 8),Array(1, 2, 3, 1, 2))
scala> a.groupBy(_>3)
res60: scala.collection.immutable.Map[Boolean,Array[Int]] = Map(false -> Array(1, 2, 3, 1, 2), true -> Array(5, 6, 9, 8))
span
定义:def span(p: (T) ⇒ Boolean): (Array[T], Array[T])
描述:将序列拆分成两个数组,从第一个元素开始,直到第一个不满足条件的元素为止,其中的元素放到第一个数组,其余的放到第二个数组,返回的是包含这两个数组的元组
scala> a
res6: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.span(_<4)
res7: (Array[Int], Array[Int]) = (Array(1, 2, 3),Array(5, 1, 6, 2, 9, 8))
scala> a.partition(_<4)
res9: (Array[Int], Array[Int]) = (Array(1, 2, 3, 1, 2),Array(5, 6, 9, 8))
scala> a.groupBy(_<4)
res10: scala.collection.immutable.Map[Boolean,Array[Int]] = Map(false -> Array(5, 6, 9, 8), true -> Array(1, 2, 3, 1, 2))
splitAt
定义:def splitAt(n: Int): (Array[T], Array[T])
描述:从指定位置开始,把序列拆分成两个数组,指定索引位置元素放入第二个数组中
scala> a
res12: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.splitAt(3)
res13: (Array[Int], Array[Int]) = (Array(1, 2, 3),Array(5, 1, 6, 2, 9, 8))
数组排序
sort
sorted
定义:def sorted[B >: A](implicit ord: math.Ordering[B]): Array[T]]
描述:使用默认的排序规则对序列排序,默认升序排列,无参数
sortWith
定义:def sortWith(t:(T,T)=>boolean):Array[T]
描述:自定义排序方法
sortBy
定义:def sortBy[B](f: (T) ⇒ B)(implicit ord: math.Ordering[B]): Array[T]
描述:按指定的排序规则对序列排序
scala> a
res14: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.sorted
res15: Array[Int] = Array(1, 1, 2, 2, 3, 5, 6, 8, 9)
scala> a.sortBy(x=>x)
res16: Array[Int] = Array(1, 1, 2, 2, 3, 5, 6, 8, 9)
scala> a.sortBy(x=> -x)
res18: Array[Int] = Array(9, 8, 6, 5, 3, 2, 2, 1, 1)
scala> a.sortWith(_<_)
res19: Array[Int] = Array(1, 1, 2, 2, 3, 5, 6, 8, 9)
scala> a.sortWith(_>_)
res20: Array[Int] = Array(9, 8, 6, 5, 3, 2, 2, 1, 1)
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 //多线程执行,所以无序的
数组长度
size length
定义:def size: Int
描述:返回序列元素个数,同 length
scala> a
res24: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.length
res25: Int = 9
scala> a.size
res26: Int = 9
lengthCompare
定义:def lengthCompare(len: Int): Int
描述:比较序列的长度和参数 len,返回序列的长度 - len
scala> a
res27: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.lengthCompare(8)
res28: Int = 1
scala> a.lengthCompare(9)
res29: Int = 0
scala> a.lengthCompare(10)
res30: Int = -1
满足条件元素的个数
segmentLength
segmentLength
定义:def segmentLength(p: (T) ⇒ Boolean, from: Int): Int
描述:从序列的 from 开始向后查找,返回满足条件 p 的连续元素的长度,只返回第一个
prefixLength
定义:def prefixLength(p: (T) ⇒ Boolean): Int
描述:由第一个索引开始,返回序列中满足条件p的元素数量,直到有不满足的则结束
count
定义:def count(p: (T) ⇒ Boolean): Int
描述:从第一个索引开始,统计符合条件的元素个数
scala> a
res31: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> a.segmentLength(_>2,1)
res33: Int = 0
scala> a.segmentLength(_>2,2)
res34: Int = 2
scala> a.prefixLength(_>2)
res35: Int = 0
scala> a.count(_>2)
res36: Int = 5
prefixLength
定义:def prefixLength(p: (T) ⇒ Boolean): Int
描述:由第一个索引开始,返回序列中满足条件p的元素数量,直到有不满足的则结束
scala> a3
res32: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a3.prefixLength(_<3)
res33: Int = 2
count
定义:def count(p: (T) ⇒ Boolean): Int
描述:统计符合条件的元素个数
scala> var a1=Array(3,5,7,1,3,2)
a1: Array[Int] = Array(3, 5, 7, 1, 3, 2)
scala> a1.count(_>2)
res20: Int = 4
//遍历完
scala> a1.prefixLength(_>2)
res21: Int = 3
//找到不满足的就停止
两数组判断
canEqual
定义:def canEqual(that: Any): Boolean
描述:判断两个对象是否可以进行比较
基本上所有对象都可以进行比较,我不知道这个方法的意义何在
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> var a2=Array('a','b','c','d','e')
a2: Array[Char] = Array(a, b, c, d, e)
scala> a1.canEqual(a2)
res17: Boolean = true
== eq
定义:def eq(that: Any): Boolean
描述:判断两个对象地址是否相等
恕我直言,两个对象地址怎么能相同呢,我不知道这个方法的意义何在
scala> var a1=Array.range(1,10)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> var copy=a1.clone
copy: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a1.eq(copy)
res41: Boolean = false
scala> a1==(copy)
res42: Boolean = false
sameElements
定义:def sameElements(that: GenIterable[A]): Boolean
描述:判断两个序列是否顺序和对应位置上的元素都一样
scala> a
res44: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> b
res45: Array[Int] = Array(1, 2, 3, 5, 1, 6, 2, 9, 8)
scala> c
res46: Array[Int] = Array(1, 2, 3)
scala> a.sameElements(b)
res47: Boolean = true
scala> a.sameElements(c)
res48: Boolean = false
corresponds
定义:def corresponds[B](that: GenSeq[B])(p: (T, B) ⇒ Boolean): Boolean
描述:判断两个序列的长度以及对应位置元素是否符合某个条件。如果两个序列具有相同的元素数量并且 p(x, y)=true,则返回 true,不满足返回false
当条件为对应位置元素是否相同时,和sameElements一样
scala> var d=Array.range(1,10)
d: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> var e=Array.range(2,11)
e: Array[Int] = Array(2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> var f=Array.range(1,5)
f: Array[Int] = Array(1, 2, 3, 4)
scala> var g=Array.range(1,10)
g: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> d.corresponds(e)(_<_)
res49: Boolean = true
scala> d.corresponds(e)(_>_)
res50: Boolean = false
scala> d.corresponds(e)(_<_)
res51: Boolean = true
scala> d.corresponds(f)(_<_)
res52: Boolean = false
scala> d.corresponds(f)(_==_)
res54: Boolean = false
scala> d.corresponds(g)(_==_)
res55: Boolean = true
两个集合做合并
++
++ union 完全一样
++:
1.都是immutable或都是mutbale时
scala> a1
res59: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> copy
res60: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a2
res61: Array[Char] = Array(a, b, c, d, e)
++ ++: 一样
1.类型相同
scala> a1++copy
res9: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9)
2.类型不同
scala> a1++a2
res10: Array[AnyVal] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e)
2.一个是immutable另一个是mutbale时
++ 结果类型为immutable还是mutable由++前面决定
++: 结果类型为immutable还是mutable由++:后面决定
scala> var a1=Array(1,2,3)
a1: Array[Int] = Array(1, 2, 3)
scala> val a2 = scala.collection.mutable.ListBuffer(3, 4)
a2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(3, 4)
scala> a1++a2
res1: Array[Int] = Array(1, 2, 3, 3, 4)
scala> a2++a1
res2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(3, 4, 1, 2, 3)
scala> a1++:a2
res3: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 3, 4)
scala> a2++:a1
res4: Array[Int] = Array(3, 4, 1, 2, 3)
diff
定义:def diff(that: collection.Seq[T]): Array[T]
描述:计算当前数组与另一个数组的差集,即将当前数组中没有在另一个数组中出现的元素返回
scala> var a1=Array(3,4,3,4,6,6,7)
a1: Array[Int] = Array(3, 4, 3, 4, 6, 6, 7)
scala> var a2=Array(3,4,8,9)
a2: Array[Int] = Array(3, 4, 8, 9)
//a1中有两个(3,4),只能消去一个
scala> a1.diff(a2)
res64: Array[Int] = Array(3, 4, 6, 6, 7)
//顺序变了结果是不一样的
scala> a2.diff(a1)
res65: Array[Int] = Array(8, 9)
intersect
定义:def intersect(that: collection.Seq[T]): Array[T]
描述:取两个集合的交集
scala> a1
res67: Array[Int] = Array(3, 4, 3, 4, 6, 6, 7)
scala> a2
res68: Array[Int] = Array(3, 4, 8, 9)
scala> a1.intersect(a2)
res69: Array[Int] = Array(3, 4)
scala> a2.intersect(a1)
res70: Array[Int] = Array(3, 4)
数组复制到另一个数组 copyToArray
copyToArray
定义:def copyToArray(xs: Array[A]): Unit
描述:将当前数组元素全部复制到另一个数组中,从第一个下标开始,
数组会改变
scala> var a=new Array[Int](10)
a: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> var b=Array(1,2,3,4)
b: Array[Int] = Array(1, 2, 3, 4)
scala> b.copyToArray(a)
scala> a
res3: Array[Int] = Array(1, 2, 3, 4, 0, 0, 0, 0, 0, 0)
copyToArray(xs, start)
定义:def copyToArray(xs: Array[A], start: Int): Unit
描述:将当前数组元素全部复制到另一个数组中,从 start 位置开始复制
scala> var a=new Array[Int](10)
a: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> b
res4: Array[Int] = Array(1, 2, 3, 4)
scala> b.copyToArray(a,3)
scala> a
res6: Array[Int] = Array(0, 0, 0, 1, 2, 3, 4, 0, 0, 0)
copyToArray(xs, start, len)
定义:def copyToArray(xs: Array[A], start: Int, len: Int): Unit
描述:将当前数组元素部分复制到另一个数组中,从 start 位置开始复制,长度为 len
scala> a
res6: Array[Int] = Array(0, 0, 0, 1, 2, 3, 4, 0, 0, 0)
scala> var a=new Array[Int](10)
a: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> b
res7: Array[Int] = Array(1, 2, 3, 4)
scala> b.copyToArray(a,3,2)
scala> a
res9: Array[Int] = Array(0, 0, 0, 1, 2, 0, 0, 0, 0, 0)
copyToBuffer
定义:def copyToBuffer[B >: A](dest: Buffer[B]): Unit
描述:将数组中的元素复制到 Buffer 中
scala> b
res7: Array[Int] = Array(1, 2, 3, 4)
scala> var c=new scala.collection.mutable.ArrayBuffer[Int]
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> b.copyToBuffer(c)
scala> c
res15: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
数组反转
reverse
reverseIterator
reverseMap
reverse
定义:def reverse: Array[T]
描述:反转序列
reverseIterator
定义:def reverseIterator: collection.Iterator[T]
描述:生成反向迭代器
reverseMap
定义:def reverseMap[B](f: (A) ⇒ B): Array[B]
描述:同 map,方向相反
scala> var a=Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.reverse
res17: Array[Int] = Array(5, 4, 3, 2, 1)
scala> a.reverseIterator.toArray
res18: Array[Int] = Array(5, 4, 3, 2, 1)
scala> a.reverseMap(_+2)
res19: Array[Int] = Array(7, 6, 5, 4, 3)
数组行列操作
flatten
定义:def flatten[U](implicit asTrav: (T) ⇒ collection.Traversable[U], m: ClassTag[U]): Array[U]
描述:扁平化(降维),将二维数组的所有元素组合在一起,形成一个一维数组返回
scala> var arr=Array(Array(1,2,3),Array(3,4,5),Array(6,7))
arr: Array[Array[Int]] = Array(Array(1, 2, 3), Array(3, 4, 5), Array(6, 7))
scala> arr.flatten
res20: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 7)
flatMap
定义:def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Array[B]
描述:先map后降维,对当前序列的每个元素进行操作,map之后的每个元素必须还是集合,结果放入新序列返回,参数要求是 GenTraversableOnce 及其子类
scala> arr
res21: Array[Array[Int]] = Array(Array(1, 2, 3), Array(3, 4, 5), Array(6, 7))
scala> arr.flatMap(_.map(_+1))
res22: Array[Int] = Array(2, 3, 4, 4, 5, 6, 7, 8)
//相当于下面的操作
scala> arr.map(_.map(_+1))
res25: Array[Array[Int]] = Array(Array(2, 3, 4), Array(4, 5, 6), Array(7, 8))
scala> arr.map(_.map(_+1)).flatten
res26: Array[Int] = Array(2, 3, 4, 4, 5, 6, 7, 8)
transpose
定义:def transpose[U](implicit asArray: (T) ⇒ Array[U]): Array[Array[U]]
描述:矩阵转置,二维数组行列转换
scala> arr
res27: Array[Array[Int]] = Array(Array(1, 2, 3), Array(3, 4, 5), Array(6, 7))
scala> arr.transpose
res28: Array[Array[Int]] = Array(Array(1, 3, 6), Array(2, 4, 7), Array(3, 5))
unzip
定义:def unzip[T1, T2](implicit asPair: (T) ⇒ (T1, T2), ct1: ClassTag[T1], ct2: ClassTag[T2]): (Array[T1], Array[T2])
描述:将数组元素均为二元元组的数组,每个元组的第一个元素组成一个数组,第二个元素组成一个数组,返回包含这两个数组的元组
scala> var a=Array((1,2),(3,4),(5,6))
a: Array[(Int, Int)] = Array((1,2), (3,4), (5,6))
scala> a.unzip
res32: (Array[Int], Array[Int]) = (Array(1, 3, 5),Array(2, 4, 6))
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> var a=Array((1,2,3),(4,5,6),(7,8,9),(3,4,5))
a: Array[(Int, Int, Int)] = Array((1,2,3), (4,5,6), (7,8,9), (3,4,5))
scala> a.unzip3
res34: (Array[Int], Array[Int], Array[Int]) = (Array(1, 4, 7, 3),Array(2, 5, 8, 4),Array(3, 6, 9, 5))
zip
定义:def zip[B](that: GenIterable[B]): Array[(A, B)]
描述:将两个序列对应位置上的元素组成一个元组数组,要求两个序列长度相同
scala> a
res39: Array[Int] = Array(1, 2, 3)
scala> b
res40: List[Char] = List(a, b, c)
scala> a.zip(b)
res41: Array[(Int, Char)] = Array((1,a), (2,b), (3,c))
zipAll
zipAll
定义:def zipAll[B](that: collection.Iterable[B], thisElem: A, thatElem: B): Array[(A, B)]
描述:同 zip ,但是允许两个序列长度不同,不足的自动填充,如果当前序列短,不足的元素填充为 thisElem,如果 that 序列短,填充为 thatElem
//后面的不足
scala> a
res43: Array[Int] = Array(1, 2, 3)
scala> b
res44: List[Char] = List(a, b)
scala> a.zipAll(b,1,'p')
res45: Array[(Int, Char)] = Array((1,a), (2,b), (3,p))
//前面的不足
scala> var c=Array(1,2)
c: Array[Int] = Array(1, 2)
scala> var d=Array('a','b','c')
d: Array[Char] = Array(a, b, c)
scala> c.zipAll(d,10,'p')
res47: Array[(Int, Char)] = Array((1,a), (2,b), (10,c))
zipWithIndex
定义:def zipWithIndex: Array[(A, Int)]
描述:序列中的每个元素和它的索引组成一个元组数组
scala> a
res48: Array[Int] = Array(1, 2, 3)
scala> a.zipWithIndex
res49: Array[(Int, Int)] = Array((1,0), (2,1), (3,2))
数组的类型转换
deep
集合类型不变,将元素类型全部转为any
scala> a
res79: Array[Int] = Array(1, 2, 3)
scala> a.deep
res80: IndexedSeq[Any] = Array(1, 2, 3)
toArray array
toBuffer
toIndexedSeq
toIterator iterator
toList
toSeq
toStream
toSet
附带去重功能
scala> c
res191: Array[Int] = Array(1, 2, 2, 3)
scala> c.toSet
res192: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
toVector to
将集合转化为Vector类型,有一些差异
scala> a
res162: Array[Int] = Array(1, 2, 3)
scala> a.toVector
res163: Vector[Int] = Vector(1, 2, 3)
scala> a.to
res164: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3)
toTraversable toIterable
把集合或迭代器转化为可重复遍历的集合
scala> a.iterator
res145: Iterator[Int] = <iterator>
scala> res145.foreach(println)
1
2
3
scala> res145.foreach(println)
//没了,只能遍历一次
scala> a.iterator.toTraversable
res146: Traversable[Int] = Stream(1, ?)
scala> res146.foreach(println)
1
2
3
scala> res146.foreach(println)
1
2
3
scala> a.toIterable
res168: Iterable[Int] = WrappedArray(1, 2, 3)
scala> res168.foreach(println)
1
2
3
scala> res168.foreach(println)
1
2
3
toMap
定义:def toMap[T, U]: Map[T, U]
描述:将序列转转换成 Map 类型,需要被转化序列中包含的元素是 Tuple2 类型,Tuple2 类型意思是:元组必须是2元的,以元组的第一个元素做键,第二个元素做值
scala> var a=Array((1,2),(3,4),(5,6))
a: Array[(Int, Int)] = Array((1,2), (3,4), (5,6))
scala> a.toMap
res52: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4, 5 -> 6)
想把一个一维数组转为map,则须先将其转为所需形式
scala> var a=Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.map((_,1))
res53: Array[(Int, Int)] = Array((1,1), (2,1), (3,1), (4,1), (5,1))
scala> a.map((_,1)).toMap
res54: scala.collection.immutable.Map[Int,Int] = Map(5 -> 1, 1 -> 1, 2 -> 1, 3 -> 1, 4 -> 1)