函数名
具体函数方法
slice
override def slice(from : scala.Int, until : scala.Int) : Repr = { /* compiled code */ }
返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素(左闭右开)
val a = Array(1, 2, 3, 4)
val b = a.slice(1, 3)
println(b.mkString(",")) // 2,3
sliding
def sliding(size : scala.Int) : scala.collection.Iterator[Repr] = { /* compiled code */ }
def sliding(size : scala.Int, step : scala.Int) : scala.collection.Iterator[Repr] = { /* compiled code */ }
滑动,从第一个元素开始,每个元素和它后面的 size - 1 个元素组成一个数组,最终组成一个新的集合返回,当剩余元素个数不够 size 时,则结束 。默认步长为1, (sliding(size, step) 可以设置步长 step,每一组元素组合完后,下一组从上一组起始元素位置 + step 后的位置处开始)
val a = Array(1, 2, 3, 4, 5)
val b = a.sliding(3).toList //每次滑3步,步长为1
for (i <- 0 to b.length - 1) {
val s = "第 %d 组: %s"
println(s.format(i + 1, b(i).mkString(",")))
}
/*
* 第 1 组: 1,2,3
* 第 2 组: 2,3,4
* 第 3 组: 3,4,5
*/
val c = a.sliding(3, 2).toList //每次滑3步,步长为2
for (i <- 0 to c.length - 1) {
val s = "第 %d 组: %s"
println(s.format(i + 1, c(i).mkString(",")))
}
/*
* 第 1 组: 1,2,3
* 第 2 组: 3,4,5
*/
sortBy
def sortBy[B](f : scala.Function1[A, B])(implicit ord : scala.math.Ordering[B]) : Repr = { /* compiled code */ }
按指定的排序规则对序列排序
val a = Array(3, 2, 1, 4)
val b = a.sortBy(x => x) // 按 x 从小到大,即对原序列升序排列
println("升序: " + b.mkString(",")) // 1,2,3,4
val c = a.sortBy(x => 0 - x) // 按 -x 从小到大,即对原序列降序排列
println("降序: " + c.mkString(",")) // 4,3,2,1
val d = Array('a','d','e','b')
val b = d.sortBy(x => x) // 按 x 从小到大,即对原序列升序排列
println("升序: " + b.mkString(",")) // a,b,d,e
sortWith
def sortWith(lt: (A, A) => Boolean): Repr = sorted(Ordering fromLessThan lt)
sortWith需要传入一个比较函数用来比较
def func(m:Int,n:Int):Boolean={
m>n
}
val a = Array(1, 2, 3, 3, 2, 3, 3, 4)
val b = a.sortWith(func)
b.foreach(println)//4, 3, 3, 3, 3, 2, 2, 1
sorted
def sorted[B >: A](implicit ord : scala.math.Ordering[B]) : Repr = { /* compiled code */ }
默认升序排列,无参数
val a = Array(3, 2, 1, 4)
val b = a.sorted
println(b.mkString(",")) // 1,2,3,4
span
override def span(p : scala.Function1[A, scala.Boolean]) : scala.Tuple2[Repr, Repr] = { /* compiled code */ }
将序列按条件拆分成两个数组,从第一个元素开始,直到第一个不满足条件的元素为止,满足条件的元素放到第一个数组,其余元素放到第二个数组
返回的是包含这两个数组的元祖
val a = Array(1, 2, 3, 4, 1, 2)
val b = a.span(x => x < 3)
println(b._1.mkString(",")) // 1,2
println(b._2.mkString(",")) // 3,4
splitAt
override def splitAt(n : scala.Int) : scala.Tuple2[Repr, Repr] = { /* compiled code */ }
从指定位置开始,把序列拆分成两个数组
val a = Array(5,6,7,8,9)
val b = a.splitAt(2)
println(b._1.mkString(",")) // 5,6
println(b._2.mkString(",")) // 7,8,9
startsWith
def startsWith[B](that : scala.collection.GenSeq[B]) : scala.Boolean = { /* compiled code */ }
def startsWith[B](that : scala.collection.GenSeq[B], offset : scala.Int) : scala.Boolean
判断序列是否以某个序列开始。
startsWith(that, offset) :判断序列从指定偏移处是否以某个序列开始)
val a = Array(1, 2, 3, 4)
val b = Array(1, 2)
println(a.startsWith(b)) // true
val c = Array(2, 3)
println(a.startsWith(c, 1)) // true
stringPrefix
def stringPrefix : scala.Predef.String = { /* compiled code */ }
返回 toString 结果的前缀,一般可以用来查看序列的类型
val a = Array(1, 2, 3, 4)
println(a.toString()) // [I@16c690dc
println(a.stringPrefix) // [I
sum
def sum[B >: A](implicit num : scala.Numeric[B]) : B = { /* compiled code */ }
序列求和,元素需为 Numeric[T] 类型
val a = Array(1, 2, 3, 4, 1)
println(a.sum) // 11
tail
override def tail : Repr = { /* compiled code */ }
返回当前序列中不包含第一个元素的序列
val a = Array(1, 2, 3, 4)
val b = a.tail
println(b.mkString(",")) // 2,3,4
tails
def tails : scala.collection.Iterator[Repr] = { /* compiled code */ }
同 inits,每一步都进行 tail 操作
val a = Array(1, 2, 3, 4)
val b = a.tails.toList
for (i <- 0 until b.length) {
val s = "第 %d 个值: %s"
println(s.format(i + 1, b(i).mkString(",")))
}
/*
* 第 1 个值: 1,2,3,4
* 第 2 个值: 2,3,4
* 第 3 个值: 3,4
* 第 4 个值: 4
* 第 5 个值:
*/
take
override def take(n : scala.Int) : Repr = { /* compiled code */ }
返回当前序列中,前 n 个元素组成的序列
val a = Array(1, 2, 3, 4)
val b = a.take(3)
println(b.mkString(",")) // 1,2,3
takeRight
override def takeRight(n : scala.Int) : Repr = { /* compiled code */ }
返回当前序列中,从右边开始,后 n 个元素组成的序列
val a = Array(1, 2, 3, 4)
val b = a.takeRight(3)
println(b.mkString(",")) // 2,3,4
takeWhile
override def takeWhile(p : scala.Function1[A, scala.Boolean]) : Repr = { /* compiled code */ }
返回当前序列中,从第一个元素开始,满足条件的连续元素组成的序列
val a = Array(1, 2, 3, 4, 1, 2)
val b = a.takeWhile(x => x < 3)
print(b.mkString(",")) // 1,2
to
override def to[Col[_]](implicit cbf: scala.collection.generic.CanBuildFrom[Nothing,Int,Col[Int]]): Col[Int]
将序列转化为scala.collection.immutable.IndexedSeq[Int] = Vector
val a = Array(1,2,3,4)
a.to
scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4)
toArray
def toArray: Array[A]
将序列转换成 Array 类型
toBuffer
def toBuffer[A1 >: A]: Buffer[A1]
将序列转换成 Buffer 类型
toIndexedSeq
def toIndexedSeq: collection.immutable.IndexedSeq[T]
将序列转换成 IndexedSeq 类型
toIterable
def toIterable: collection.Iterable[T]
将序列转换成可迭代的类型
toIterator
def toIterator: collection.Iterator[T]
将序列转换成迭代器,同 iterator 方法
toList
def toList: List[T]
将序列转换成 List 类型
toMap
def toMap[T, U]: Map[T, U]
将序列转转换成 Map 类型,需要被转化序列中包含的元素是 Tuple2 类型
toSeq
def toSeq: collection.Seq[T]
将序列转换成 Seq 类型
toSet
def toSet[B >: A]: Set[B]
将序列转换成 Set 类型
toStream
def toStream: collection.immutable.Stream[T]
将序列转换成 Stream 类型
toTraversable
def toTraversable: Traversable[Int]
将序列转换成 Traversable类型
toVector
def toVector: Vector[T]
将序列转换成 Vector 类型
transform
def transform(f: Int => Int): scala.collection.mutable.WrappedArray[Int]
将序列中的元素转换成指定的元素(同类型)
val a = Array(1,2,3,4)
a.transform(x => 6)
Array(6,6,6,6)
transpose
def transpose[U](implicit asArray : scala.Function1[T, scala.Array[U]]) : scala.Array[scala.Array[U]] = { /* compiled code */ }
矩阵转置,二维数组行列转换
val a = Array(Array("a", "b"), Array("c", "d"), Array("e", "f"))
val b = a.transpose
b.foreach(x => println((x.mkString(","))))
/*
* a,c,e
* b,d,f
*/
union
override def union[B >: A, That](that : scala.collection.GenSeq[B])(implicit bf : scala.collection.generic.CanBuildFrom[Repr, B, That]) : That = { /* compiled code */ }
合并两个序列,同操作符 ++
val a = Array(1, 2)
val b = Array(3, 4)
val c = a.union(b)
println(c.mkString(",")) // 1,2,3,4
unzip
def unzip[T1, T2](implicit asPair : scala.Function1[T, scala.Tuple2[T1, T2]], ct1 : scala.reflect.ClassTag[T1], ct2 : scala.reflect.ClassTag[T2]) : scala.Tuple2[scala.Array[T1], scala.Array[T2]] = { /* compiled code */ }
将含有两个二元组的数组,每个元组的第一个元素组成一个数组,第二个元素组成一个数组,返回包含这两个数组的元组
val a = Array(("a", "b"), ("c", "d"))
val b = a.unzip
println(b._1.mkString(",")) // a,c
println(b._2.mkString(",")) // b,d
unzip3
def unzip3[T1, T2, T3](implicit asTriple : scala.Function1[T, scala.Tuple3[T1, T2, T3]], ct1 : scala.reflect.ClassTag[T1], ct2 : scala.reflect.ClassTag[T2], ct3 : scala.reflect.ClassTag[T3]) : scala.Tuple3[scala.Array[T1], scala.Array[T2], scala.Array[T3]] = { /* compiled code */ }
将含有3个元祖的数组,每个元祖的第一个元素组成一个数组,第二个元素组成一个数组,第三个元素组成一个数组,返回包含这三个数组的元组
val a = Array(("a", "b", "x"), ("c", "d", "y"), ("e", "f", "z"))
val b = a.unzip3
println(b._1.mkString(",")) // a,c,e
println(b._2.mkString(",")) // b,d,f
println(b._3.mkString(",")) // x,y,z
update
def update(i : scala.Int, x : T) : scala.Unit = { /* compiled code */ }
将序列中 i 索引处的元素更新为 x (直接在本数组上修改)
val a = Array(1, 2, 3, 4)
a.update(1, 5)
println(a.mkString(",")) //1,5,3,4
updated
def updated[B >: A, That](index : scala.Int, elem : B)(implicit bf : scala.collection.generic.CanBuildFrom[Repr, B, That]) : That = { /* compiled code */ }
将序列中 i 索引处的元素更新为 x,并返回替换后的数组(修改副本)
val a = Array(1, 2, 3, 4)
val b = a.updated(1, 5)
println(b.mkString(",")) //1,5,3,4
view
override def view : scala.AnyRef with scala.collection.mutable.IndexedSeqView[A, Repr] = { /* compiled code */ }
override def view(from : scala.Int, until : scala.Int) : scala.collection.mutable.IndexedSeqView[A, Repr] = { /* compiled code */ }
返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素(左闭右开)
val a = Array(1, 2, 3, 4)
val b = a.view(1, 3)
println(b.mkString(",")) // 2,3
withFilter
def withFilter(p : scala.Function1[A, scala.Boolean]) : scala.collection.generic.FilterMonadic[A, Repr] = { /* compiled code */ }
根据条件 p 过滤元素
val a = Array(1, 2, 3, 4)
val b = a.withFilter(x => x > 2).map(x => x)
println(b.mkString(",")) // 3,4
zip
override def zip[A1 >: A, B, That](that : scala.collection.GenIterable[B])(implicit bf : scala.collection.generic.CanBuildFrom[Repr, scala.Tuple2[A1, B], That]) : That = { /* compiled code */ }
将两个序列对应位置上的元素组成一个元组数组,要求两个序列长度相同
val a = Array(1, 2, 3, 4)
val b = Array(4, 3, 2, 1)
val c = a.zip(b)
println(c.mkString(",")) // (1,4),(2,3),(3,2),(4,1)
zipAll
def zipAll[B, A1 >: A, That](that : scala.collection.GenIterable[B], thisElem : A1, thatElem : B)(implicit bf : scala.collection.generic.CanBuildFrom[Repr, scala.Tuple2[A1, B], That]) : That = { /* compiled code */ }
同 zip ,但是允许两个序列长度不同,不足的自动填充,如果当前序列短,不足的元素填充为 thisElem,如果 that 序列短,填充为 thatElem
val a = Array(1, 2, 3, 4, 5, 6, 7)
val b = Array(5, 4, 3, 2, 1)
val c = a.zipAll(b, 8, 9) // (1,5),(2,4),(3,3),(4,2),(5,1),(6,9),(7,9)
println(c.mkString(","))
val x = Array(1, 2, 3, 4)
val y = Array(6, 5, 4, 3, 2, 1)
val z = x.zipAll(y, 8, 9) // (1,6),(2,5),(3,4),(4,3),(8,2),(8,1)
println(z.mkString(","))
zipWithIndex
override def zipWithIndex[A1 >: A, That](implicit bf : scala.collection.generic.CanBuildFrom[Repr, scala.Tuple2[A1, scala.Int], That]) : That = { /* compiled code */ }
序列中的每个元素和它的索引组成一个元组数组
val a = Array('a', 'b', 'c', 'd')
val b = a.zipWithIndex
println(b.mkString(",")) // (a,0),(b,1),(c,2),(d,3)
subSequence
def subSequence(start : scala.Int, end : scala.Int) : java.lang.CharSequence = { /* compiled code */ }
返回 start 和 end 间的字符序列,不包含 end 处的元素
val a = Array('a', 'b', 'c', 'd')
val b = a.subSequence(1, 3)
println(b.toString) // bc