首页 > 其他 > 详细

scala基础函数

时间:2020-09-16 23:23:25      阅读:49      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

clone(创建副本)
val chars = Array(‘a‘,‘b‘,‘c‘)
println(a.apply(2))
Int = 3
val newchars = chars.clone()
结果输出:
Array[Char] = Array(‘a‘,‘b‘,‘c‘)
==============================
++ (拼接数值)
var  e = Array(1,2,3,4)
var  k = Array(5,6,7,8)
e++k = Array(1,2,3,4,5,6,7,8)
或者e.++(k)
++: (前后拼接数值)
var  e = Array(1,2,3,4)
var  k = Array(5,6,7,8)
k++:e = Array[Int](5,6,7,8,1,2,3,4)
e++:k = Array[Int](1,2,3,4,5,6,7,8)
==============================
+:(前后拼接单值,:在前表示数值在前)
var  e = Array(1,2,3,4)
24+:k = Array[Int] = Array(24,1,2,3,4)
k:+24 = Array[Int] = Array(1,2,3,4,24)
==============================
/:(左二叉树)
val y = Array(1,2,3)
(88 /: y) (_+_)
结果输出
Int = 94
==============================
addString (拼接字符)
var g = new StringBuilder
var  k = Array(5,6,7,8)
k.addString(g)
StringBuilder = 5678
k.addString(g,"1",",","2")
结果输出
StringBuilder = 567815,6,7,82
==============================
map
var  e = Array(1,2,3,4)
e.map(x=>x+2)
结果输出
Array[Int] = Array(3,4,5,6)
==============================
def (创建函数)
def ab(x:Int):Int = {
x+2
}
abc:(x: Int)Int
abc(10)
Int = 12
Int 代表返回值
或写成
k.map(abc)
==============================
compose(先算右侧,再算左侧)
def f(a:Int) = 2*a
def g(b:Int) = 3*b
def fComposeg = f _ compose g _
fComposeg(1)
结果输出
Int = 6
==============================
apply (取出指定索引处的元素)
var a = Array(1,2,3)
var b = a.apply(2) //下标位置
结果输出
Int = 3
==============================
偏函数
collect
val a = Array(1,2,3,4)
val func:PartialFunction[Int,Int]={
case x if x%2==0 => x+2
case x => x+1 //case _ => 1 _只能用一次
}
a.collect(func) //collect调用偏函数
结果输出
Array[Int] = Array(2, 4, 4, 6)
==============================
collectFirst
val a = Array(1,2,3,4)
val func:PartialFunction[Int,Int]={
case x if x%2==0 => x+2
}
a.collect(func)
结果输出
Array[Int] = Array(4, 6)
a.collectFirst(func) //取出第一个符合条件的数值进行操作
结果输出
Option[Int] = Some(4)
a.collectFirst(func).get
some的get方法取值
Int = 4
==============================
combinations
val a = Array(3,5,62,34,6,6,8)
a.combinations(2)
res12: Iterator[Array[Int]] = non-empty iterator
res12.foreach(x => println(x.mkString(",")))//新生成迭代器,只能用一次
结果输出:
3,5
3,62
3,34
3,6
3,8
5,62
5,34
5,6
5,8
62,34
62,6
62,8
34,6
34,8
6,6
6,8
==============================
contains(包含某个对象)
a.contains(5)
boolean = false
a.contains(3)
boolean = true
val b = Array(1,2,3,Array(4,5))
val c = Array(4,5,6)
var c:Array[Any] = Array(4,5)
b.containsSlice(c) //包含某个数组
boolean = false
val a = List(1,2,3)
val b = List(2,3)
a.containsSlice(b)
boolean = true
==============================
copyToArray(括号中表示拷贝的第一个数值;拷贝原数组所有数值并放到新数组中的第n下标;拷贝的数值长度)
val a = Array(‘a‘,‘b‘,‘c‘)
val b : Array[Char] = new Array(5)
a.copyToArray(b)  //结果输出:[a,b,c,0,0]
a.copyToArray(b,1) //结果输出:[0,a,0,0,0]
a.copyToArray(b,1,2) //结果输出:[0,a,b,0,0,0]
=============================
copyToBuffer(将数组中的内容拷贝到Buffer中)
import scala.collection.mutable.ArrayBuffer  //导包
val a = Array(1,2,3)
val b : ArrayBuffer[Int] = ArrayBuffer()
a.copyToBuffer(b)
结果输出:scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
println(b.mkString(",")) //取值1,2,3
=============================
corresponds(判断两个数组长度及对应位置元素是否符合某个条件,符合返回true,否则false)
val a = List(1,2,3)
val b = List(2,3)
a.corresponds(b)(_<_)
输出结果:
boolean = false
val c = Array(1,2,3)
a.corresponds(c)(_==_)
输出结果:
boolean = true
=============================
count (满足条件的的数值的和)
val a = Array(1,2,3)
a.count(x => x%2==0)   //=> 莱姆他表达式
输出结果:
Int = 1
val e = Array("hello","world","hadoop")
e.count(x => x.contains("o"))
输出结果:
Int = 3
val e = Array("hello","world","hadoop")
e.count(x => x.indexOf("o") > -1)
输出结果:
Int = 3
=============================
diff(计算当前数组与另一数组的不同,并将当前数组中与另一数组中的不同元素返回)
val a = Array(1,2,3,4)
val b = Array(4,5,6,7)
val c = a.diff(b)
此刻数组c中的数值为(1,2,3)
println(c.mkString(",")) //取值
输出结果:1,2,3
=============================
distinct(去除当前数组中重复的元素,只保留一个)
val a = List(1,2,3)
1+:a
res49:List[Int] = List(1,1,2,3)
res49.distinct
输出结果:
List[Int] = List(1,2,3)
=============================
drop(将当前数组中前n个元素删除)
val a = List(1,2,3)
a.drop(1)
List[Int] = List(2,3)
=============================
dropRight(删除当前数组中右侧后n个元素)
a.dropRight(1)
List[Int] = List(1,2)
=============================
dropWhile(去除当前数组中符合条件的元素,直到遇到第一个不满足条件的元素结束)
val a = Array(5, 6, 4, 1, 6, 7)
a.dropWhile(x => x<3)
结果输出:
Array[Int] = Array(3)
val a = Array(5, 6, 4, 1, 6, 7)
a.dropWhile(x => x>3) //a.dropWhile(_>2)
Array[Int] = Array(1, 2, 3) //返回下标
=============================
filter(取出当前数组中符合条件的元素,组成新数组返回)
val a = Array(1,2,3)
a.filter(_>2)
Array[Int] = Array(3)
=============================
endsWith/startsWith(判断是否以另一数组为结尾/起始)
val a = Arrray(2,1)
val b = Array(3,2,1)
b.endsWith(a)
输出结果:
boolean = true
val a = Arrray(2,1)
val b = Array(2,1,1)
b,startsWith(a)
输出结果:
boolean = true
=============================
exists(判断当前数组是否包含符合条件的元素)
val a = List(1,2,3)
a.exists(_>2)
输出结果:
boolean = true
val a = List(1,2,3)
a.exists(_>3)
输出结果:
boolean = false
=============================
filterNot(将当前数组中不符合某条件的数组取出并组成新的数组)
val a = List(1,2,3)
a.filterNot(_>2)
输出结果:
List[Int] = List(1,2)
=============================
find(取出当前数组中第一个符合条件的元素)
val a = List(1,2,3)
a.find(_>1)
输出结果:
Option[Int] = Some(2)
取值:
a.find(_>1).get
Int = 2
=============================
flatten(将二维数组中的所有元素联结,并返回一个新的一维数组) //一维+二维不可降
val g = List(List(1,2,3),List(4,5))
g.flatten
结果输出:
List[Int] = List(1,2,3,4,5)
val g = List(List(1,2,3),List(4,5),List(List(7,8),List(9,10))
a.flatten.flatten
结果输出:
List[Int] = List(1,2,3,4,5,6,7,8,9,10)
=============================
flatMap(对当前数组中的每个元素进行操作,结果放入新数组返回)
val a = Array(1,2,3,4)
val b = a.flatMap(x => 1 to x)
println(b.mkString(","))
结果输出:
1,1,2,1,2,3,1,2,3,4
=============================
aggregate(对当前数组中的每个元素进行二元操作,其中aggregates必须包含两个参数)
def i(m:Int,n:Int):Int ={
val s = "com=%d+%d"
println(s.format(m,n))
return m+n
}
def t(m:Int,n:Int):Int ={
val s = "com=%d+%d"
println(s.format(m,n))
return m+n
}
scala> val b = a.par.aggregate(5)(i,t)
/**运算过程
com=5+1
com=5+2
com=5+4
com=5+3
com=6+7
com=8+9
com=13+17
b: Int = 30
=============================
fold/foldleft/foldright(对当前数组中的每个元素进行二元操作)
fold
def i(m:Int,n:Int):Int ={
val s = "com=%d+%d"
println(s.format(m,n))
return m+n
}
val a = Array(1,2,3,4)
val b = a.fold(5)(i)
/**运算过程
com=5+1
com=6+2
com=8+3
com=11+4
=======
foldleft
def i(m:Int,n:Int):Int ={
val s = "com=%d+%d"
println(s.format(m,n))
return m+n
}
val a = Array(1,2,3,4)
val b = a.fold(5)(i)
/**运算过程
com=5+1
com=6+2
com=8+3
com=11+4
=======
foldright
def i(m:Int,n:Int):Int ={
val s = "com=%d+%d"
println(s.format(m,n))
return m+n
}
val a = Array(1,2,3,4)
val b = a.fold(5)(i)
/**运算过程
com=4+5
com=3+9
com=2+12
com=1+14
=============================
forall(检查序列中是否全部都满足条件,满足则返回true)
val a = Array(1,2,3,4)
a.forall(_<0)
boolean = true
a.forall(_2)
boolean = false
=============================
foreach(遍历数组中的元素,进行某项操作)
val a = Array(1,2,3,4)
a.foreach(x => println(x*10)
输出结果:
10
20
30
40
=============================
groupBy(按条件进行分组,返回值是Map类型,将当前数组中的奇数与偶数分组)
val a = Array(1,2,3,4)
val b = a.groupBy(x => x match{
case x if (x%2==0) => "ou"
case _ => "ji"
});
输出结果:
Map[String,Array[Int]] = Map(ou -> Array(2,4), ji -> Array(1,3))
取值:
res0.apply("ou")
res1:Array[Int] = Array(2,4)
res0.apply("ji")
res1:Array[Int] = Array(1,3)
res0.get("ou").get
Option[Array[Int]] = Some([I@78b9d614) //地址
res0.get("ji").get
res37: Option[Array[Int]] = Some([I@3d48674)
=============================
grouped(按指定数量分组,每组有size数量个元素,并返回一个集合,以下是两两进行分组)
val a = Array(1,2,3,4)
a.grouped(2).foreach(x => println(x.mkString(",")))
结果输出
1,2
3,4
=============================
hasDefiniteSize(判断当前数组是否存在有限的长度,对应Stream这样的流数据)
val a = Array(1,2,3,4)
println(a.hasDefiniteSize)
结果输出:
boolean = true
=============================
head/tail/tails(head空数组报错)
val a = Array(1,2,3,4)
a.head
结果输出:
Int = 1
==========
tail
val a = Array(1,2,3,4)
a.tail
结果输出:
Int = 2,3,4
=============================
tails(对数组进行循环遍历,逐次拿到最后的值)
val a = Array(4, 5, 6, 3, 5, 7)
a.tails.foreach(x => println(x.mkString(",")))
结果输出:
4,5,6,3,5,7
5,6,3,5,7
6,3,5,7
3,5,7
5,7
7
=============================
headOption(空数组返回none)
val a = Array(1,2,3,4)
a.headOption
结果输出:
Int = 3
假如:
val b : Array[Char] = new Array(0) //创建空数组
b.headOption
结果输出:
Option[Char] = None
=============================
indexOf(查询当前数组中某数值的下标)
val a = Array(1,2,3,4)
a.indexOf(3)
结果输出:
Int = 2
val b = Array(1,2,3,3,4,5,3) 
a.indexOf(3,3) //从下标3的位置开始寻找3,找到第一个就返回其下标位置
结果输出:
Int = 3
=============================
indexOfSlice(检测当前数组中是否包含另一数组,并返回第一次匹配出现的元素索引值)
val a = Array(1,2,3,4)
a.indexOfSlice(Array(2,3))
结果输出:
Int = 1
val b = Array(1, 2, 3, 3, 4, 5, 3)
val c = Array(3,4)
b.indexOfSlice(c,3) //从下标3的位置开始寻找数组c,并返回第一个匹配出现的元素的索引
结果输出:
Int = 3
=============================
indexWhere(搜索第一个符合条件的数值的下标)
val a = Array(1,2,3,4)
a.indexWhere(x => x>1)
结果输出:
Int = 1
=============================
indices(返回当前数组索引集合)
val a = Array(1,2,3,4)
a.indices
结果输出:
Range = Range(0,1,2,3)
val d = Array(Array(4,5,6),Array(7,8,9))
d.indices
结果输出:
scala.collection.immutable.Range = Range(0, 1)
=============================
init(返回当前数组中不包含最后一位元素的数组)
val a = Array(1,2,3,4)
a.init
结果输出:
Array[Int] = Array(1, 2, 3)
=============================
inits(对当前数组进行inits操作,上一步作为下一步的操作对象,与tails相反)
val a = Array(1,2,3,4)
a.inits
a.inits.foreach(x => println(x.mkString(",")))
结果输出:
1,2,3,4
1,2,3
1,2
1
=============================
intersect(取两个集合的交集)
val a = Array(1,2,3,4)
val b = Array(2,3,4,5)
a.intersect(b)
结果输出:
Array[Int] = Array(2, 3, 4)
=============================
isDefinedAt(判断当前数组是否存在指定的索引值)
val a = Array(1,2,3,4)
a.isDefinedAt(2)
结果输出:
boolean = true
=============================
isEmpty(判断当前数组是否为空)
val a = Array(1,2,3,4)
a.isEmpty
结果输出:
boolean = false
=============================
isTraversab(判断当前数组是否可以反复遍历)
val a = Array(1,2,3,4)
a.inits(形成迭代器)
a.inits.isTraversab
boolean = false
a.isTraversab
boolean = true
=============================
iterator
val a = Array(1,2,3,4)
a.iterator
=============================
last(取得序列中最后一个元素)
val a = Array(1,2,3,4)
a.last
结果输出:
Int = 4
=============================
lastIndexOf(取得数组中最后一个等于参数元素的位置)
val a = Array(1,2,3,4,2)
a.lastIndexOf(2)
结果输出:
Int = 4
a.lastIndexOf(2,2) //取得数组中最后一个等于参数元素的位置,并在指定的end之前(包括)的元素中查找
Int = 1
=============================
lastIndexWhere(返回当前数组中最后一个满足某条件的元素的索引值)
val a = Array(1,2,3,4)
a.lastIndexWhere(x => x<4)
结果输出:
Int = 0
val b = Array(1,2,3,3,4,5,3)
b.lastIndexWhere({x => x>4},6) //返回当前数组中最后一个满足某条件的元素的索引值,并在指定的end之前a
结果输出:
Int = 5
=============================
lastIndexOfSlice(判断当前数组时候包含另一数组,并返回最后一次出现该数组位置处的索引)
val b = Array(1,2,3,3,4,5,3)
val c = Array(2,3)
b.lastIndexOfSlice(c)
结果输出:
Int = 1
val b = Array(1,2,3,3,4,5,3)
val c = Array(2,3)
b.lastIndexOfSlice(c,6) //返回最后一次出现该数组位置处的索引,并在指定的end之前
结果输出:
Int = 1
=============================
lastOption(返回当前序列中最后一个对象)
val a = Array(1,2,3,4)
a.lastOption
结果输出:
Option[Int] = Some(4)
=============================
length(返回当前数组中的元素长度/个数)
val a = Array(1,2,3,4)
a.length
结果输出:
Int = 4
=============================
lengthCompare(比较两对数组的长度与参数,根据二者关系的不同返回不同的值)
//比较规则
   x<0   if this.length < len
   x==0  if this.length == len
   x>0  if this.length > len
val a = Array(1,2,3,4)
a.lengthCompare(2)
结果输出:
Int = 2
=============================
map(定义函数进行操作)
val a = Array(1,2,3,4)
val b = a.map(x => x*10)
println(b.mkString(","))
结果输出:
10,20,30,40
=============================
max/min/sum(返回当前数组中的最大值/最小值/总和)
val a = Array(1,2,3,4)
a.max
结果输出:
Int = 4
a.min
结果输出:
Int = 1
a.sum
结果输出:
Int = 10
=============================
maxBy(返回当前数组中第一个符合条件的元素)
val a = Array(1,2,3,4)
a.maxBy(_>2)
结果输出:
Int = 3
=============================
minBy(返回第一个不符合条件的元素)
val a = Array(1,2,3,4)
a.minBy(_>2)
结果输出:
Int = 1
=============================
mkString(将所有元素组合成一个字符串)
val a = Array(1,2,3,4)
a.mkString(",")
结果输出:
String = 1,2,3,4
=============================
nonEmpty(判断当前数组不为空)
val a = Array(1,2,3,4)
a.nonEmpty
结果输出:
boolean = true
=============================
padTo(向后补齐,如果当前数组长度小于Len,那么新产生的数组长度为len,否则返回当前数组)
val a = Array(1,2,3,4)
val b = a.padTo(7,9) //7代表补齐的数组长度,9代表补齐当前数组的数值
结果输出:
b: Array[Int] = Array(1, 2, 3, 4, 9, 9, 9)
val a = Array(1,2,3,4)
val b = a.padTo(2,9)
结果输出:
b: Array[Int] = Array(1,2,3,4)
=============================
par(返回一个并行实现,产生的并行序列,不能被修改)
val a = Array(1,2,3,4)
a.par
结果输出:
ParArray[Int] = ParArray(1,2,3,4)
=============================
partition(根据条件拆分当前数组,将满足条件的放入第一个数组中,其余放在第二个数组中)
val a = Array(1,2,3,4)
a.partition(_>2)
结果输出:
(Array[Int], Array[Int]) = (Array(3, 4),Array(1, 2))
=============================
patch(批量替换)
val a = Array(1,2,3,4)
a.patch(1,Array(4,5,6),1)
结果输出:
Array[Int] = Array(1, 4, 5, 6, 3, 4) //从第一个数值处开始,将后面的一个数值替换为4,5,6
=============================
permutations(排列组合,组合中的内容可以相同,但是顺序不可相同)
val a = Array(1,2,3,4)
a.permutations.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
a.permutations.toList //产生集合
a.permutations.toArray //产生数组
a.permutations.toSet //产生数值
a.permutations.toSeq //产生seq数值数组
=============================
prefixLength(给定一个条件,返回一个前置数组的长度,这个数组中的元素都满足该条件)
val a = Array(1,2,3,4)
a.prefixLength(_<4)
结果输出:
Int = 3
=============================
product(数组中所有元素乘积的值,其中元素只能是数值类型)
val a = Array(1,2,3,4)
a.product
结果输出:
Int = 24
=============================
reduce
val a = Array(1, 2, 3, 4, 5, 6, 3, 4, 5, 5)
a.reduce(_+_)
结果输出:
Int = 38
reduceLeft/reduceRight
reduceLeftOption/reduceRightOption(加Option防止空数组报错)
=============================
练习
val words = Array("hello world","hello scala","spark scala")
words.flatMap(x => x.split(" "))
words.flatMap(x => x.split(" ")).groupBy(x => x)
words.flatMap(x => x.split(" ")).groupBy(x => x).foreach(f=>println(f._1,f._2.length))
=============================
reverse(反转数组)
val a = Array(1,2,3,4)
a.reverse
结果输出:
4,3,2,1
a.reverseIterator.foreach(println)(反转迭代)
结果输出:
4
3
2
1
=============================
reverseMap(反向map方法)
val a = Array(1,2,3,4)
val b = a.reverseMap(x => x*10)
println(b.mkString(","))
结果输出:
40,30,20,10
=============================
sameElements(判断两个数组在顺序与对应位置上的元素相同)
val a = Array(1,2,3,4)
val b = Array(1,2,3,4)
a.sameElements(b)
结果输出:
boolean = true
=============================
scan()(同fold)
def i(m:Int,n:Int):Int ={
val s = "com=%d+%d"
println(s.format(m,n))
return m+n
}
val a = Array(1,2,3,4)
val b = a.scan(5)(i)
结果输出:
com=5+1
com=6+2
com=8+3
com=11+4
b: Array[Int] = Array(5, 6, 8, 11, 15)
=============================
segmentLength(从当前数组的0下标位置向后查找,并返回所有满足条件的连续元素的长度,当第一个元素不满足直接返回0)
val a = Array(1,2,3,4)
a.segmentLength(_<=2,0)
结果输出:
Int = 2
=============================
seq(seq数组)
val a = Array(1,2,3,4)
a.seq.foreach(println)
结果输出:
1
2
3
4
=============================
size(统计当前数组元素个数,同样统计空值)
val a = Array(1,2,3,4)
a.size
结果输出:
Int = 4
=============================
slice(取出当前数组中,1(from)到(until)之间的片段,前包含后不包含)
val a = Array(1,2,3,4)
a.slice(1,3)//下标位置
结果输出:
Array[Int] = Array(2, 3)
=============================
sliding(每个元素与它后面的size-1个元素组成一个新的数组,最终组成一个新的集合返回,当剩余元素不足size数时停止)
val a = Array(1,2,3,4)
a.sliding(2).toList
结果输出:
List[Array[Int]] = List(Array(1, 2), Array(2, 3), Array(3, 4))
a.sliding(3,2).toList //括号中前一位控制几个数值为一组,后一位控制几个数值间隔
结果输出:
List[Array[Int]] = List(Array(1, 2, 3), Array(3, 4))
=============================
sortBy(按指定的排序规则排序)
val a = Array(1,2,3,4)
a.sortBy(x => x)
1,2,3,4
a.sortBy(x => -x)
4,3,2,1
======
sortWith(自定义排序方法)
val a = Array(1,2,3,4)
a.sortWith(_.compareTo(_)>0) //大数在前
结果输出:
Array[Int] = Array(4, 3, 2, 1)
val ff = Array(("zs",50),("ls",80),("ww",70))
Array[(String, Int)] = Array((ls,80), (ww,70), (zs,50))
ff.sortBy(x => x._2) //对应元组的第二位数据进行排序
Array[(String, Int)] = Array((zs,50), (ww,70), (ls,80))
======
sorted(使用默认规则排序)
val ff = Array(("zs",50),("ls",80),("ww",70))
ff.sorted
结果输出:
Array[(String, Int)] = Array((ls,80), (ww,70), (zs,50))
=============================
span(分割当前数组为两个,从第一个元素开始,直到找到第一个不满足条件的元素为止,之前的元素放在一个数组中,其余放在第二个数组中)
val a = Array(1,2,3,4)
a.span(x => x<3)
结果输出:
(Array[Int], Array[Int]) = (Array(1, 2),Array(3, 4))
=============================
splitAt(从指定位置开始,将当前数组拆分成两个)
val a = Array(1,2,3,4)
a.splitAt(2)
结果输出:
(Array[Int], Array[Int]) = (Array(1, 2),Array(3, 4))
=============================
stringPrefix(返回toString结果的前缀)
val a = Array(1,2,3,4)
a.toString()
a.stringPrefix
结果输出:
String = [I
=============================
subSequence(返回1(start)和(end)间的字符,只可以是字符,并且前包含后不包含)
val ee = Array(‘a‘,‘b‘,‘c‘)
ee.subSequence(1,3)
结果输出:
CharSequence = bc
=============================
take(返回当前数组中前n个元素组成的新数组)
val a = Array(1,2,3,4)
a.take(2)
结果输出:
Array[Int] = Array(1,2)
=====
takeRight(返回当前数组中,从右边开始,选择n个元素组成的新数组)
val a = Array(1,2,3,4)
a.takeRight(2)
结果输出:
Array[Int] = Array(3,4)
=====
takeWhile(返回当前数组中,从第一个元素开始,满足条件的连续元素组成的新数组,遇到第一个不满足条件的元素立刻终止,并返回该元素)
val a = Array(1,2,3,4)
a.takeWhile(x => x>0)
结果输出:
Array[Int] = Array(1, 2, 3, 4)
=============================
toArray(转成Array类型)
val a = List(1,2,3,4)
结果输出:
a.toArray = Array(1,2,3,4)
=============================
toBuffer(转换成Buffer类型,长度不固定(ArrayBuffer))
val a = List(1,2,3,4)
结果输出:
scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4)
=============================
toList(固定长度,不能改变)(转换成List类型)
val a = List(1,2,3,4)
结果输出:
List[Int] = List(1, 2, 3, 4)
=============================
toMap
val d = Array((1,2),(3,4),(5,6))
val d = Array((1,2),(3,4),(5,6)).toMap
结果输出:
scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4, 5 -> 6)
=============================
toIterator
scala.collection.mutable.Buffer //当前包下数据可改
scala.collection.immutable //当前包下数据不可改
=============================
练习
val a = Array(1,2,3,4,5)
a.map((_,1)).toMap   //转成map
=============================
toStream(转换Stream类型)
val a = Array(1,2,3,4,5)
a.toStream
scala.collection.immutable.Stream[Int] = Stream(1, ?)
a.toStream.toStream(1)
结果输出:
Int = 2
懒汉模式,当需要时才取出
=============================
toVector(转换Vector类型)
val a = Array(1,2,3,4,5)
a.toVector
结果输出:
Vector[Int] = Vector(1, 2, 3, 4, 5)
=============================
transpose(矩阵转换,二维数组行列转换)
val a = Array(Array(1,2),Array(3,4),Array(5,6))
a.transpose
结果输出:
Array[Array[Int]] = Array(Array(1, 3, 5), Array(2, 4, 6))
=============================
union(联合两个数组,同操作符++)
val a = Array(1,2,3,4,5)
val k = Array((2,3),(4,6))
a.union(k)
结果输出:
Array[Any] = Array(1, 2, 3, 4, 5, (2,3), (4,6))
res21.toSet(去重)
res22: scala.collection.immutable.Set[Any] = Set(6, Array(1, 2), Array(3, 4), Array(5, 6))
=============================
unzip(将含有两个元素的数组,第一个元素取出组成一个数组,第二个元素组成一个新数组)
val k = Array((2,3),(4,6))
k.unzip
结果输出:
(Array[Int], Array[Int]) = (Array(2, 4),Array(3, 6))
===========
unzip3
val c = Array(("a","b","c"),("d","e","f"),("g","h","t"))
c.unzip3
结果输出:
(Array[String], Array[String], Array[String]) = (Array(a, d, g),Array(b, e, h),Array(c, f, t))
=============================
update(将数组中某一个索引处的数值更新为另一个)
val a = Array(1,2,3,4,5)
a.update(1,6) //(前一个代表被修改处的索引,第二个代表新的数值)
结果输出:
Array[Int] = Array(1, 6, 3, 4, 5)
=============================
view(返回form到until之间的数值,前包含后不包含)
val a = Array(1,2,3,4,5)
a.view(0,4).toArray //下标0位置到下标4位置之间的数值
Array[Int] = Array(1,2,3,4,5)
=============================
withFilter(取出符合条件的元素地址)
val a = Array(1,2,3,4,5)
a.withFilter({x => x>3}).map(x => x)
结果输出:
Array(4,5)
=============================
zip(将两个数组对应位置上的元素组成一个序列)
val a = Array(5, 6, 7, 8, 9, 0)
val b = Array(7,8,9)
val c = a.zip(b)
结果输出:
Array[(Int, Int)] = Array((7,5), (8,6), (9,7))
=============================
zipAll(同zip,但是允许两个数组长度不一样,不足的自动填充,)
val a = Array(5, 6, 7, 8, 9, 0)
val b = Array(5, 6, 4, 3)
a.zipAll(b,3,6)
结果输出:
Array[(Int, Int)] = Array((5,5), (6,6), (7,4), (8,3), (9,6), (0,6))
=============================
zipWithIndex(数组中的每个元素和它的索引组成同一数组)
val a = Array(5, 6, 7, 8, 9, 0)
a.zipWithIndex
结果输出:
Array[(Int, Int)] = Array((5,0), (6,1), (7,2), (8,3), (9,4), (0,5))

  

scala基础函数

原文:https://www.cnblogs.com/zzyz/p/13647802.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!