Scala 数组 集合 函数大全

数组、集合函数

++

说明: 把两个数组或集合,合并成一个大集合,谁在左边合成大集合的时候,元素放在前面。
返回值: 返回值是一个数组或者集合,也可以是数组和集合合并,返回值谁在左边,返回值就是谁的类型。
代码演示:

//创建两个数组
scala> var str1=Array(1,2,3)
str1: Array[Int] = Array(1, 2, 3)
scala> var  str2=Array(4,5)
str2: Array[Int] = Array(4, 5)

//两个数组合并第一个数组放前面
scala> str1++str2
res0: Array[Int] = Array(1, 2, 3, 4, 5)

//两个数组合并第二个数组放前面
scala> str2++str1
res1: Array[Int] = Array(4, 5, 1, 2, 3)

//创建两个集合
scala> var list1=List(2,4,6,8)
list1: List[Int] = List(2, 4, 6, 8)
scala> var list=List(1,3,5,7)
list: List[Int] = List(1, 3, 5, 7)

//两个集合相加
scala> list++list1
res2: List[Int] = List(1, 3, 5, 7, 2, 4, 6, 8)

//一个数组一个集合相加
scala> str1++list
res3: Array[Int] = Array(1, 2, 3, 1, 3, 5, 7)
++:

说明:++是一个作用,但是返回值不再是以左边的为准,而是以右边为准。
注:没有:++这个表达式
返回值: 数组或集合
代码演示:

scala> str1++:list
res4: List[Int] = List(1, 2, 3, 1, 3, 5, 7)
+:( :+)

说明: 将左边的数组或者集合作为右边的数组或集合的第一个元素,插入到右边数组或集合中。
返回值: 数组或者集合
代码演示:

scala> str1+:list
res6: List[Any] = List(Array(1, 2, 3), 1, 3, 5, 7)

scala> list+:str1
res7: Array[Any] = Array(List(1, 3, 5, 7), 1, 2, 3)
/:

说明: 对数组中所有的元素从左向右遍历,进行相同的迭代操作,是foldLeft 的简写
返回值: 是数组或集合中的元素类型。
代码演示:

//运算方式是:(((10+1)+2)+...)
scala> (10 /: str1 )(_+_)
res8: Int = 16

//创建集合
scala> val a=List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)

//代码演示过程
scala> val b=(10 /: a)((x,y)=>{println(x,y);x+y})
(10,1)
(11,2)
(13,3)
(16,4)
b: Int = 20
:\

说明: 对数组中所有的元素从右向左遍历,进行相同的迭代操作,是foldRight的简写
返回值: 是数组或集合中的元素类型。
代码演示:

scala> (str1 :\ 10 )(_+_)
res10: Int = 16

addString(str)

说明: 把数组或集合中的每个元素都添加到Stringbuilder中
返回值: StringBuilder
代码演示:

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

scala> str1.addString(str3)
res11: StringBuilder = 123

scala> println(str3)
123

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

scala> list.addString(str4)
res13: StringBuilder = 1357
aggregate

说明: 聚合计算,aggregate 是柯里化方法,参数是两个方法
返回值: 集合或者数组中的元素类型
代码演示:

//定义两个函数
scala> def f1(m:Int,n:Int):Int={
     | m+n
     | }
f1: (m: Int, n: Int)Int
scala> def f2(m:Int,n:Int):Int={
     | m+n
     | }
f2: (m: Int, n: Int)Int

//创建集合a
scala> val a= List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)

//不分区
//5 + 1
//6+2
//8+3
//11+4
scala> val b=a.aggregate(5)(f1,f2)
b: Int = 15

//分区
//5+1
//5+2
//5+3
//5+4
//6+7
//8+9
//13+17
//par表示分区
scala> val b=a.par.aggregate(5)(f1,f2)
b: Int = 30
andThen

说明: 使用Scalaz,一个函数可以映射到另一个函数上。
**返回值:**与函数相关
代码演示:

//定义两个方法
scala> val f:Int => Int = (a) => a+10
f: Int => Int = <function1>
scala> val g:Int => Int = (a) =>a*100
g: Int => Int = <function1>

//方法演示
//从左向右,把10放入f中算出结果放入g中
scala> (f andThen g)(10)
res17: Int = 2000

//相当于
scala> g(f(10))
res19: Int = 2000
apply

说明: 获取指定索引处的元素
返回值: 返回值值所索引数组或集合的元素类型
代码演示:

scala> list.apply(1)
res23: Int = 3
applyOrElse

说明: 它接收2个参数,第一个是调用的参数,第二个是个回调函数。如果第一个调用的参数匹配,返回匹配的值,否则调用回调函数。
代码演示:

//applyOrElse的第一个参数是对应数组的下标,有就返回参数,没有回调函数
scala> list.applyOrElse(7,{num:Int => "false"})
res24: Any = false

scala> list.applyOrElse(1,{num:Int => "false"})
res25: Any = 3

scala> str1.applyOrElse(1,{num:Int => "false"})
res26: Any = 2
array

说明: 显示数组地址
代码演示:

scala> println(str1.array)
[I@6c75b16b
canEqual

说明: 判断两个对象是否可以进行比较
返回值: boolean值(基本来说都是true,因为对象基本都可比较)
代码演示:

scala> list.canEqual(str1)
res33: Boolean = true
clone

说明: 创建一个数组的副本
代码演示

//数组可以创建副本
scala> val strcopy=str1.clone()
strcopy: Array[Int] = Array(1, 2, 3)

//集合不可以创建副本(报错)
scala> val listcopy=list.clone()
<console>:12: error: method clone in class Object cannot be accessed in List[Int]
 Access to protected method clone not permitted because
 prefix type List[Int] does not conform to
 object $iw where the access take place
       val listcopy=list.clone()
collect

说明: 通过执行一个并行计算(偏函数),得到一个新的数组对象
返回值: 一个新的数组对象
代码演示:

scala> val fun:PartialFunction[Int,Int]={
     | case 1 => 10
     | case 2 => 20
     | case 3 => 30
     | }
fun: PartialFunction[Int,Int] = <function1>

scala> val b=str1.collect(fun)
b: Array[Int] = Array(10, 20, 30)

scala> val c=list.collect(fun)
c: List[Int] = List(10, 30)
collectFirst

说明: 在序列中查找第一个符合偏函数定义的元素,并执行偏函数计算
代码演示:

scala> val c=list.collectFirst(fun)
c: Option[Int] = Some(10)

scala> val c=str1.collectFirst(fun)
c: Option[Int] = Some(10)
combinations

说明: 讲一个数组中随机抽几个元素组合在一起,输出不相同的组合,不考虑前后顺序
代码演示:

scala> str1.combinations(2).foreach(x => println(x.mkString(",")+""))
1,2
1,3
2,3
compose
contains

说明: 查看数组或者集合中是否存在该元素,一般与filter连用。
返回值: Boolean值
代码演示:

//创建一个数组一个集合
scala> val arr=Array(1,2,3,4,5)
arr: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val list=Array(1,3,5,7,9)
list: Array[Int] = Array(1, 3, 5, 7, 9)

//查看数组或者集合中是否存在目标元素
scala> arr.contains(1)
res0: Boolean = true
scala> list.contains(1)
res2: Boolean = true
containsSlice

说明: 查看当前序列是否包含另一个序列,不区分数组与集合,Array也可以查看list
返回值: Boolean值
代码演示:

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

scala> arr.containsSlice(list1)
res5: Boolean = true
copyToArray(xs) / copyToArray(xs,start) / copyToArray(xs,start,len)

说明:
copyToArray(xs):将当前数组元素复制到另一个数组中
copyToArray(xs,start):将当前数组元素复制到另一个数组中,从 start 位置开始复制
copyToArray(xs,start,len):将当前数组元素复制到另一个数组中,从 start 位置开始复制,长度为 len
返回值: 数字或集合
代码演示:

//仅有一个参数
scala> val b=new Array[Int](5)
b: Array[Int] = Array(0, 0, 0, 0, 0)

scala> arr.copyToArray(b)

scala> b.foreach(println)
1
2
3
4
5

//有两个参数,确定从哪开始复制
scala> val b=new Array[Int](5)
b: Array[Int] = Array(0, 0, 0, 0, 0)

scala> arr.copyToArray(b,2)

scala> b.foreach(println)
0
0
1
2
3

//三个参数,从哪开始赋值,到哪里结束
scala> val b=new Array[Int](5)
b: Array[Int] = Array(0, 0, 0, 0, 0)

scala> arr.copyToArray(b,2,2)

scala> b.foreach(println)
0
0
1
2
0
copyToBuffer

说明: 将数组中的元素复制到 Buffer 中
代码演示:

//导入可变集合包
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

//创建ArrayBuffer对象
//从a中复制到b中
scala> var b=ArrayBuffer[Int]()
b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> a.copyToBuffer(b)

scala> b.foreach(println)
1
2
3
corresponds

说明: 判断两个序列的长度以及对应位置元素是否符合某个条件。如果两个序列具有相同的元素数量并且 println(x, y)=true,则返回 true
返回值: boolean
代码演示:

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

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

scala> println(a.corresponds(b)(_<_))
true
count

说明: 统计数组或者集合中符合条件的元素个数
返回值: Int类型
代码演示

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

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

scala> a.count(_>2)
res1: Int = 2

scala> b.count(_>2)
res2: Int = 2
deep

说明: 返回IndexedSeq的类型的数组
代码演示

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

scala> c.deep
res4: IndexedSeq[Any] = Array(2, 4, 3, 1)
diff

说明 计算当前数组与另一个数组的差集,即将当前数组中没有在另一个数组中出现的元素返回
返回值 数组或集合
代码演示:

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

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

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

说明 去除数组或元素中重复出现的元素
返回值 数组或集合
代码演示

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

scala> c.distinct
res10: Array[Int] = Array(1, 2, 3, 4)
drop

说明 删除数组或集合中前n个元素
返回值 数组或集合
代码演示

scala> a.drop(1)
res12: Array[Int] = Array(2, 3, 4)

scala> a.drop(5)
res13: Array[Int] = Array()
dropRight

说明 从数组或集合的右边删除n个元素
返回值 数组或集合
代码演示

scala> a.dropRight(1)
res16: Array[Int] = Array(1, 2, 3)
dropWhile

说明 从左向右删除满足条件的元素,知道不满足停止
返回值 数组或集合
代码演示

//因为第一个就不符合条件,所以一个都没有删除
scala> a.dropWhile(_>2)
res17: Array[Int] = Array(1, 2, 3, 4)

scala> a.dropWhile(_<3)
res18: Array[Int] = Array(3, 4)
endsWith

说明 判断数组或集合的末尾是否是相应的序列
返回值 boolean值
代码演示

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

scala> a.endsWith(b)
res20: Boolean = true
exists

说明 判断数组或集合中是否存在符合条件的相应的元素
返回值 boolean值
代码演示

scala> a.exists(_==2)
res23: Boolean = true
filter

说明 遍历数组或集合过滤出符合条件的元素
返回值 数组或集合
代码演示

scala> a.filter(_>1)
res24: Array[Int] = Array(2, 3, 4)
filterNot

说明 遍历数组或集合过滤出不符合条件的元素
返回值 数组或集合
代码演示

scala> a.filterNot(_>1)
res25: Array[Int] = Array(1)
find

说明 找到第一个符合条件的元素
返回值 Some类型
代码演示

scala> a.find(_>2)
res26: Option[Int] = Some(3)
flatMap

说明 先进行map操作,在进行flatten操作
代码演示

//创建数组
scala> val str=Array("hello world","hello java","hello scala","hello spark")
str: Array[String] = Array(hello world, hello java, hello scala, hello spark)

//用flatMap
scala> str.flatMap(_.split(" "))
res43: Array[String] = Array(hello, world, hello, java, hello, scala, hello, spark)

//先用map后用flatten
scala> str.map(_.split(" "))
res44: Array[Array[String]] = Array(Array(hello, world), Array(hello, java), Array(hello, scala), Array(hello, spark))
scala> str.map(_.split(" ")).flatten
res45: Array[String] = Array(hello, world, hello, java, hello, scala, hello, spark)
flatten

说明 扁平化,将数组或集合内部是集合或数组的元素全部打散编程元素(扁平化)
返回值 数组或集合
案例演示

scala> str.flatten
res46: Array[String] = Array(hello world, hello java, hello scala, hello spark)
fold

说明 对数组或集合中的每个元素进行二元运算
代码演示

scala> a.fold(5)(_+_)
res49: Int = 15
foldLeft

说明 和foldLeft一样

foldRight

说明 对数组或集合的每个元素从右到左进行二元运算
代码演示

scala> a.foldRight(2)(_+_)
res50: Int = 12
forall

说明 检测数组或集合中是否每个元素都符合相应的条件
返回值 boolean
代码演示

scala> a.forall(_<5)
res51: Boolean = true
foreach

说明 遍历序列中的元素,进行操作
代码演示

scala> a.foreach(x => println(x*10))
10
20
30
40
groupBy

说明 按条件分组,条件由括号内函数匹配,返回值是 Map 类型,每个 key 对应一个数组
代码演示

scala> a.groupBy(_>2)
res53: scala.collection.immutable.Map[Boolean,Array[Int]] = Map(false -> Array(1, 2), true -> Array(3, 4))
grouped

说明 按指定数量分组,每组有 size 个元素,返回一个迭代器
代码演示

scala> a.grouped(2)
res55: Iterator[Array[Int]] = non-empty iterator

scala> res55.foreach(x => println(x.mkString(",")))
1,2
3,4
hasDefiniteSize

说明 判断该数组或者集合是否存在有限长度
返回值 boolean
代码演示

scala> a.hasDefiniteSize
res57: Boolean = true
head

说明 返回序列的第一个元素,如果序列为空,将引发错误
返回值 第一个元素的类型
代码演示

scala> a.head
res58: Int = 1
headOption

说明 和head一样
返回值 Option类型
代码演示

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

说明 返回相应元素所在下标
返回值 Int类型
代码演示

scala> a.indexOf(1)
res61: Int = 0
indexOfSlice(that / that ,from)

说明 返回元素 elem 在序列中第一次出现的索引,指定从索引 from 开始查找
返回值类型 Int类型
代码演示

scala> b
res63: Array[Int] = Array(3, 4)

scala> a.indexOf
indexOf   indexOfSlice

scala> a.indexOfSlice(b)
res64: Int = 2
indexWhere(p / p,from)

说明 返回当前序列中第一个满足条件 p 的元素的索引
返回当前序列中第一个满足条件 p 的元素的索引,指定从索引 from 开始查找
返回值 Int类型
代码演示

scala> a.indexWhere(_>1,2)
res66: Int = 2

scala> a.indexWhere(_>1)
res67: Int = 1
indices

说明 返回数组下标集合
返回值 Range类型
代码演示

scala> a.indices
res68: scala.collection.immutable.Range = Range(0, 1, 2, 3)
init

说明 去除数组或集合中第一个元素,并返回新的数组
返回值 数组类型
代码演示

scala> a.init
res69: Array[Int] = Array(1, 2, 3)
inits

说明 对集合中的元素进行 init 迭代操作,该操作的返回值中, 第一个值是当前序列的副本,最后一个值为空,每一步都进行 init 操作,上一步的结果作为下一步的操作对象
代码演示

scala> a.inits
res70: Iterator[Array[Int]] = non-empty iterator

scala> res70.foreach(x => println(x.mkString(",")))
1,2,3,4
1,2,3
1,2
1
intersect

说明 取交集
返回值 集合或数组
代码演示

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

scala> a.intersect(b)
res72: Array[Int] = Array(3, 4)
isDefinedAt

说明 判断序列中是否存在指定索引
返回值 boolean
代码演示

scala> a.isDefinedAt(2)
res73: Boolean = true

scala> a.isDefinedAt(5)
res74: Boolean = false
isEmpty

说明 判断数组或集合中是否是空的
返回值 boolean
代码演示

scala> a.isEmpty
res75: Boolean = false
isTraversableAgain

说明 判断序列是否可以反复遍历
返回值 boolean类型
代码演示

scala> a.isTraversableAgain
res76: Boolean = true
iterator

说明 生成当前序列的迭代器
返回值 迭代器
代码演示

scala> a.iterator
res77: Iterator[Int] = non-empty iterator
last

说明 返回序列的最后一个元素,如果序列为空,将引发错误
返回值 当前数组或集合的最后一个元素的类型
代码演示

scala> a.last
res78: Int = 4
lastIndexOf

说明 返回元素 elem 在序列中最后一次出现的索引
返回值 Int类型
代码演示

scala> a.lastIndexOf(2)
res79: Int = 1
lastIndexOf(elem, end)

说明 返回元素 elem 在序列中最后一次出现的索引,指定在索引 end 之前(包括)的元素中查找
返回值 Int类型
代码演示

scala> val a = Array(1, 3, 2, 3, 4)
scala> println(a.lastIndexOf(3, 2))
1
lastIndexWhere

说明 返回当前序列中最后一个满足条件 p 的元素的索引
返回值 Int
代码演示

scala> a.lastIndexWhere(_>2)
res81: Int = 3
lastIndexWhere(p, end)

说明 返回当前序列中最后一个满足条件 p 的元素的索引,指定在索引 end 之前(包括)的元素中查找
返回值 Int类型
代码演示

scala> a.lastIndexWhere(_>2,2)
res83: Int = 2
lastOption

说明 和last基本一样
返回类型 Option
代码演示

scala> a.lastOption
res84: Option[Int] = Some(4)
length

说明 计算数组或集合长度
返回值 Int类型
代码演示

scala> a.length
res85: Int = 4
lengthCompare

说明 比较序列的长度和参数 len,返回序列的长度 - len
返回值 Int类型
代码演示

scala> a.lengthCompare(2)
res86: Int = 2
map

说明 对序列中的元素进行 f 操作,返回生成的新序列
代码演示

scala> a.map(_*10)
res87: Array[Int] = Array(10, 20, 30, 40)
max

说明 返回序列中最大的元素
返回值 返回数值型数据类型
代码演示

scala> val d=Array(1.1,2.3,4.5,3.2)
d: Array[Double] = Array(1.1, 2.3, 4.5, 3.2)

scala> d.max
res88: Double = 4.5
maxBy

说明 返回序列中符合条件的第一个元素
代码演示

scala> a.maxBy(_>2)
res91: Int = 3
min

说明 返回序列中最小的元素
代码演示

scala> a.min
res92: Int = 1
minBy

说明 返回序列中不符合条件的第一个元素
代码演示

scala> a.minBy(_>2)
res93: Int = 1
mkString

说明 将序列中所有元素拼接成一个字符串
返回值 String类型
代码演示

scala> a.mkString
res95: String = 1234
mkString(sep)

说明 将序列中所有元素拼接成一个字符串,以 sep 作为元素间的分隔符
返回值 String类型
代码演示

scala> a.mkString(",")
res96: String = 1,2,3,4
mkString(start, sep, end)

说明 将序列中所有元素拼接成一个字符串,以 start 开头,以 sep 作为元素间的分隔符,以 end 结尾
返回值 String类型
代码演示

scala> a.mkString("(",",",")")
res97: String = (1,2,3,4)
nonEmpty

说明 判断序列是否不为空
返回值 Boolean类型
代码演示

scala> a.nonEmpty
res98: Boolean = true
padTo

说明 填充序列,如果当前序列长度小于 len,那么新产生的序列长度是 len,多出的几个位值填充 elem,如果当前序列大于等于 len ,则返回当前序列
代码演示

scala> a.padTo(7,0)
res100: Array[Int] = Array(1, 2, 3, 4, 0, 0, 0)
par

说明 返回一个并行实现,产生的并行序列不能被修改
代码演示

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

说明 按条件将序列拆分成两个数组,满足条件的放到第一个数组,其余的放到第二个数组,返回的是包含这两个数组的元组
代码演示

scala> a.partition(_>2)
res102: (Array[Int], Array[Int]) = (Array(3, 4),Array(1, 2))
patch

说明 批量替换,从原序列的 from 处开始,后面的 replaced 个元素,将被替换成序列 that
代码演示

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

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

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

说明 permutations 表示排列,这个排列组合会选出所有排列顺序不同的字符组合,permutations 与 combinations 不同的是,相同的组合考虑排列,对于 “abc”、“cba”,视为不同的组合
代码演示

scala> a.permutations
res106: Iterator[Array[Int]] = non-empty iterator

scala> res106.foreach(x  => println(x.mkString(",")))
1,2,3,4
1,2,4,3
1,3,2,4
1,3,4,2
1,4,2,3
1,4,3,2
2,1,3,4
2,1,4,3
2,3,1,4
2,3,4,1
2,4,1,3
2,4,3,1
3,1,2,4
3,1,4,2
3,2,1,4
3,2,4,1
3,4,1,2
3,4,2,1
4,1,2,3
4,1,3,2
4,2,1,3
4,2,3,1
4,3,1,2
4,3,2,1
prefixLength

说明 给定一个条件 p,返回一个前置数列的长度,这个数列中的元素都满足 p
代码演示

scala> a.prefixLength(_<3)
res109: Int = 2
product

说明 返回所有元素乘积的值
代码演示

scala> a.product
res110: Int = 24
reduce

说明 同 fold,不需要初始值
代码演示

scala> a.reduce(_+_)
res111: Int = 10
reduceLeft

说明 和reduce效果一样都是从左向右。
代码演示

scala> a.reduceLeft(_+_)
res112: Int = 10
reduceRight

说明 和reduce效果一样都是从右向左。
代码演示

scala> a.reduceRight(_+_)
res113: Int = 10
reduceLeftOption

说明 同 reduceLeft,返回 Option
代码演示

scala> a.reduceLeftOption(_+_)
res114: Option[Int] = Some(10)
reduceRightOption

说明 同 reduceRight,返回 Option
代码演示

scala> a.reduceRightOption(_+_)
res115: Option[Int] = Some(10)
reverse

说明 反转序列
代码演示

scala> a.reverse
res116: Array[Int] = Array(4, 3, 2, 1)
reverseIterator

说明 生成反向迭代器
代码演示

scala> res126.foreach(println)
4
3
2
1
reverseMap

说明 同 map,方向相反
代码演示

scala> a.reverseMap(_*10)
res128: Array[Int] = Array(40, 30, 20, 10)
sameElements

说明 判断两个序列是否顺序和对应位置上的元素都一样
返回值 boolean值类型
代码演示

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

scala> a.sameElements(b)
res129: Boolean = true
scan

说明 同 fold,scan 会把每一步的计算结果放到一个新的集合中返回,而 fold 返回的是最后的结果
返回值 数组
代码演示

scala> a.scan(0)(_+_)
res131: Array[Int] = Array(0, 1, 3, 6, 10)
scanLeft

说明 和scan基本一样,都是从左向右
返回值 数组
代码演示

scala> a.scanLeft(0)(_+_)
res132: Array[Int] = Array(0, 1, 3, 6, 10)
scanRight

说明 和scan方向相反
返回值 数组
代码演示

scala> a.scanRight(0)(_+_)
res133: Array[Int] = Array(10, 9, 7, 4, 0)
segmentLength

说明 从序列的 from 开始向后查找,返回满足条件 p 的连续元素的长度,只返回第一个
返回值 Int类型
代码演示

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

scala> c.segmentLength(_<3,3)
res135: Int = 3
seq

说明 产生一个引用当前序列的 sequential 视图
返回值 数组或集合
代码演示

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

说明 返回序列元素个数,同 length
返回值 Int类型
代码演示

slice

说明 返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素
代码演示

scala> a.slice(1,3)
res140: Array[Int] = Array(2, 3)
sliding(size /size,step)

说明 滑动,从第一个元素开始,每个元素和它后面的 size - 1 个元素组成一个数组,最终组成一个新的集合返回,当剩余元素个数不够 size 时,则结束
代码演示

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

scala> res141.foreach(x => println(x.mkString(",")))
1,2,3
2,3,4
3,4,5
sortBy

说明 按指定的排序规则对序列排序
代码演示

scala> a.sortBy(x => x)
res144: Array[Int] = Array(1, 2, 3, 4, 5)

scala> a.sortBy(x => -x)
res145: Array[Int] = Array(5, 4, 3, 2, 1)
sorted

说明 使用默认的排序规则对序列排序
代码演示

scala> a.sorted
res146: Array[Int] = Array(1, 2, 3, 4, 5)
span

说明 将序列拆分成两个数组,从第一个元素开始,直到第一个不满足条件的元素为止,其中的元素放到第一个数组,其余的放到第二个数组,返回的是包含这两个数组的元组
代码演示

scala> a.span(_<3)
res147: (Array[Int], Array[Int]) = (Array(1, 2),Array(3, 4, 5))
splitAt

说明 从指定位置开始,把序列拆分成两个数组
代码演示

scala> a.splitAt(2)
res148: (Array[Int], Array[Int]) = (Array(1, 2),Array(3, 4))
startsWith(that)

说明 判断序列是否以某个序列开始
代码演示

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

scala> a.startsWith(b)
res151: Boolean = true
startsWith(that, offset)

说明 判断序列从指定偏移处是否以某个序列开始
代码演示

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

scala> a.startsWith(b,1)
res152: Boolean = true
stringPrefix

说明 返回 toString 结果的前缀
代码演示

scala> a.stringPrefix
res153: String = [I
sum

说明 序列求和,元素需为 Numeric[T] 类型
代码演示

scala> a.sum
res154: Int = 10
tail

说明 返回当前序列中不包含第一个元素的序列
代码演示

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

说明 同 inits,每一步都进行 tail 操作
代码演示

scala> a.tails
res156: Iterator[Array[Int]] = non-empty iterator

scala> res156.foreach(x => println(x.mkString(",")))
1,2,3,4
2,3,4
3,4
4
take

说明 返回当前序列中,前 n 个元素组成的序列
返回值 数组
代码演示

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

说明 返回当前序列中,从右边开始,后 n 个元素组成的序列
返回值 数组
代码演示

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

说明 返回当前序列中,从第一个元素开始,满足条件的连续元素组成的序列
返回值 数组
代码演示

scala> a.takeWhile(_<2)
res160: Array[Int] = Array(1)
toArray

说明 将序列转换成 Array 类型
代码演示

scala> a.toArray
res161: Array[Int] = Array(1, 2, 3, 4)
toBuffer

说明 将序列转换成 IndexedSeq 类型
代码演示

scala> a.toBuffer
res162: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4)
toIndexedSeq

说明 将序列转换成 IndexedSeq 类型
代码演示

scala> a.toIndexedSeq
res163: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4)
toIterable

说明 将序列转换成可迭代的类型
代码演示

scala> a.toIterable
res164: Iterable[Int] = WrappedArray(1, 2, 3, 4)
toIterator

说明 将序列转换成迭代器,同 iterator 方法
代码演示

scala> a.toIterator
res165: Iterator[Int] = non-empty iterator
toList

说明 将序列转换成 List 类型
代码演示

scala> a.toList
res166: List[Int] = List(1, 2, 3, 4)
toMap

说明 将序列转转换成 Map 类型,需要被转化序列中包含的元素是 Tuple2 类型

toSeq

说明 将序列转换成 Seq 类型
代码演示

scala> a.toSeq
res169: Seq[Int] = WrappedArray(1, 2, 3, 4)
toSet

说明 将序列转换成 Set 类型
代码演示

scala> a.toSet
res170: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
toStream

说明 将序列转换成 Stream 类型
代码演示

scala> a.toStream
res171: scala.collection.immutable.Stream[Int] = Stream(1, ?)
toVector

说明 将序列转换成 Vector 类型
代码演示

scala> a.toVector
res172: Vector[Int] = Vector(1, 2, 3, 4)
transpose

说明 矩阵转置,二维数组行列转换
代码演示

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

说明 合并两个序列,同操作符 ++
代码演示

scala> a.union(b)
res173: Array[Int] = Array(1, 2, 3, 4, 2, 3)
unzip

说明 将含有两个二元组的数组,每个元组的第一个元素组成一个数组,第二个元素组成一个数组,返回包含这两个数组的元组
代码演示

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

scala> m.unzip
res175: (Array[Int], Array[Int]) = (Array(1, 3),Array(2, 4))
unzip3

说明 将含有三个三元组的数组,每个元组的第一个元素组成一个数组,第二个元素组成一个数组,第三个元素组成一个数组,返回包含这三个数组的元组
代码演示

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

scala> m.unzip3
res176: (Array[Int], Array[Int], Array[Int]) = (Array(1, 3),Array(2, 4),Array(3, 5))
update

说明 将序列中 i 索引处的元素更新为 x
代码演示

scala> a.update(1,7)

scala> a
res178: Array[Int] = Array(1, 7, 3, 4)
updated

说明 将序列中 i 索引处的元素更新为 x,并返回替换后的数组
代码演示

scala> a.updated(1,6)
res180: Array[Int] = Array(1, 6, 3, 4)
view

说明 返回当前序列中从 from 到 until 之间的序列,不包括 until 处的元素
代码演示

scala> a.view
res181: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqView(...)

scala> a.view(1,3).mkString(",")
res184: String = 2,3
withFilter

说明 根据条件 p 过滤元素
代码演示

scala> a.withFilter(_>2).map(x => x).mkString
res189: String = 34
zip

说明 将两个序列对应位置上的元素组成一个元组数组,要求两个序列长度相同
代码演示

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

scala> a.zip(b)
res191: Array[(Int, Int)] = Array((1,4), (2,3), (3,2), (4,1))
zipAll

说明 同 zip ,但是允许两个序列长度不同,不足的自动填充,如果当前序列短,不足的元素填充为 thisElem,如果 that 序列短,填充为 thatElem
代码演示

scala> a.zipAll(b,8,9)
res195: Array[(Int, Int)] = Array((1,4), (2,3), (3,2), (4,9))
zipWithIndex

说明 序列中的每个元素和它的索引组成一个元组数组
代码解释

scala> a.zipWithIndex
res196: Array[(Int, Int)] = Array((1,0), (2,1), (3,2), (4,3))
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值