Scala数组大全
- ++
- ++:
- +:
- :+
- /:
- :\
- addString(b)
- addString(b,sep)
- aggregate
- apply
- canEqual
- charAt
- clone
- collect
- collectFirst
- combinations
- compose
- contains
- containsSlice
- copyToArray(xs)
- copyToArray(xs, start)
- copyToArray(xs, start, len)
- copyToBuffer
- corresponds
- count
- diff
- distinct
- drop
- dropRight
- dropWhile
- endsWith
- exists
- filter
- find
- flatMap
- flatten
- fold
- foldLeft
- foldRight
- forall
- foreach
- goupBy
- gouped
- hasDefiniteSize
- head
- headOption
- indexOf(elem)
- indexOf(elem,from)
- indexOfSlice(that)
- indexOfSlice(that,from)
- indexWhere(p)
- indexWhere(p,from)
- indices
- init
- inits
- intersect
- isDefinedAt
- isEmpty
- isTraversableAgain
- iterator
- last
- lastIndexOf(elem)
- lastIndexOf(elem, end)
- lastIndexOfSlice(that)
- lastIndexOfSlice(that, end)
- lastIndexWhere(p)
- lastIndexWhere(p, end)
- lastOption
- length
- lengthCompare
- map
- max
- maxBy
- min
- minBy
- mkString
- mkString(sep)
- mkString(start, sep, end)
- nonEmpty
- padTo
- par
- partition
- patch
- permutations
- prefixLength
- product
- reduce
- reduceLeft
- reduceRight
- reduceLeftOption
- reduceRightOption
- reverse
- reverseIterator
- reverseMap
- sameElements
- scan
- scanLeft
- scanRight
- segmentLength
- seq
- size
- slice
- sliding(size,step)
- sortBy
- sorted
- sortWith
- span
- splitAt
- startsWith(that)
- startsWith(that, offset)
- stringPrefix
- subSequence
- 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(n,m,k)
- zipWithIndex
++
定义:def ++[B](that: GenTraversableOnce[B]): Array[B]
描述:合并集合,并返回一个新的数组,新数组包含左右两个集合的内容
案例:
var a=Array(1,2,3)
var b=Array(4,"a")
scala> a++b
res0: Array[Any] = Array(1, 2, 3, 4, a)
++:
定义:def ++:[B >: A, That](that: collection.Traversable[B])(implicit bf: CanBuildFrom[Array[T], B, That]): That
描述:这个方法同上一个方法类似,两个加号后面多了一个冒号,但是不同的是右边操纵数的类型决定着返回结果的类型
案例:
scala> var a=Array(1,2,3)
scala> var b=List(4,5,6)
scala> a++:b
res0: List[Int] = List(1, 2, 3, 4, 5, 6)
scala> b++:a
res1: Array[Int] = Array(4, 5, 6, 1, 2, 3)【由冒号后面决定类型】
+:
定义:def +:(elem: A): Array[A]
描述:在数组前面添加一个元素,并返回新的数组对象
案例:
var a =Array(1,2,3)
24+:a
res6: Array[Int] = Array(24, 1, 2, 3)
:+
定义:def +:(elem: A): Array[A]
描述:在数组后面添加一个元素,并返回新的数组对象
案例:
var a =Array(1,2,3)
a:+24
res7: Array[Int] = Array(1, 2, 3, 24)
/:
定义:def /:[B](z: B)(op: (B, T) ⇒ B): B
描述:对数组中所有的元素从左向右遍历,进行相同的迭代操作,foldLeft 的简写
案例:
var a =Array(1,2,3)
(88 /: a)(_+_) //(((88+1)+2)+3
res8: Int = 94
(88 /: a)(_*_) //(((88*1)*2)*3
res9: Int = 528
:\
定义:def /:[B](z: B)(op: (B, T) ⇒ B): B
描述:对数组中所有的元素从右向左遍历,进行相同的迭代操作,foldRight 的简写
案例:
var a =Array(1,2,3)
(a :\ 88)(_-_)
res13: Int = -86 //1-(2-(3-88))
(a:\88)(_*_)
res12: Int = 528 //1*(2*(3*88))
addString(b)
定义:def addString(b: StringBuilder): StringBuilder
描述:将数组中的元素逐个添加到 StringBuilder 中
案例:
scala> var y = new StringBuilder()
y: StringBuilder =
scala> b.addString(y)
res14: StringBuilder = 45a
addString(b,sep)
定义:def addString(b: StringBuilder, sep: String): StringBuilder
描述:将数组中的元素逐个添加到 StringBuilder 中,每个元素用 sep 分隔符分开
案例:
var b =Array(4,5,a)
scala> var y = new StringBuilder()
y: StringBuilder =
scala> b.addString(y,",")
res15: StringBuilder = 4,5,a
scala> b.addString(y,"{",",","}")
res16: StringBuilder = 4,5,a{4,5,a}
aggregate
定义:def aggregate[B](z: ⇒ B)(seqop: (B, T) ⇒ B, combop: (B, B) ⇒ B): B
描述:聚合计算,aggregate 是柯里化方法,参数是两个方法
案例:为了方便理解,把 aggregate 的两个参数分别封装成两个方法,并把分区和不分区的计算过程分别打印出来
def seqno(m: Int, n: Int): Int = {
val s = "seq_exp = %d + %d"
println(s.format(m, n))
m + n
}
def combine(m: Int, n: Int): Int = {
val s = "com_exp = %d + %d"
println(s.format(m, n))
m + n
}
def main(args: Array[String]) {
val a = List(1, 2, 3, 4)
val b = a.aggregate(5)(seqno, combine) // 不分区
println("b = " + b)
/**
* seq_exp = 5 + 1
* seq_exp = 6 + 2
* seq_exp = 8 + 3
* seq_exp = 11 + 4
* b = 15
*/
val c = a.par.aggregate(5)(seqno, combine) // 分区
println("c = " + c)
/**
* seq_exp = 5 + 3
* seq_exp = 5 + 2
* seq_exp = 5 + 4
* seq_exp = 5 + 1
* com_exp = 6 + 7
* com_exp = 8 + 9
* com_exp = 13 +17
* c = 30
*/
}
通过上面的运算不难发现,不分区时,seqno 是把初始值顺序和每个元素相加,把得到的结果与下一个元素进行运算。
分区时,seqno 是把初始值与每个元素相加,但结果不参与下一步运算,而是放到另一个序列中,由第二个方法 combine 进行处理。
上面过程可以简写为
val b = a.aggregate(5)(_+_,_+_) // 不分区
val c = a.par.aggregate(5)(_+_,_+_) // 分区
apply
定义:def apply(i: Int): T
描述:获取指定索引处的元素
案例:
var a =Array(1,2,3)
scala> a.apply(1)
res26: Int = 2
canEqual
定义:def canEqual(that: Any): Boolean
描述:判断两个对象是否可以进行比较
案例:
var a=Array(1,2,3)
var b=List(4,5,6)
scala> a.canEqual(b)
res2: Boolean = true
charAt
定义:def charAt(index: Int): Char
描述:获取 index 索引处的字符,这个方法会执行一个隐式的转换,将 Array[T] 转换为 ArrayCharSequence,只有当 T 为 Char 类型时,这个转换才会发生
案例:
var h =Array('a','b','c','d')
scala> h.charAt(2)
res28: Char = c
clone
定义:def clone(): Array[T]
描述:创建一个副本
案例:【修改k中值,a中不会跟随修改】
var a=Array(1,2,3)
scala> var k =a.clone()
k: Array[Int] = Array(1, 2, 3)
collect
定义:def collect[B](pf: PartialFunction[A, B]): Array[B]
描述:通过执行一个并行计算(偏函数),得到一个新的数组对象
案例:
var a = Array(1,2,3,4,5,6,7)
scala> var func:PartialFunction[Int,Int]={
| case x if x%2==0 =>x*2
| case x => x+100}
func: PartialFunction[Int,Int] = <function1>
scala> a.collect(func)
res0: Array[Int] = Array(101, 4, 103, 8, 105, 12, 107)
collectFirst
定义:def collectFirst[B](pf: PartialFunction[T, B]): Option[B]
描述:在序列中查找第一个符合偏函数定义的元素,并执行偏函数计算
案例:
var a = Array(1,2,3)
scala> var func:PartialFunction[Int,Int]={
| case x if x%2==0 =>x*2}
func: PartialFunction[Int,Int] = <function1>
scala> a.collectFirst(func)
res4: Option[Int] = Some(4)
combinations
定义:def combinations(n: Int): collection.Iterator[Array[T]]
描述:combinations 表示组合,这个排列组合会选出所有包含字符不一样的组合,但不考虑顺序,对于 “abc”、“cba”,视为相同组合,参数 n 表示序列长度,就是几个字符为一组
案例:
var a = Array(1,2,3)
scala> a.combinations(2)
res7: Iterator[Array[Int]] = non-empty iterator
scala> res7.foreach(println)
[I@30b1c5d5
[I@3e2d65e1
[I@1174676f
scala> res7.foreach(x=>println(x.mkString(",")))
scala> a.combinations(2)
res10: Iterator[Array[Int]] = non-empty iterator
scala> res10.foreach(x=>println(x.mkString(",")))
1,2
1,3
2,3
compose
描述:期待一个C类型转化为B类型,也就是是说compose则是将先执行靠里面的函数,再将结果传给外层函数,所以先执行f将C类型的x转化为一个A类型,再作为参数传给g,得到一个B类型。
contains
定义:def combinations(n: Int): collection.Iterator[Array[T]]
描述:combinations 表示组合,这个排列组合会选出所有包含字符不一样的组合,但不考虑顺序,对于 “abc”、“cba”,视为相同组合,参数 n 表示序列长度,就是几个字符为一组
案例:
var a = Array(1,2,3)
scala> a.contains(2)
res13: Boolean = true
containsSlice
定义:def containsSlice[B](that: GenSeq[B]): Boolean
描述:判断当前序列中是否包含另一个序列
案例:
var a = Array(1,2,3)
var c =Array(2,3)
scala> a.containsSlice(c)
res16: Boolean = true
copyToArray(xs)
定义:def copyToArray(xs: Array[A]): Unit
描述:将当前数组元素复制到另一个数组中
案例:
var a = Array(1,2,3)
scala> var d =new Array[Int](5)
d: Array[Int] = Array(0, 0, 0, 0, 0)
scala> a.copyToArray(d)
scala> d
res35: Array[Int] = Array(1, 2, 3, 0, 0)
copyToArray(xs, start)
定义:def copyToArray(xs: Array[A], start: Int): Unit
描述:将当前数组元素复制到另一个数组中,从 start 位置开始复制
案例:
var a = Array(1,2,3)
scala> var d =new Array[Int](5)
d: Array[Int] = Array(0, 0, 0, 0, 0)
scala> a.copyToArray(d,3)
scala> d
res37: Array[Int] = Array(0, 0, 0, 1, 2)
copyToArray(xs, start, len)
定义:def copyToArray(xs: Array[A], start: Int, len: Int): Unit
描述:将当前数组元素复制到另一个数组中,从 start 位置开始复制,长度为 len
案例:
var a = Array(1,2,3)
scala> var d =new Array[Int](5)
d: Array[Int] = Array(0, 0, 0, 0, 0)
scala> a.copyToArray(d,2,2)
scala> d
res41: Array[Int] = Array(0, 0, 1, 2, 0)
copyToBuffer
定义:def copyToBuffer[B >: A](dest: Buffer[B]): Unit
描述:将数组中的元素复制到 Buffer 中
案例:
var a = Array(1,2,3)
scala> var d = new ArrayBuffer[Int]()
d: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> a.copyToBuffer(d)
scala> d
res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
corresponds
定义:def corresponds[B](that: GenSeq[B])(p: (T, B) ⇒ Boolean): Boolean
描述:判断两个序列的长度以及对应位置元素是否符合某个条件。如果两个序列具有相同的元素数量并且 p(x, y)=true,则返回 true
案例:
var a = Array(1,2,3)
var c=Array(4,5,6)
scala> a.corresponds(c)(_>_)
res45: Boolean = false
scala> a.corresponds(c)(_<_)
res46: Boolean = true
count
定义:def count(p: (T) ⇒ Boolean): Int
描述:统计符合条件的元素个数
案例:
var e =Array(1,2,3,4,5,6,7,8)
scala> e.count(x=>x<5)
res47: Int = 4
省略写法
scala> e.count(_>5)
res48: Int = 3
diff
定义:def diff(that: collection.Seq[T]): Array[T]
描述:计算当前数组与另一个数组的差集,即将当前数组中没有在另一个数组中出现的元素返回
案例:
var a =Array(1,2,3)
var c = Array(4, 5, 1, 2, 8)
scala> c.diff(a)
res113: Array[Int] = Array(4, 5, 8)
distinct
定义:def distinct: Array[T]
描述:去除当前集合中重复的元素,只保留一个
案例:
scala> var h =Array(1,2,3,1,4,5,3)
scala> h.distinct
res115: Array[Int] = Array(1, 2, 3, 4, 5)
drop
定义:def drop(n: Int): Array[T]
描述:将当前数组中前 n 个元素去除,返回一个新数组
案例:
var h =Array(1,2,3,1,4,5,3)
scala> h.drop(2)
res117: Array[Int] = Array(3, 1, 4, 5, 3)
dropRight
定义:def dropRight(n: Int): Array[T]
描述:功能同 drop,去掉尾部的 n 个元素
案例:
var h =Array(1,2,3,1,4,5,3)
scala> h.dropRight(4)
res119: Array[Int] = Array(1, 2, 3)
dropWhile
定义:def dropWhile(p: (T) ⇒ Boolean): Array[T]
描述:去除当前数组中符合条件的元素,返回剩余的数组,这个需要一个条件,就是从当前数组的第一个元素起,就要满足条件,直到碰到第一个不满足条件的元素结束(即使后面还有符合条件的元素),否则返回整个数组
案例:
var a =Array(1,2,3)
scala> a.dropWhile(_<3)
res50: Array[Int] = Array(3)
scala> a.dropWhile(_>2)
res51: Array[Int] = Array(1, 2, 3)
endsWith
定义:def endsWith[B](that: GenSeq[B]): Boolean
描述:判断当前序列是否以某个序列结尾
案例:
var a =Array(1,2,3)
scala> a.endsWith(Array(2,3))
res52: Boolean = true
exists
定义:def exists(p: (T) ⇒ Boolean): Boolean
描述:判断当前数组是否包含符合条件的元素
案例:
var a =Array(1,2,3)
scala> a.exists(x=>x==2) //scala> a.exists(_==2)(省略写法)
res53: Boolean = true
filter
定义:def filter(p: (T) ⇒ Boolean): Array[T]
描述:取得当前数组中符合条件的元素,组成新的数组返回
案例:
var e =Array(1,2,3,4,5,6,7,8)
scala> e.filter(x=>x<5)
res57: Array[Int] = Array(1, 2, 3, 4)
scala> e.filter(_>5)
res58: Array[Int] = Array(6, 7, 8)
find
定义:def find(p: (T) ⇒ Boolean): Option[T]
描述:查找第一个符合条件的元素,返回 Option
案例:
var e =Array(1,2,3,4,5,6,7,8)
scala> e.find(x=>x>3)
res59: Option[Int] = Some(4)
flatMap
定义:def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Array[B]
描述:对当前序列的每个元素进行操作,结果放入新序列返回,参数要求是 GenTraversableOnce 及其子类
案例:
var a=Array(1,2,3,4)
scala> a.flatMap(x=>1 to x)
res1: Array[Int] = Array(1, 1, 2, 1, 2, 3, 1, 2, 3, 4)
flatten
定义:def flatten[U](implicit asTrav: (T) ⇒ collection.Traversable[U], m: ClassTag[U]): Array[U]
描述:扁平化,将二维数组的所有元素组合在一起,形成一个一维数组返回
案例:
scala> var b=Array(Array(1,2,3,4),Array(5,6,7))
b: Array[Array[Int]] = Array(Array(1, 2, 3, 4), Array(5, 6, 7))
scala> b.flatten
res2: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
fold
定义:def fold[A1 >: A](z: A1)(op: (A1, A1) ⇒ A1): A1ClassTag[U]): Array[U]
描述:对序列中的每个元素进行二元运算,和 aggregate 有类似的语义,但执行过程有所不同
案例:
def seqno(m: Int, n: Int): Int = {
val s = "seq_exp = %d + %d"
println(s.format(m, n))
m + n
}
def main(args: Array[String]) {
val a = Array(1, 2, 3, 4)
val b = a.fold(5)(seqno) // 不分区
println("b = " + b)
/**
* seq_exp = 5 + 1
* seq_exp = 6 + 2
* seq_exp = 8 + 3
* seq_exp = 11 + 4
* b = 15
*/
val c = a.par.fold(5)(seqno) // 分区
println("c = " + c)
/**
* seq_exp = 5 + 3
* seq_exp = 5 + 2
* seq_exp = 5 + 4
* seq_exp = 5 + 1
* com_exp = 6 + 7
* com_exp = 8 + 9
* com_exp = 13 + 17
* c = 30
*/
}
foldLeft
定义:def foldLeft[B](z: B)(op: (B, T) ⇒ B): BClassTag[U]): Array[U]
描述:从左到右计算,简写方式:def /:[B](z: B)(op: (B, T) ⇒ B): B
案例:
def seqno(m: Int, n: Int): Int = {
val s = "seq_exp = %d + %d"
println(s.format(m, n))
m + n
}
def main(args: Array[String]): Unit = {
val a = Array(1, 2, 3, 4)
val b = a.foldLeft(5)(seqno) // 简写: (5 /: a)(_ + _)
println("b = " + b)
/**
* seq_exp = 5 + 1
* seq_exp = 6 + 2
* seq_exp = 8 + 3
* seq_exp = 11 + 4
* b = 15
*/
}
foldRight
定义:foldRight[B](z: B)(op: (B, T) ⇒ B): B
描述:从右到左计算,简写方式:def :[B](z: B)(op: (T, B) ⇒ B): B
案例:
def seqno(m: Int, n: Int): Int = {
val s = "seq_exp = %d + %d"
println(s.format(m, n))
m + n
}
def main(args: Array[String]): Unit = {
val a = Array(1, 2, 3, 4)
val b = a.foldRight(5)(seqno) // 简写: (a :\ 5)(_ + _)
println("b = " + b)
/**
* seq_exp = 4 + 5
* seq_exp = 3 + 9
* seq_exp = 2 + 12
* seq_exp = 1 + 14
* b = 15
*/
}
forall
定义:def forall(p: (T) ⇒ Boolean): Boolean
描述:检测序列中的元素是否都满足条件 p,如果序列为空,则返回 true
案例:
scala> a
res17: Array[Int] = Array(1, 2, 3, 4)
scala> a.forall(x=>x>5)
res18: Boolean = false
scala> a.forall(x=>x<5)
res19: Boolean = true
foreach
定义:def foreach(f: (A) ⇒ Unit): Unit
描述:遍历序列中的元素,进行 f 操作
案例:
scala> var a =Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> a.foreach(x=>println(x*10))
10
20
30
40
goupBy
定义:def groupBy[K](f: (T) ⇒ K): Map[K, Array[T]]
描述:按条件分组,条件由 f 匹配,返回值是 Map 类型,每个 key 对应一个数组
案例:
var c =Array(1,2,3,4,5,6,7,8)
scala> c.groupBy(x=>x match{
| case x if x%2==0 =>"ou"
| case _=>"ji"})
res141: scala.collection.immutable.Map[String,Array[Int]] = Map(ou -> Array(2, 4, 6, 8), ji -> Array(1, 3, 5, 7))
scala> res141.foreach(x => println(x._1,x._2.mkString(",")))
(ou,2,4,6,8)
(ji,1,3,5,7)
gouped
定义:def grouped(size: Int): collection.Iterator[Array[T]]
描述:按指定数量分组,每组有 size 个元素,返回一个迭代器
案例:
var c =Array(1,2,3,4,5,6,7,8)
scala> c.grouped(3)
res143: Iterator[Array[Int]] = non-empty iterator
scala> res143.foreach(x=>println(x.mkString(",")))
1,2,3
4,5,6
7,8
hasDefiniteSize
定义:def hasDefiniteSize: Boolean
描述:检测序列是否存在有限的长度,对应 Stream 这样的流数据则返回 false
案例:
var c =Array(1,2,3,4,5,6,7,8)
scala> c.hasDefiniteSize
res145: Boolean = true
head
定义:def head: T
描述:返回序列的第一个元素,如果序列为空,将引发错误
案例:
var c =Array(1,2,3,4,5,6,7,8)
scala> c.head
res152: Int = 1
headOption
定义:def headOption: Option[T]
描述:返回序列的第一个元素的 Option 类型对象,如果序列为空,则返回 None
案例:
var a =Array(1,2,3,4)
scala> a.headOption
res1: Option[Int] = Some(1)
//b为空
scala> var b=Array[Int]()
b: Array[Int] = Array()
scala> b.headOption
res3: Option[Int] = None
indexOf(elem)
定义:def indexOf(elem: T): Int
描述:返回元素 elem 在序列中第一次出现的索引
案例:
scala> var c =Array(1,2,3,4,2,3,4,5)
c: Array[Int] = Array(1, 2, 3, 4, 2, 3, 4, 5)
scala> c.indexOf(3)
res4: Int = 2
indexOf(elem,from)
定义:def indexOf(elem: T, from: Int): Int
描述:返回元素 elem 在序列中第一次出现的索引,指定从索引 from 开始查找
案例:
scala> var c =Array(1,2,3,4,2,3,4,5)
c: Array[Int] = Array(1, 2, 3, 4, 2, 3, 4, 5)
scala> c.indexOf(3,3)
res5: Int = 5
indexOfSlice(that)
定义:def indexOfSlice[B >: A](that: GenSeq[B]): Int
描述:检测当前序列中是否包含序列 that,并返回第一次出现该序列的索引
案例:
var c =Array(1,2,3,4,2,3,4,5)
var d =Array(4,2)
scala> c.indexOfSlice(d)
res6: Int = 3
indexOfSlice(that,from)
定义:def indexOfSlice[B >: A](that: GenSeq[B], from: Int): Int
描述:检测当前序列中是否包含另一个序列 that,指定从索引 from 开始查找,并返回第一次出现该序列的索引
案例:
var c =Array(1,2,3,4,2,3,4,5)
var d =Array(2,3)
scala> c.indexOfSlice(d,2)
res7: Int = 4
indexWhere§
定义:def indexWhere(p: (T) ⇒ Boolean): Int
描述:返回当前序列中第一个满足条件 p 的元素的索引
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.indexWhere(_>4)
res164: Int = 6
indexWhere(p,from)
定义:def indexWhere(p: (T) ⇒ Boolean, from: Int): Int
描述:返回当前序列中第一个满足条件 p 的元素的索引,指定从索引 from 开始查找
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.indexWhere(_>3,4)
res165: Int = 5
indices
定义:def indices: collection.immutable.Range
描述:返回当前序列索引集合
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.indices
res168: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
init
定义:def init: Array[T]
描述:返回当前序列中不包含最后一个元素的序列(去尾)
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.init
res170: Array[Int] = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5)
inits
定义:def inits: collection.Iterator[Array[T]]
描述:对集合中的元素进行 init 迭代操作,该操作的返回值中, 第一个值是当前序列的副本,最后一个值为空,每一步都进行 init 操作,上一步的结果作为下一步的操作对象
案例:
var a = Array(1, 2, 3, 4)
scala> a.inits
res9: Iterator[Array[Int]] = non-empty iterator
scala> a.inits.foreach(x=>println(x.mkString(",")))
1,2,3,4
1,2,3
1,2
1
intersect
定义:def intersect(that: collection.Seq[T]): Array[T]
描述:取两个集合的交集
案例:
var a = Array(1, 2, 3, 4)
var d =Array(2,3,4,5,6)
scala> a.intersect(d)
res15: Array[Int] = Array(2, 3, 4)
isDefinedAt
定义:def isDefinedAt(idx: Int): Boolean
描述:判断序列中是否存在指定索引
案例:
var a = Array(1, 2, 3, 4)
scala> a.isDefinedAt(2)
res18: Boolean = true
scala> a.isDefinedAt(6)
res19: Boolean = false
isEmpty
定义:def isEmpty: Boolean
描述:判断序列是否为空
案例:
var a = Array(1, 2, 3, 4)
var b =Array[Int]()
scala> a.isEmpty
res20: Boolean = false
scala> b.isEmpty
res21: Boolean = true
isTraversableAgain
定义:def isTraversableAgain: Boolean
描述:判断序列是否可以反复遍历,该方法是 GenTraversableOnce 中的方法,对于 Traversables 一般返回 true,对于 Iterators 返回 false,除非被复写
案例:
var a =Array(1,2,3,4)
var g =a.iterator
g: Iterator[Int] = non-empty iterator
scala> a.isTraversableAgain
res23: Boolean = true
scala> g.isTraversableAgain
res25: Boolean = false
iterator
定义:def iterator: collection.Iterator[T]
描述:生成当前序列的迭代器
案例:
var a =Array(1,2,3,4)
scala> a.iterator
res27: Iterator[Int] = non-empty iterator
last
定义:def last: T
描述:返回序列的最后一个元素,如果序列为空,将引发错误
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.last
res193: Int = 3
lastIndexOf(elem)
定义:def lastIndexOf(elem: T): Int
描述:返回元素 elem 在序列中最后一次出现的索引
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.lastIndexOf(5)
res194: Int = 9
lastIndexOf(elem, end)
定义:def lastIndexOf(elem: T, end: Int): Int
描述:返回元素 elem 在序列中最后一次出现的索引,指定在索引 end 之前(包括)的元素中查找
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.lastIndexOf(1,9)
res195: Int = 3
lastIndexOfSlice(that)
定义:def lastIndexOfSlice[B >: A](that: GenSeq[B]): Int
描述:检测当前序列中是否包含序列 that,并返回最后一次出现该序列的索引
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
var j = Array(1,3)
scala> h.lastIndexOfSlice(j)
res196: Int = 3
lastIndexOfSlice(that, end)
定义:def lastIndexOfSlice[B >: A](that: GenSeq[B], end: Int): Int
描述:检测当前序列中是否包含序列 that,并返回最后一次出现该序列的索引,指定在索引 end 之前(包括)的元素中查找
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
var j = Array(1,3)
scala> h.lastIndexOfSlice(j,2)
res198: Int = 0
lastIndexWhere§
定义:ef lastIndexWhere(p: (T) ⇒ Boolean): Int
描述:返回当前序列中最后一个满足条件 p 的元素的索引
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.lastIndexWhere(_<3)
res200: Int = 7
lastIndexWhere(p, end)
定义:def lastIndexWhere(p: (T) ⇒ Boolean, end: Int): Int
描述:返回当前序列中最后一个满足条件 p 的元素的索引,指定在索引 end 之前(包括)的元素中查找
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.lastIndexWhere(_<3,6)
res201: Int = 3
lastOption
定义:def lastOption: Option[T]
描述:返回序列的最后一个元素的 Option 类型对象,如果序列为空,则返回 None
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
var b = Array[Int]()
scala> h.lastOption
res202: Option[Int] = Some(3)
scala> b.lastOption
res29: Option[Int] = None
length
定义:def length: Int
描述:返回序列元素个数
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.length
res203: Int = 11
lengthCompare
定义:def lengthCompare(len: Int): Int
描述:比较序列的长度和参数 len,返回序列的长度 - len
案例:
var h = Array(1, 3, 4, 1, 3, 4, 5, 2, 4, 5, 3)
scala> h.lengthCompare(10)
res206: Int = 1
scala> h.lengthCompare(20)
res207: Int = -9
scala> h.lengthCompare(11)
res208: Int = 0
map
定义:def map[B](f: (A) ⇒ B): Array[B]
描述:对序列中的元素进行 f 操作,返回生成的新序列
案例:
var a = Array(1,2,3,4)
scala> a.map(x=>x*2)
res32: Array[Int] = Array(2, 4, 6, 8)
max
定义:def max: A
描述:返回序列中最大的元素
案例:
var c = Array(1, 2, 3, 4, 2, 3, 4, 5)
scala> c.max
res34: Int = 5
maxBy
定义:def maxBy[B](f: (A) ⇒ B): A
描述:返回序列中符合条件的第一个元素
案例:
var c = Array(1, 2, 3, 4, 2, 3, 4, 5)
scala> c.maxBy(x=>x>3)
res35: Int = 4
min
定义:def max: A
描述:返回序列中最小的元素
案例:
var c = Array(1, 2, 3, 4, 2, 3, 4, 5)
scala> c.min
res36: Int = 1
minBy
定义:def minBy[B](f: (A) ⇒ B): A
描述:返回序列中不符合条件的第一个元素
案例:
var c = Array(1, 2, 3, 4, 2, 3, 4, 5)
scala> c.minBy(x=>x<3)
res37: Int = 3
mkString
定义:def mkString: String
描述:将序列中所有元素拼接成一个字符串
案例:
scala> var h =Array("a","b","c")
h: Array[String] = Array(a, b, c)
scala> h.mkString
res38: String = abc
mkString(sep)
定义:def mkString: String
描述:将序列中所有元素拼接成一个字符串,以 sep 作为元素间的分隔符
案例:
var h =Array("a","b","c")
scala> h.mkString("*")
res39: String = a*b*c
mkString(start, sep, end)
定义:def mkString(start: String, sep: String, end: String): String
描述:将序列中所有元素拼接成一个字符串,以 start 开头,以 sep 作为元素间的分隔符,以 end 结尾
案例:
var h =Array("a","b","c")
scala> h.mkString("{","*","}")
res40: String = {a*b*c}
nonEmpty
定义:def nonEmpty: Boolean
描述:判断序列是否不为空
案例:
var a =Array(1,2,3)
var b = Array[Int]()
scala> a.nonEmpty
res41: Boolean = true
scala> b.nonEmpty
res42: Boolean = false
padTo
定义:def padTo(len: Int, elem: A): Array[A]
描述:填充序列,如果当前序列长度小于 len,那么新产生的序列长度是 len,多出的几个位值填充 elem,如果当前序列大于等于 len ,则返回当前序列
案例:
var a = Array(1, 2, 3, 4)
scala> a.padTo(8,9)
res44: Array[Int] = Array(1, 2, 3, 4, 9, 9, 9, 9)
par
定义:def par: ParArray[T]
描述:返回一个并行实现,产生的并行序列不能被修改
案例:
var a =Array(1,2,3,4)
scala> a.par
res50: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3, 4)
partition
定义:def partition(p: (T) ⇒ Boolean): (Array[T], Array[T])
描述:按条件将序列拆分成两个数组,满足条件的放到第一个数组,其余的放到第二个数组,返回的是包含这两个数组的元组
案例:
var c = Array(1, 2, 3, 4, 2, 3, 4, 5)
scala> c.partition(x=>x<=3)
res54: (Array[Int], Array[Int]) = (Array(1, 2, 3, 2, 3),Array(4, 4, 5))
patch
定义:def patch(from: Int, that: GenSeq[A], replaced: Int): Array[A]
描述:批量替换,返回新数组(从当前序列的第n个元素开始,后面的m个元素,被序列批量替换)
案例:
var a =Array(1,2,3,4)
var c = Array(1, 2, 3, 4, 2, 3, 4, 5)
scala> c.patch(2,a,2)
res59: Array[Int] = Array(1, 2, 1, 2, 3, 4, 2, 3, 4, 5)//从c的第三个元素开始,取两个元素,即 3 和 4 ,将这两个元素替换为序列 a
permutations
定义:def permutations: collection.Iterator[Array[T]]
描述:排列组合(考虑顺序且无法指定组合,而combinations不考虑排序),返回一个迭代器
案例:
scala> var r =Array(1,2,3)
r: Array[Int] = Array(1, 2, 3)
scala> r.permutations.foreach(x=>println(x.mkString(",")))
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1
prefixLength
定义:def prefixLength(p: (T) ⇒ Boolean): Int
描述:给定一个条件 p,返回一个前置数列的长度,这个数列中的元素都满足 p
案例:
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
定义:def product: A
描述:返回所有元素乘积的值
案例:
var r =Array(1,2,3)
scala> r.product
res62: Int = 6
reduce
定义:def reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1
描述:同 fold,不需要初始值
案例:
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
定义:def reduceLeft[B >: A](op: (B, T) ⇒ B): B
描述:同 foldLeft,从左向右计算,不需要初始值
案例:
scala> var a=Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> a.reduceLeft(_-_)//1-2-3=-4
res105: Int = -4
reduceRight
定义:def reduceRight[B >: A](op: (B, T) ⇒ B): B
描述:同 foldRight,从右向左计算,不需要初始值
案例:
var a = Array(1, 2, 3, 4)
scala> a.reduceRight(_*_)//4*3*2*1
res64: Int = 24
reduceLeftOption
定义:def reduceLeftOption[B >: A](op: (B, T) ⇒ B): Option[B]
描述:同 reduceLeft,返回 Option
案例:
var a = Array(1, 2, 3)
scala> a.reduceLeftOption(_-_)
res107: Option[Int] = Some(-4)
reduceRightOption
定义:def reduceRightOption[B >: A](op: (T, B) ⇒ B): Option[B]
描述:同 reduceRight,返回 Option
案例:
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
定义:def reverse: Array[T]
描述:反转序列
案例:
var a = Array(1, 2, 3, 4)
scala> a.reverse
res66: Array[Int] = Array(4, 3, 2, 1)
reverseIterator
定义:def reverseIterator: collection.Iterator[T]
描述:生成反向迭代器
案例:
var a =Array(1,2,3,4)
scala> a.reverseIterator.foreach(print)
4321
reverseMap
定义:def reverseMap[B](f: (A) ⇒ B): Array[B]
描述:同 map,方向相反
案例:
var a =Array(1,2,3,4)
scala> a.reverseMap(x=>x*10)
res72: Array[Int] = Array(40, 30, 20, 10)
sameElements
定义:def sameElements(that: GenIterable[A]): Boolean
描述:判断两个序列是否顺序和对应位置上的元素都一样
案例:
var a = Array(1, 2, 3, 4)
var c = Array(1, 2, 3, 4)
var d = Array(2, 3, 4, 5, 6)
scala> a.sameElements(c)
res76: Boolean = true
scala> a.sameElements(d)
res77: Boolean = false
scan
定义:def scan[B >: A, That](z: B)(op: (B, B) ⇒ B)(implicit cbf: CanBuildFrom[Array[T], B, That]): That
描述:同 fold,scan 会把每一步的计算结果放到一个新的集合中返回,而 fold 返回的是最后的结果
案例:
var a =Array(1,2,3,4)
scala> a.scan(4)(_+_)//4为初始值,4+1=5,5+2=7,7+3=10,10+4=14
res78: Array[Int] = Array(4, 5, 7, 10, 14)
scanLeft
定义:def scanLeft[B, That](z: B)(op: (B, T) ⇒ B)(implicit bf: CanBuildFrom[Array[T], B, That]): That
描述:同 foldLeft,从左向右计算,每一步的计算结果放到一个新的集合中返回
案例:
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
定义:def scanRight[B, That](z: B)(op: (T, B) ⇒ B)(implicit bf: CanBuildFrom[Array[T], B, That]): That
描述:同 foldRight,从右向左计算,每一步的计算结果放到(从右向左放)一个新的集合中返回
案例:
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
定义:def segmentLength(p: (T) ⇒ Boolean, from: Int): Int
描述:从序列的 from 开始向后查找,返回满足条件 p 的连续元素的长度,只返回第一个
案例:
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
定义:def seq: collection.mutable.IndexedSeq[T]
描述:产生一个引用当前序列的 sequential 视图
案例:
scala> a
res83: Array[Int] = Array(1, 2, 3, 4)
scala> a.seq
res84: scala.collection.mutable.IndexedSeq[Int] = WrappedArray(1, 2, 3, 4)
size
定义:def size: Int
描述:返回序列元素个数,同 length
案例:
var a =Array(1,2,3,4)
scala> a.size
res85: Int = 4
slice
定义:def slice(from: Int, until: Int): Array[T]
描述:返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素
案例:
scala> var a=Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> a.slice(1,3)
res151: Array[Int] = Array(2, 3)
sliding(size,step)
定义:def sliding(size: Int): collection.Iterator[Array[T]]
描述:从第一个元素开始,每个元素和它后面的 size - 1 个元素组成一个数组,最终组成一个新的集合返回,当剩余元素个数不够 size 时,则结束。该方法可以设置步长 step,每一组元素组合完后,下一组从上一组起始元素位置 + step 后的位置处开始
案例:
var a =Array(1,2,3,4)
scala> a.sliding(2,2)
res89: Iterator[Array[Int]] = non-empty iterator
scala> a.sliding(2,2).foreach(x=>println(x.mkString(",")))
1,2
3,4
sortBy
定义:def sortBy[B](f: (T) ⇒ B)(implicit ord: math.Ordering[B]): Array[T]
描述:按指定的排序规则对序列排序(x=>x)升序,(x=> -x)
案例:
var d =Array(2,3,4,1,2)
scala> d.sortBy(x=>x)
res95: Array[Int] = Array(1, 2, 2, 3, 4)
scala> d.sortBy(x=>0 - x)
res97: Array[Int] = Array(4, 3, 2, 2, 1)
sorted
定义:def sorted[B >: A](implicit ord: math.Ordering[B]): Array[T]]
描述:使用默认的排序规则对序列排序
案例:
var d =Array(2,3,4,1,2)
scala> d.sorted
res98: Array[Int] = Array(1, 2, 2, 3, 4)
sortWith
定义:def sortWith((符号)): Array[T]]
描述:自定义排序方法
案例:
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)
span
定义:def span(p: (T) ⇒ Boolean): (Array[T], Array[T])
描述:将序列拆分成两个数组,从第一个元素开始,直到第一个不满足条件的元素为止,其中的元素放到第一个数组,其余的放到第二个数组,返回的是包含这两个数组的元组
案例:
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
定义:def splitAt(n: Int): (Array[T], Array[T])
描述:从指定位置开始,把序列拆分成两个数组
案例:
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(that)
定义:def startsWith[B](that: GenSeq[B]): Boolean
描述:判断序列是否以某个序列开始
案例:
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
startsWith(that, offset)
定义:def startsWith[B](that: GenSeq[B], offset: Int): Boolean
描述:判断序列从指定偏移处是否以某个序列开始
案例:
scala> var a =Array(1,2,3,4,5,6)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.startsWith(Array(3,4),5)
res99: Boolean = false
scala> a.startsWith(Array(3,4),2)
res100: Boolean = true
stringPrefix
定义:def stringPrefix: String
描述:返回 toString 结果的前缀
案例: 不知道有什么用
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
subSequence
定义:def subSequence(start: Int, end: Int): CharSequence
描述:返回 start 和 end 间的字符序列,不包含 end 处的元素
案例:
scala> var a=Array('a','b','c')
a: Array[Char] = Array(a, b, c)
scala> a.subSequence(1,2)
res11: CharSequence = b
sum
定义:def sum: A
描述:序列求和,元素需为 Numeric[T] 类型
案例:
scala> a
res102: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.sum
res101: Int = 21
tail
定义:def tail: Array[T]
描述:返回当前序列中不包含第一个元素的序列
案例:
scala> a
res102: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.tail
res103: Array[Int] = Array(2, 3, 4, 5, 6)
tails
定义:def tails: collection.Iterator[Array[T]]
描述:同 inits,每一步都进行 tail 操作
案例:
scala> c
res104: Array[Int] = Array(1, 2, 3, 4)
scala> c.tails
res105: Iterator[Array[Int]] = non-empty iterator
scala> c.tails.foreach(x=>println(x.mkString(",")))
1,2,3,4
2,3,4
3,4
4
take
定义:def take(n: Int): Array[T]
描述:返回当前序列中,前 n 个元素组成的序列
案例:
var a = Array(1, 2, 3, 4, 5, 6)
scala> a.take(4)
res107: Array[Int] = Array(1, 2, 3, 4)
takeRight
定义:def takeRight(n: Int): Array[T]
描述:返回当前序列中,从右边开始,后 n 个元素组成的序列
案例:
var a = Array(1, 2, 3, 4, 5, 6)
scala> a.takeRight(2)
res108: Array[Int] = Array(5, 6)
takeWhile
定义:def takeWhile(p: (T) ⇒ Boolean): Array[T]
描述:返回当前序列中,从第一个元素开始,满足条件的连续元素组成的序列
案例:
var a = Array(1, 2, 3, 4, 5, 6)
scala> a.takeWhile(_>4)//如果第一个不满足则返回空
res109: Array[Int] = Array()
scala> a.takeWhile(_<4)
res110: Array[Int] = Array(1, 2, 3)
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 类型
toVector
定义:def toVector: Vector[T]
描述:将序列转换成 Vector 类型
transpose
定义:def transpose[U](implicit asArray: (T) ⇒ Array[U]): Array[Array[U]]
描述:矩阵转置,二维数组行列转换
案例:
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
定义:def union(that: collection.Seq[T]): Array[T]
描述:合并两个序列,同操作符 ++
案例:
var a=Array(1,2,3)
var b=Array(4,"a")
scala> a.union(b)
res0: Array[Any] = Array(1, 2, 3, 4, a)
unzip
定义:def unzip[T1, T2](implicit asPair: (T) ⇒ (T1, T2), ct1: ClassTag[T1], ct2: ClassTag[T2]): (Array[T1], Array[T2])
描述:将含有两个二元组的数组,每个元组的第一个元素组成一个数组,第二个元素组成一个数组,返回包含这两个数组的元组
案例:
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
定义: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(("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
定义:def update(i: Int, x: T): Unit
描述:将序列中 i 索引处的元素更新为 x
案例:
scala> a
res111: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.update(3,99)
scala> a
res113: Array[Int] = Array(1, 2, 3, 99, 5, 6)
updated
定义:def updated(index: Int, elem: A): Array[A]
描述:将序列中 i 索引处的元素更新为 x,并返回替换后的数组
案例:
scala> a
res111: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.updated(3,88)
res114: Array[Int] = Array(1, 2, 3, 88, 5, 6)
view
定义:def view(from: Int, until: Int): IndexedSeqView[T, Array[T]]
描述:返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素
案例:
scala> a
res113: Array[Int] = Array(1, 2, 3, 99, 5, 6)
scala> a.view(2,4).foreach(println)
3
99
withFilter
定义:def withFilter(p: (T) ⇒ Boolean): FilterMonadic[T, Array[T]]
描述:根据条件 p 过滤元素
案例:
scala> a
res122: Array[Int] = Array(1, 2, 3, 99, 5, 6)
scala> a.withFilter(_<4).foreach(println)
1
2
3
zip
定义:def zip[B](that: GenIterable[B]): Array[(A, B)]
描述:将两个序列对应位置上的元素组成一个元组数组,要求两个序列长度相同
案例:
var a = Array(1,2,3,99,5,6)
var c = Array(3,41,4,6,7,45)
scala> a.zip(c)
res131: Array[(Int, Int)] = Array((1,3), (2,41), (3,4), (99,6), (5,7), (6,45))
zipAll(n,m,k)
定义:def zipAll[B](that: collection.Iterable[B], thisElem: A, thatElem: B): Array[(A, B)]
描述:同 zip ,但是允许两个序列长度不同,不足的自动填充,如果当前序列短,不足的元素填充为 thisElem,如果 that 序列短,填充为 thatElem
案例:
scala> a
res132: Array[Int] = Array(1, 2, 3, 99, 5, 6)
scala> b
res133: Array[Int] = Array()
scala> a.zipAll(b,2,"%") //b短用%补足
res134: Array[(Int, Any)] = Array((1,%), (2,%), (3,%), (99,%), (5,%), (6,%))
scala> b.zipAll(a,2,"%")//b短用2补足
res135: Array[(Int, Any)] = Array((2,1), (2,2), (2,3), (2,99), (2,5), (2,6))
zipWithIndex
定义:def zipWithIndex: Array[(A, Int)]
描述:序列中的每个元素和它的索引组成一个元组数组
案例:
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))