列表
- Array[T]
- ++
- ++:
- +:
- :+
- /:
- :\
- addString
- aggregate
- apply
- applyOrElse
- canEqual
- charAt
- clone
- collect
- collectFirst
- combinations
- contains
- containsSlice
- copyToArray
- copyToBuffer
- corresponds
- count
- diff
- distinct
- drop
- dropRight
- dropWhile
- endsWith
- exists
- filter
- filterNot
- find
- flatMap
- flatten
- fold
- foldLeft
- foldRight
- forall
- foreach
- groupBy
- grouped
- hasDefiniteSize
- head
- headOption
- indexOf
- indexOfSlice
- indexWhere
- indices
- init
- inits
- intersect
- isDefinedAt
- isEmpty
- isTraversableAgain
- iterator
- last
- lastIndexOf
- lastIndexOfSlice
- lastIndexWhere
- lastOption
- length
- lengthCompare
- map
- max
- min
- maxBy
- minBy
- mkString
- nonEmpty
- padTo
- par
- partition
- patch
- permutations
- prefixLength
- product
- reduce
- reduceLeft
- reduceLeftOption
- reduceOption
- reduceRight
- reduceRightOption
- reverse
- reverseIterator
- reverseMap
- sameElements
- scan
- scanLeft
- scanRight
- segmentLength
- seq
- size
- slice
- sliding
- sortBy
- sortWith
- sorted
- span
- splitAt
- startsWith
- stringPrefix
- sum
- tail
- tails
- take
- takeRight
- takeWhile
- toArray
- toBuffer
- toIndexedSeq
- toIterable
- toIterator
- toList
- toMap
- toSeq
- toSet
- toStream
- toVector
- transpose
- union
- unzip
- unzip3
- update
- updated
- view
- withFilter
- zip
- zipAll
- zipWithIndex
Array[T]
T为元素对象,定义一个数组:val a = Array(1,2,3)
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> a(0)//取出下标0的对应元素
res0: Int = 1
++
合并数组,返回新的数组:a ++ b
scala> val a = Array(1,2,3)
scala> val b = Array(4,5,6)
scala> a++b
res1: Array[Int] = Array(1, 2, 3, 4, 5, 6)
++:
合并数组/集合,右边集合类型决定了返回的类型:a ++: b
scala> a
res3: Array[Int] = Array(1, 2, 3)
scala> val b=List(4,5,6)
scala> b
res4: List[Int] = List(4, 5, 6)
scala> a++:b//b是List类型,所以返回的是List类型
res5: List[Int] = List(1, 2, 3, 4, 5, 6)
+:
在数组前面添加一个元素,并返回新的数组对象:T+:a
scala> a
res6: Array[Int] = Array(1, 2, 3)
scala> 3+:a
res7: Array[Int] = Array(3, 1, 2, 3)
:+
在数组后面面添加一个元素,并返回新的数组对象:a:+T
scala> a
res8: Array[Int] = Array(1, 2, 3)
scala> a:+0
res9: Array[Int] = Array(1, 2, 3, 0)
/:
对数组中所有的元素从左向右遍历,进行相同的迭代操作,与foldLeft,reduceLeft一起理解:(T/:a)(_运算符_)
scala> a
res13: Array[Int] = Array(1, 2, 3)
scala> (1/:a)(_+_)//累加:((1+1)+2)+3
res14: Int = 7
:\
对数组中所有的元素从右向左遍历,进行相同的迭代操作,与foldRight,reduceRight一起理解:(a:\T)(_运算符_)
scala> a
res15: Array[Int] = Array(1, 2, 3)
scala> (a:\5)(_-_)//递减:1-(2-(3-5))
res16: Int = -3
addString
将数组中的元素添加到StringBuilder中:a.addString(b[StringBuilder])
scala> val a = Array(1,2,3)
scala> val b = new StringBuilder()
b: StringBuilder =
scala> a.addString(b)
res23: StringBuilder = 123
将数组中的元素添加到StringBuilder中,用sep隔开,a.addString(b[StringBuilder],sep)
scala> val a = Array(1,2,3)
scala> val b = new StringBuilder()
b: StringBuilder =
scala> a.addString(b,"#")
res25: StringBuilder = 1#2#3
aggregate
聚合计算,aggregate 是柯里化方法,参数是两个方法(不分区时,把初始值顺序和每个元素相加,把得到的结果与下一个元素进行运算;分区时,在以上步骤,多一个combine过程):a.aggregate(初始值)(运算规则,combine运算规则)
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
*/
}
val b = a.aggregate(5)(_+_,_+_) // 不分区
val c = a.par.aggregate(5)(_+_,_+_) // 分区
apply
取出指定下标对应的元素:a.apply(index)
scala> a
res29: Array[Int] = Array(1, 2, 3)
scala> a.apply(0)
res30: Int = 1
applyOrElse
接收2个参数,第一个是调用的参数,第二个是个回调函数。如果第一个调用的参数匹配,返回匹配的值,否则调用回调函数。
scala> a
res54: Array[Int] = Array(1, 2, 3)
scala> a.applyOrElse(1,{x:Int=>"two"})//下标1存在元素2,返回2
res55: Any = 2
scala> a.applyOrElse(6,{x:Int=>"two"})//下标6不存在,返回two
res56: Any = two
canEqual
判断两个对象是否可以进行比较:a.canEqual(b)
不知道有什么用
scala> a
res37: Array[Int] = Array(1, 2, 3)
scala> b
res38: Array[String] = Array(f)
scala> a.canEqual(b)
res39: Boolean = true
charAt
取出指定下标对应的元素,类似于apply,但是针对char类型:b.charAt(index)
scala> val b=Array('f','a','b')
b: Array[Char] = Array(f, a, b)
scala> b.charAt(2)
res41: Char = b
scala> val c="String"
c: String = String
scala> c.charAt(2)//c是字符串,但是由字符组成
res42: Char = r
clone
克隆当前数组,返回一个新数组:a.clone()
scala> a
res43: Array[Int] = Array(1, 2, 3)
scala> a.clone()
res44: Array[Int] = Array(1, 2, 3)
collect
执行一个并行计算(偏函数),得到一个新的数组对象(通过偏函数对某些元素进行操作):a.collect(fun)
scala> var fun:PartialFunction[Int,Int]={
| case x if x<=2 => x*2
| case x=> x
| }//偏函数作用:如果当前元素小于2,当前元素*2;否则返回当前元素
fun: PartialFunction[Int,Int] = <function1>
scala> a.collect(fun)
res58: Array[Int] = Array(2, 4, 3)
collectFirst
找到第一个符合偏函数定义的元素,并计算,最后返回这个元素的Option对象,可用get方法取值:a.collectFirst(fun)
scala> a.collectFirst(fun)//依然是上一个偏函数(如果当前元素小于2,当前元素*2;否则返回当前元素)
res59: Option[Int] = Some(2)//1是第一个满足条件的元素,返回1*2
scala> a.collectFirst(fun).get
res60: Int = 2
combinations
根据指定组合个数,排列组合(不考虑顺序)返回一个迭代器,需要遍历查看:a.combinations(Int)
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.combinations(2)//指定2个一组
res64: Iterator[Array[Int]] = non-empty iterator
scala> a.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
contains
判断集合中是否包含指定元素,返回Boolean:a.contains(T)
scala> a
res68: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.contains(3)
res69: Boolean = true
scala> a.contains(9)
res71: Boolean = false
containsSlice
判断集合中是否包含对应的集合(值的对比),返回Boolean:a.containsSlice(b)
scala> a
res73: Array[Int] = Array(1, 2, 3, 4, 5)
scala> b
res74: Array[Int] = Array(3, 4)
scala> a.containsSlice(b)
res75: Boolean = true
copyToArray
复制当前数组复制到另一个数组中(注意类型一致),无返回值:a.copyToArray(new c)
scala> var c=new Array[Int](5)
c: Array[Int] = Array(0, 0, 0, 0, 0)
scala> a.copyToArray(c)
scala> c
res80: Array[Int] = Array(1, 2, 3, 4, 5)
copyToBuffer
将数组中的元素复制到Buffer中(注意类型一致)(需要导包:import scala.collection.mutable.ArrayBuffer
):a.copyToBuffer(new [ArrayBuffer]b)
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> val b=new ArrayBuffer[Int]()
b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> a.copyToBuffer(b)
scala> b
res86: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
corresponds
判断两个数组长度是否相等以及对应位置的元素是否符合某个条件,如果都满足,则返回true(类似sameElements):a.corresponds(b)(_指定条件_)
scala> a
res90: Array[Int] = Array(1, 2, 3, 4, 5)
scala> b
res91: Array[Int] = Array(2, 3, 4, 5, 6)
scala> a.corresponds(b)(_<_)
res92: Boolean = true
scala> a.corresponds(b)(_>_)
res93: Boolean = false
count
统计符合条件的元素个数,返回Int:a.count(条件)
scala> a
res94: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.count(_>3)
res95: Int = 2
diff
求集合的差集,返回当前数组没有出现在另一个数组中的元素组成的数组(与++,intersect联合记忆):a.diff(b)
scala> a
res96: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b=Array(4,3)
b: Array[Int] = Array(4, 3)
scala> a.diff(b)
res97: Array[Int] = Array(1, 2, 5)
distinct
当前集合中的元素去重,返回新数组:a.distinct
scala> var a=Array(1,1,2,2,3,4,4,4,5)
a: Array[Int] = Array(1, 1, 2, 2, 3, 4, 4, 4, 5)
scala> a.distinct
res98: Array[Int] = Array(1, 2, 3, 4, 5)
drop
将当前数组中的前n个元素去除,返回一个新数组(与head、init、last、tail、take联合记忆):a.drop(Int)
scala> val a=Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> a.drop(2)
res101: Array[Int] = Array(3, 4)
dropRight
将当前数组中的后n个元素去除,返回一个新数组:a.dropRight(Int)
scala> a
res102: Array[Int] = Array(1, 2, 3, 4)
scala> a.dropRight(2)
res103: Array[Int] = Array(1, 2)
dropWhile
去除当前数组中符合条件的元素,返回剩余的数组,要求从第一个元素起就要满足条件,直到遇到第一个不满足条件的元素为止,如果第一个元素就不满足,则返回整个数组:a.dropWhile(条件)
scala> a
res109: Array[Int] = Array(1, 2, 3, 4)
scala> a.dropWhile(_<=2)//3不满足条件,返回剩余元素
res110: Array[Int] = Array(3, 4)
scala> a.dropWhile(_==2)//1不满足条件,返回剩余元素
res111: Array[Int] = Array(1, 2, 3, 4)
endsWith
判断当前序列是否以某个序列结尾,返回Boolean(与startsWith联合记忆):a.endsWith(b)
scala> a
res114: Array[Int] = Array(1, 2, 3, 4)
scala> b
res115: Array[Int] = Array(3, 4)
scala> a.endsWith(b)
res116: Boolean = true
exists
当前数组是否包含符合条件的元素,返回Boolean:a.exists(条件)
scala> a
res117: Array[Int] = Array(1, 2, 3, 4)
scala> a.exists(_>2)
res118: Boolean = true
scala> a.exists(_>9)
res119: Boolean = false
filter
过滤(每个元素都与条件比较,与forall联合记忆),取出当前数组中符合条件的元素,返回新的数组:a.filter(条件)
scala> a
res122: Array[Int] = Array(1, 2, 3, 4)
scala> a.filter(_<4)
res123: Array[Int] = Array(1, 2, 3)
filterNot
过滤(每个元素都与条件比较),取出当前数组中不符合条件的元素,返回新的数组:a.filterNot(条件)
scala> a
res124: Array[Int] = Array(1, 2, 3, 4)
scala> a.filterNot(_<4)
res125: Array[Int] = Array(4)
find
查找第一个符合条件的元素,返回Option(用get取出some中的元素):a.find(条件)
scala> a
res126: Array[Int] = Array(1, 2, 3, 4)
scala> a.find(_<=2)
res127: Option[Int] = Some(1)
scala> a.find(_<=2).get
res128: Int = 1
flatMap
对当前序列的每个元素进行操作,返回一个新序列(理解成先map再flatten):a.flatMap(指定操作)
scala> a
res133: Array[Int] = Array(1, 2, 3, 4)
scala> a.flatMap(x=>1 to x)
//1
//1,2
//1,2,3
//1,2,3,4再进行flatten
res134: Array[Int] = Array(1, 1, 2, 1, 2, 3, 1, 2, 3, 4)
flatten
扁平化,将二维数组的所有元素组合在一起,返回一个一维数组:a.flatten
scala> var a=Array(Array('a','b'),Array('n','m'))
a: Array[Array[Char]] = Array(Array(a, b), Array(n, m))
scala> a.flatten
res4: Array[Char] = Array(a, b, n, m)
fold
对序列中的每个元素进行二元运算:a.fold(Int)(_运算符_)
scala> var a=Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> a.fold(3)(_+_)//3+1+2+3
res8: Int = 9
foldLeft
同/:
scala> a
res9: Array[Int] = Array(1, 2, 3)
scala> a.foldLeft(1)(_+_)//1+1+2+3
res10: Int = 7
foldRight
同:\
scala> a.foldRight(1)(_-_)//1-(2-(3-1))
res11: Int = 1
forall
判断序列中的每个元素是否都满足条件,如果满足,返回true:a.forall(条件)
scala> a
res13: Array[Int] = Array(1, 2, 3)
scala> a.forall(_>=0)
res14: Boolean = true
scala> a.forall(_<0)
res15: Boolean = false
foreach
遍历序列中的元素,进行指定操作,无返回值,一般与println连用:a.foreach(print)
scala> a
res22: Array[Int] = Array(1, 2, 3)
scala> a.foreach(print)
123
groupBy
按条件分组,返回值是Map类型,每个key对应一个数组:a.groupBy(条件)
scala> var a=Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.groupBy(_>3)//大于3的是true一组,小于等于3的是false一组
res24: scala.collection.immutable.Map[Boolean,Array[Int]] = Map(false -> Array(1, 2, 3), true -> Array(4, 5))
//分词统计
scala> var words=Array("hello world","hello scala","scala is bast")
words: Array[String] = Array(hello world, hello scala, scala is bast)
scala> words.flatMap(_.split(" ")).groupBy(x=>x).foreach(f=>println(f._1,f._2.length))
(is,1)
(world,1)
(bast,1)
(scala,2)
(hello,2)
grouped
按指定数量分组,可指定每组元素个数,返回一个迭代器:a.grouped(指定每组元素数量)
scala> a
res31: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.grouped(3)
res32: Iterator[Array[Int]] = non-empty iterator//返回一个迭代器
scala> a.grouped(3).foreach(x=>println(x.mkString(",")))
1,2,3
4,5
hasDefiniteSize
检测序列是否存在有限长度,有限返回true,stream流返回false:a.hasDefiniteSize
scala> a
res36: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.hasDefiniteSize
res37: Boolean = true
scala> a.toStream.hasDefiniteSize
res38: Boolean = false
head
返回序列的第一个元素(与last联系),如果序列是空,报错:a.head
scala> a
res39: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.head
res40: Int = 1
scala> val b=Array[Int]()
b: Array[Int] = Array()
scala> b.head
java.util.NoSuchElementException: next on empty iterator
...
headOption
返回序列的第一个元素的Option对象(与lastOption联系),如果序列是空,返回None:a.headOption
scala> a.headOption
res41: Option[Int] = Some(1)
scala> a.headOption.get
res42: Int = 1
scala> val b=Array[Int]()
b: Array[Int] = Array()
scala> b.headOption
res43: Option[Int] = None
indexOf
返回指定元素在序列中第一次出现的下标(与lastIndexOf联系)(如果没有,返回-1):a.indexOf(T)
scala> a
res48: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.indexOf(3)
res49: Int = 2
scala> a.indexOf(7)
res50: Int = -1
indexOfSlice
检测当前序列中是否包含指定序列(与lastIndexOfSlice联系),有的话,返回序列第一次出现的下标(如果没有,返回-1):a.indexOfSlice(b)
scala> a
res54: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.indexOfSlice(Array(3,4))
res55: Int = 2
scala> a.indexOfSlice(Array(1,3,4))
res56: Int = -1
indexWhere
返回当前序列第一个满足指定条件的元素的下标(与lastIndexWhere联系):a.indexWhere(条件)
scala> a
res57: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.indexWhere(_>3)
res58: Int = 3
indices
返回数组/集合的下标的集合(特殊集合Range):a.indices
scala> a
res59: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.indices
res60: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4)
init
返回一个不包含最后一个元素的序列(去尾):a.init
scala> a
res62: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.init
res63: Array[Int] = Array(1, 2, 3, 4)
inits
inits的循环操作,直到最后的值为空,返回一个迭代器:a.init
scala> a
res72: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.inits
res73: Iterator[Array[Int]] = non-empty iterator
scala> a.inits.foreach(x=>println(x.mkString(",")))
1,2,3,4,5
1,2,3,4
1,2,3
1,2
1
intersect
取两个集合的交集:a.intersect(b)
scala> var a=Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> var b=Array(1,4,5,6)
b: Array[Int] = Array(1, 4, 5, 6)
scala> a.intersect(b)
res9: Array[Int] = Array(1, 4)
isDefinedAt
判断序列中是否存在指定的索引,返回Boolean:a.isDefinedAt(Int)
scala> a
res10: Array[Int] = Array(1, 2, 3, 4)
scala> a.isDefinedAt(1)
res11: Boolean = true
scala> a.isDefinedAt(5)
res12: Boolean = false
isEmpty
判断序列是否为空(对应nonEmpty判断序列是否不为空)返回Boolean:a.isEmpty
scala> a
res13: Array[Int] = Array(1, 2, 3, 4)
scala> a.isEmpty
res14: Boolean = false
isTraversableAgain
判断序列是否可以反复遍历,返回Boolean,iterator返回false: a.isTraversableAgain
scala> a.isTraversableAgain
res15: Boolean = true
scala> a.grouped(3).isTraversableAgain//a.grouped(3)返回的是迭代器,只能遍历一次
res16: Boolean = false
iterator
生成当前序列的迭代器:a.iterator
scala> a
res28: Array[Int] = Array(1, 2, 3, 4)
scala> a.iterator
res29: Iterator[Int] = non-empty iterator
scala> a.iterator.foreach(x=>println(x))
1
2
3
4
last
返回序列的最后一个元素,如果序列为空,报错:a.last
scala> a
res31: Array[Int] = Array(1, 2, 3, 4)
scala> a.last
res32: Int = 4
lastIndexOf
返回指定元素在序列中最后一次出现的下标:a.lastIndexOf(Int)
scala> a
res35: Array[Int] = Array(1, 2, 3, 4)
scala> a.lastIndexOf(4)
res36: Int = 3
lastIndexOfSlice
序列中是否包含指定序列,并返回指定序列最后元素在当前序列中最后一次出现的下标:a.lastIndexOfSlice(b)
scala> a
res40: Array[Int] = Array(1, 2, 3, 4)
scala> a.lastIndexOfSlice(Array(3,4))
res41: Int = 2
lastIndexWhere
返回当前序列中最后一个满足指定条件的元素的下标:a.lastIndexWhere(条件)
scala> a
res45: Array[Int] = Array(1, 2, 3, 4)
scala> a.lastIndexWhere(_>3)
res46: Int = 3//4的下标
lastOption
返回当前序列最后的一个元素的Option,如果序列为空,返回None: a.lastOption
scala> a
res47: Array[Int] = Array(1, 2, 3, 4)
scala> a.lastOption
res48: Option[Int] = Some(4)
scala> a.lastOption.get
res49: Int = 4
length
返回数组/集合元素的个数(同size):a.length
scala> a
res50: Array[Int] = Array(1, 2, 3, 4)
scala> a.length
res51: Int = 4
lengthCompare
比较当前序列和指定参数Int的值,返回差值(直接理解为length-Int):a.lengthCompare(Int)
scala> a
res52: Array[Int] = Array(1, 2, 3, 4)
scala> a.lengthCompare(5)
res53: Int = -1 //4-5=-1
map
对数组里的每个元素进行指定操作,返回一个新数组 :a.map(指定操作)
scala> a
res54: Array[Int] = Array(1, 2, 3, 4)
scala> a.map(x=>x+2)
res55: Array[Int] = Array(3, 4, 5, 6)
max
返回当前序列中最大的元素:a.max
scala> a
res56: Array[Int] = Array(1, 2, 3, 4)
scala> a.max
res57: Int = 4
min
返回当前序列的最小元素:a.min
scala> a
res58: Array[Int] = Array(1, 2, 3, 4)
scala> a.min
res59: Int = 1
maxBy
返回当前序列中符合指定条件的第一个元素(类似indexWhere,但是indexWhere返回下标)(同find.get):a.maxBy(条件)
scala> a
res61: Array[Int] = Array(1, 2, 3, 4)
scala> a.maxBy(_>2)
res62: Int = 3
scala> a.find(_>2).get
res63: Int = 3
scala> a.indexWhere(_>2)
res64: Int = 2//2是3的下标
minBy
返回当前序列不符合指定条件的第一个元素:a.minBy(条件)
scala> a
res65: Array[Int] = Array(1, 2, 3, 4)
scala> a.minBy(_>3)//1是第一个不满足>3这个条件的元素
res66: Int = 1
mkString
字符串拼接(可有分隔符,可加前后尾):a.mkString("头","分隔符","尾")
scala> a
res68: Array[Int] = Array(1, 2, 3, 4)
scala> a.mkString("#")
res69: String = 1#2#3#4
scala> a.mkString("{","#","}")
res70: String = {1#2#3#4}
nonEmpty
判断当前序列是否不为空(isEmpty),返回Boolean:a.nonEmpty
scala> a
res71: Array[Int] = Array(1, 2, 3, 4)
scala> a.nonEmpty
res72: Boolean = true
padTo
后填充序列,返回一个新序列 :a.padTo(定义长度,填充内容)
scala> a
res80: Array[Int] = Array(1, 2, 3, 4)
scala> a.padTo(9,0)//用0把a填充为一个长度为9的新序列
res81: Array[Int] = Array(1, 2, 3, 4, 0, 0, 0, 0, 0)
par
返回一个并行实现,产生的并行序列不能被改变,返回ParArray:a.par
scala> a.par
res82: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3, 4)
partition
按条件拆分成两个数组,满足指定条件的放一个,其他的放另一个,返回一个二维数组:a.partition(条件)
scala> a
res83: Array[Int] = Array(1, 2, 3, 4)
scala> a.partition(_>=3)
res84: (Array[Int], Array[Int]) = (Array(3, 4),Array(1, 2))
patch
批量替换,返回新数组(从当前序列的第n个元素开始,后面的m个元素,被序列批量替换):a.patch(n,b,m)
scala> a
res90: Array[Int] = Array(1, 2, 3, 4)
scala> b
res91: Array[String] = Array(a, b)
scala> a.patch(1,b,2)//将下标1和2之间的内容用数组b替换
res92: Array[Any] = Array(1, a, b, 4)
permutations
排列组合(考虑顺序且无法指定组合,而combinations不考虑排序),返回一个迭代器:a.permutations
scala> var a =Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> a.permutations
res97: Iterator[Array[Int]] = non-empty iterator
scala> a.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
返回满足条件的前置序列的长度:a.prefixLength(条件)
scala> var a = Array(1,2,3,4,5,6)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.prefixLength(_<=3)//小于3的是1,2,3 长度为3
res100: Int = 3
product
返回当前序列所有元素的乘积(阶乘):a.product
scala> a
res101: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.product
res102: Int = 720//1*2*3*4*5*6
reduce
类似flod,但是不需要初始值,返回Int:a.reduce(_运算符_)
scala> a
res103: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.reduce(_-_)//1-2-3-4-5-6=-19
res104: Int = -19
reduceLeft
类似于foldLeft,类似于/:
(左操作),返回Int:a.reduceLeft(_运算符_)
scala> var a=Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> a.reduceLeft(_-_)//1-2-3=-4
res105: Int = -4
reduceLeftOption
类似reduceLeft 返回Option:a.reduceLeftOption(_运算符_)
scala> a
res106: Array[Int] = Array(1, 2, 3)
scala> a.reduceLeftOption(_-_)
res107: Option[Int] = Some(-4)
reduceOption
同reduceLeftOption,返回Option:a.reduce(_运算符_)
scala> a
res107: Array[Int] = Array(1, 2, 3)
scala> a.reduceOption(_-_)
res109: Option[Int] = Some(-4)
reduceRight
类似于foldRight,类似于:\
(右操作),返回Int:a.reduceRight(_运算符_)
scala> a
res111: Array[Int] = Array(1, 2, 3)
scala> a.reduceRight(_-_)//1-(2-3)=2
res112: Int = 2
reduceRightOption
类似reduceRight 返回Option:a.reduceRightOption(_运算符_)
scala> a
res113: Array[Int] = Array(1, 2, 3)
scala> a.reduceRightOption(_-_)
res114: Option[Int] = Some(2)
scala> a.reduceRightOption(_-_).get
res115: Int = 2
reverse
序列反转:a.reverse
scala> a
res116: Array[Int] = Array(1, 2, 3)
scala> a.reverse
res117: Array[Int] = Array(3, 2, 1)
scala> str
res119: String = 12345
scala> str.reverse
res120: String = 54321
reverseIterator
序列反转的迭代器:a.reverseIterator
scala> a
res128: Array[Int] = Array(1, 2, 3)
scala> a.reverseIterator.foreach(print)
321
reverseMap
同map方法,进行map,然后reverse反转map后的序列:a.reverseMap(map操作)
scala> a
res130: Array[Int] = Array(1, 2, 3)
scala> a.reverseMap(_*3)
res131: Array[Int] = Array(9, 6, 3)
sameElements
判断两个序列的顺序和对应位置上的元素是否都一样(类似corresponds):a.sameElements(b)
scala> a
res132: Array[Int] = Array(1, 2, 3)
scala> b
res133: Array[Int] = Array(1, 2, 3)
scala> a.sameElements(b)
res134: Boolean = true
scan
同fold方法,但是scan会把每一步计算的结果返回成一个新的集合:a.scan(初值)(_运算符_)
scala> a
res135: Array[Int] = Array(1, 2, 3)
scala> a.scan(5)(_+_)//5,5+1=6,6+2=8,8+3=11
res136: Array[Int] = Array(5, 6, 8, 11)
scanLeft
同foldLeft,把每一步计算的结果返回成一个新的集合:a.scanLeft(初值)(_运算符_)
scala> a
res137: Array[Int] = Array(1, 2, 3)
scala> a.scanLeft(2)(_-_)//2,2-1=1,1-2=-1,-1-3=-4
res138: Array[Int] = Array(2, 1, -1, -4)
scanRight
同foldRight,把每一步计算的结果返回成一个新的集合:a.scanRight(初值)(_运算符_)
scala> a
res140: Array[Int] = Array(1, 2, 3)
scala> a.scanRight(7)(_-_)// 1-6=-5, 2-(-4)=6, 3-7=-4, 7
res141: Array[Int] = Array(-5, 6, -4, 7)
segmentLength
在序列中按照指定条件和一个给定开始查找的下标,返回满足条件的连续序列的长度:a.segmentLength(条件,开始下标)
scala> var a=Array(1,2,3,4,1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4, 1, 2, 3, 4)
scala> a.segmentLength(_>1,1)//从下标1开始,满足大于1的连续元素为2,3,4,长度为3
res146: Int = 3
seq
返回一个WrappedArray特殊数组,且是mutable可修改类型:a.seq
scala> var a=Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> a.seq
res148: scala.collection.mutable.IndexedSeq[Int] = WrappedArray(1, 2, 3)
size
返回数组/集合的大小(个数)同length: a.size
scala> a
res149: Array[Int] = Array(1, 2, 3)
scala> a.size
res150: Int = 3
slice
返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素:a.slice(from ,until )
scala> var a=Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.slice(1,3)
res151: Array[Int] = Array(2, 3)
sliding
简易理解窗口滑动,设置窗口大小和步长(步长默认为1),当剩余元素个数不够结束:a.sliding(窗口大小,步长)
scala> a
res156: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.sliding(3,2)//窗口大小为3,每次移动2:123,345
res157: Iterator[Array[Int]] = non-empty iterator
scala> a.sliding(3,2).foreach(x=>println(x.mkString(",")))
1,2,3
3,4,5
sortBy
按指定的排序规则对序列进行排序(x=>x)升序,(x=> -x)降序:a.sortBy(排序规则)
scala> var a=Array(3,4,1,7,2)
a: Array[Int] = Array(3, 4, 1, 7, 2)
scala> a.sortBy(x=>x)
res160: Array[Int] = Array(1, 2, 3, 4, 7)
sortWith
自定义排序方法:a.sortWith(自定义排序)
scala> var a=Array(2,1,5,9,4,6)
a: Array[Int] = Array(2, 1, 5, 9, 4, 6)
scala> a.sortWith(_>_)
res1: Array[Int] = Array(9, 6, 5, 4, 2, 1)
sorted
默认升序排列:a.sorted
scala> a
res4: Array[Int] = Array(2, 1, 5, 9, 4, 6)
scala> a.sorted
res5: Array[Int] = Array(1, 2, 4, 5, 6, 9)
span
将序列按指定条件拆分成两个数组,直到第一个不满足条件的元素位置,前面已经满足条件的放入一个数组,其他的放入另一个(类似partition,但是span遇到第一个不满足条件的元素会结束之后的判断):
scala> a.sorted
res5: Array[Int] = Array(1, 2, 4, 5, 6, 9)
scala> a.span(_>3)//1不满足条件,直接返回
res6: (Array[Int], Array[Int]) = (Array(),Array(2, 1, 5, 9, 4, 6))
scala> a.span(_<3)
res7: (Array[Int], Array[Int]) = (Array(2, 1),Array(5, 9, 4, 6))
splitAt
从指定的下标开始,把序列拆分成两个数组,且指定下标的数组分在后面,返回一个二维数组(类似span,partition):a.splitAt(Int)
scala> var a = Array(1,2,3,4,5,6)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.splitAt(4)
res0: (Array[Int], Array[Int]) = (Array(1, 2, 3, 4),Array(5, 6))
startsWith
判断序列是否以某个指定序列开始:a.startsWith(b)
scala> a
res2: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.startsWith(Array(1,2))
res3: Boolean = true
scala> a.startsWith(Array(1,3))
res4: Boolean = false
stringPrefix
返回toString的前缀,不知道有什么用:a.stringPrefix
scala> a
res6: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.toString
res7: String = [I@5a6f6cac
scala> a.stringPrefix
res8: String = [I
sum
求和:a.sum
scala> var a=Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> a.sum
res12: Int = 10
tail
返回不包含当前序列第一个元素的新序列(去头):a.tail
scala> a
res14: Array[Int] = Array(1, 2, 3, 4)
scala> a.tail
res15: Array[Int] = Array(2, 3, 4)
tails
每一步都进行tail方法,直到最后为空:a.tails
scala> a
res22: Array[Int] = Array(1, 2, 3, 4)
scala> a.tails
res23: Iterator[Array[Int]] = non-empty iterator
scala> a.tails.foreach(x=>println(x.mkString(",")))
1,2,3,4
2,3,4
3,4
4
take
返回当前序列前n个元素组成的新数组:a.take(Int)
scala> a
res25: Array[Int] = Array(1, 2, 3, 4)
scala> a.take(1)
res26: Array[Int] = Array(1)
scala> a.take(2)
res27: Array[Int] = Array(1, 2)
takeRight
返回当前序列,从右边开始,n个元素组成的序列:a.takeRight(Int)
scala> a
res29: Array[Int] = Array(1, 2, 3, 4)
scala> a.takeRight(2)
res30: Array[Int] = Array(3, 4)
takeWhile
返回当前序列中,从第一个元素开始,满足条件的连续元素组成的序列(如果第一个不满足就返回空):a.takeWhile(指定条件)
scala> a
res32: Array[Int] = Array(1, 2, 3, 4, 3, 4, 1, 4)
scala> a.takeWhile(_>3)
res33: Array[Int] = Array()
scala> a.takeWhile(_<3)
res34: Array[Int] = Array(1, 2)
toArray
将序列转换成Array类型
toBuffer
将序列转换成Buffer(ArrayBuffer需要导包:import scala.collection.mutable.ArrayBuffer)
toIndexedSeq
将序列转换成IndexedSep类型,返回Vector集合
toIterable
将序列转换成可迭代类型(接口)
toIterator
将序列转换成迭代器类型,同iterator方法(iterator只能遍历一遍)
toList
将序列转换成List类型(长度固定的集合(ArrayList))(不可修改的)
toMap
将序列转换成Map类型(immutable不可修改的)
toSeq
将序列转换成Seq类型
toSet
将序列转换成Set类型
toStream
将序列转换成Stream类型
toVector
将序列转换成Vector类型
transpose
矩阵转置,二维数组的行转列 :a.transpose
scala> a
res40: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4), Array(5, 6))
scala> a.foreach(x=>println(x.mkString(",")))
1,2
3,4
5,6
scala> a.transpose.foreach(x=>println(x.mkString(",")))
1,3,5
2,4,6
union
合并两个数组,同++:a.union(b)
scala> var a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> var b=Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)
scala> a.union(b)
res43: Array[Int] = Array(1, 2, 3, 4, 5, 6)
unzip
将一个有两个二元组的数组,每一个元数组的第一个元素组成一个数组,第二个元素组成一个数组返回一个新的元组:a.unzip
scala> var a=Array(("ls",23),("zs",19))
a: Array[(String, Int)] = Array((ls,23), (zs,19))
scala> a.unzip
res45: (Array[String], Array[Int]) = (Array(ls, zs),Array(23, 19))
unzip3
将一个有三个三元组的数组,每一个元数组的第一个元素组成一个数组,第二个元素组成一个数组返回一个新的元组,第三个元素组成一个数组返回一个新的元组:a.unzip3
scala> var a=Array(("ls",23,4000),("zs",19,3500),("czh",30,15000))
a: Array[(String, Int, Int)] = Array((ls,23,4000), (zs,19,3500), (czh,30,15000))
scala> a.unzip3
res46: (Array[String], Array[Int], Array[Int]) = (Array(ls, zs, czh),Array(23, 19, 30),Array(4000, 3500, 15000))
update
更改指定下标处的元素为指定元素(更改当前序列):a.update(下标,替换元素)
scala> var a=Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.update(0,9)
scala> a
res48: Array[Int] = Array(9, 2, 3, 4, 5)
updated
更改指定下标处的元素为指定元素(不更改当前序列,返回新序列):a.updated(下标,替换元素)
scala> a
res51: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.updated(0,9)
res52: Array[Int] = Array(9, 2, 3, 4, 5)
scala> a
res53: Array[Int] = Array(1, 2, 3, 4, 5)
view
返回指定区间内(下标之间)的元素,左闭右开(类似subSequence),返回IndexedSeqView:a.view(shart,end)
scala> a
res59: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.view(1,3)
res60: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewS(...)
scala> print(a.view(1,3).mkString(","))
2,3
withFilter
根据指定条件过滤元素(与map连用)(类似filter):a.withFilter(过滤条件)
scala> a
res62: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.withFilter(_>2)
res63: scala.collection.generic.FilterMonadic[Int,Array[Int]] = scala.collection.TraversableLike$WithFilter@ca04000
scala> a.withFilter(_>2).map(x=>x)
res64: Array[Int] = Array(3, 4, 5)
zip
将两个相同长度的序列对应位置上的元素组成一个元组数组:a.zip(b)
scala> a
res67: Array[Int] = Array(1, 2, 3, 4, 5)
scala> b
res68: Array[Char] = Array(a, b, c, d, e)
scala> a.zip(b)
res69: Array[(Int, Char)] = Array((1,a), (2,b), (3,c), (4,d), (5,e))
zipAll
同zip,但是不要求长度相等,但是需要给出补全元素,例如a.zipAll(b,n,m)
,如果a短,补足n;如果b短,补足m
scala> a
res70: Array[Int] = Array(1, 2, 3, 4, 5)
scala> b
res71: Array[Char] = Array(a, b)
scala> a.zipAll(b,2,"$")//b短,用$补全b
res72: Array[(Int, Any)] = Array((1,a), (2,b), (3,$), (4,$), (5,$))
scala> b.zipAll(a,2,"$")//b短,用2补全b
res73: Array[(AnyVal, Any)] = Array((a,1), (b,2), (2,3), (2,4), (2,5))
zipWithIndex
将序列中的每一个元素和他对应的下标,组成一个元组数组:a.zipWithIndex
scala> a
res76: Array[Char] = Array(a, b, c, d, e)
scala> a.zipWithIndex
res77: Array[(Char, Int)] = Array((a,0), (b,1), (c,2), (d,3), (e,4))