当前位置:网站首页>Encyclopedia of scala operators
Encyclopedia of scala operators
2022-06-28 06:59:00 【Xiaobinbin &】
Array A complete collection of array operators
Count array var array:Array[Int]=Array(1,2,3,4,5,6)
One 、 Extract elements
1. array.foreach(f:(a:T)=>Uint) // Traverse == Arguments are passed to the function
array.foreach(println) => 1 2 3 4 5 62.val rst:Int=array(index) // Get by index
println(array(1)) =>23.val rst:Int=array.apply(index) // Subscript extract element , If you cross the border, you will report an error
println(array.apply(1)) =>2
println(array.apply(6)) =>ERROR:ArrayIndexOutOfBoundsException4.val rst:Int=array.applyOrElse(index,defalut:(a:Int)=> The body of the function (Any))
// By index index To extract elements , If the index is out of bounds, the function body will be executed , And return the return value of the function
println(array.applyOrElse(1,(a:Int)=>println(s"$a"))) =>2
println(array.applyOrElse(6,(a:Int)=>println(s"$a"))) =>6 ()5.val rst:Int=array.head // Get the element at the beginning of the line , If the beginning of a line is empty, an error will be reported NoSuchElementException
val rst:Option[Int]=array.headOption // Get the element at the beginning of the line , According to the function .isEmpty To perform a void judgment ,get Get its value
val option: Option[Int] = array.headOption
if (option.isEmpty) {
println(" Line beginning is empty ")
}else
{
println(option.get)
}6.val rst:Int=array.last // Get the end of line element , If the end of the line is empty, an error will be reported NoSuchElementException
val rst:Option[Int]=array.lastOption // Get the element at the beginning of the line , According to the function .isEmpty To perform a void judgment ,get Get its value
7.val arr:Array[T]=array.slice(start:Int,end:Int) // Get the elements in the index interval
val arr: Array[Int] = array.slice(2, 4)
arr.foreach(println) ==>3 48.val arr:Array[T]=array.take(num:Int) // Get from left to back num Elements
9.val arr:Array[T]=array.takeRight(num:Int) // Get... From right to left num Elements
10.val arr:Array[T]=array.takeWhile(f:(a:Int)=>Boolean)
// Start from the first number on the left to get the elements that meet the conditions , The first one does not satisfy the requirement of returning null directly , The first number satisfies the condition , Until the end of the first element that does not meet the condition
array.takeWhile((a:Int)=>a>1).foreach(println) ==> empty
array.takeWhile((a:Int)=>a<3).foreach(println) ==> 1 211. val arr:Array[T]=array.filter(f:(a:Int)=>Boolean) // Extract the elements that meet the conditions
val arr:Array[T]=array.filterNot(f:(a:Int)=>Boolean) // Extract elements that do not meet the conditions
array.filter((a:Int)=>a>3).foreach(println) ==> 4 5 6
array.filterNot((a:Int)=>a<=3).foreach(println) ==> 1 2 312. val arr:Array[T]=array.collect(Predicate:(T)=>Boolean) // Get the element that satisfies the partial function condition
val rst:T=array.collectFist(Predicate:(T)=>Boolean) // Get the first element that satisfies the partial function condition
val p:PartialFunction[Int,Int]=(a:Int)=>a match {
case x if(x%2==0) =>x
}
array.collect(p).foreach(println) ==> 2,4,6
== Equivalent to array.collect({case x if(x%2==0)=>x}).foreach(println)13.val arr.Array[T]=array.find(f:(T)=>Boolean) // Get the first element that satisfies the condition
array.find((a:Int)=>a>2).foreach(println) ==>314.val arr:Array[T]=array.init // Exclude the last element after each execution
val arr:Array[T]=array.tail // Exclude the first element after each execution
val Iterator[Array[T]]=array.inits // Exclude other elements of the last element , Encapsulated as an iterator
val Iterator[Array[T]]=array.tails // Exclude other elements of the first element , Encapsulated as an iterator
array.foreach(print)
println() ==>123456
val init1: Array[Int] = array.init
init1.foreach(print)
println() ==>12345
val init2: Array[Int] = init1.init
init2.foreach(print)
println() ==>1234
array.foreach(print)
println() ==>123456
val tail1: Array[Int] = array.tail
tail1.foreach(print)
println() ==>23456
val tail2: Array[Int] = tail1.tail
tail2.foreach(print)
println() ==>3456
val inits: Iterator[Array[Int]] = array.inits
inits.foreach((a:Array[Int])=>println(a.mkString(",")))
1,2,3,4,5,6
1,2,3,4,5
1,2,3,4
1,2,3
1,2
1 inits.length ==>7( The last element is empty )
val tails: Iterator[Array[Int]] = array.tails
tails.foreach((a:Array[Int])=>println(a.mkString(",")))
1,2,3,4,5,6
2,3,4,5,6
3,4,5,6
4,5,6
5,6
6 tails.length ==>7( The last element is empty )
Two 、 modify
15. oldArr:Array[T]=array.update(index:Int,t:T) // Modify the value under an index in the original array ( The value of the original array changes )
16. val newArr:Array[T]=array.updated(index:Int,t:T) // Modify the value under an index in the array , Return a new array ( The value of the original array remains unchanged )
17. val newArr:Array[T]=array.drop(num:Int) // Delete from left to right num Data
18. val newArr:Array[T]=array.dropRight(num:Int) // Delete... From right to left num Data
19. val newArr:Array[T]=array,dropRight(f:(T)=Boolean) // From left to right , The data satisfying the condition is assigned to the new array
20. val newArr:Array[T]=array : + num[T] // Add an element to the right
21. val newArr:Array[T]=num[T] +: array // Add an element to the left
22. val newArr:Array[T]=array.union(array1) //array And array1 Combine
23 val newArr:Array[T]=array ++ array1 //array And array1 Combine
24 val newArr:Array[T]=array.intersect(array1) //array And array1 intersection
25. val newArr:Array[T]=array.diff(array1) //array And array1 Difference set
26. val newArr:Array[T]=array.patch(a:Int,GenSeq(...),num:Int) // The index of the array is indexed to a Start looking for the element of num individual , Replace with GenSeq The elements inside
val array=Array(1,2,3,4,5,6,7)
array.patch(2,GenSeq(9,9,9),2).foreach(print)
==> 129995673、 ... and 、 Iterative operator
27. val newArr:Array[U]=array.map(f: T=>U) // Forward processing one by one
28. val newArr:Array[U]=array.transform(f: T=>U) // Forward processing one by one ( Change the original array )
29. val newArr:Array[U]=array.reverseMap(f: T=>U) // Reverse one by one
30. val newArr:Array[T]=2Darray,flatten // Lower one dimension , flat
31. val newArr:Array[T]=2Darray.flatMap(t=>t.map(..)) // First lower one dimension , Each element is processed
array.map(a => a+2).foreach(print) ==> 3456789
array.foreach(print) ==> 1234567
array.transform(a=>a+2).foreach(print) ==> 3456789
array.foreach(print) ==> 3456789
array.reverseMap(a=>a+2).foreach(print) ==> 111098765
val arr=Array(Array(1,2,3),Array(4,5,6))
arr.flatten.foreach(print) ==> 123456Four . grouping
32. val newArr:Array[T]=array.par // Parallel array processing , It is often used to start the thread to process , With the method aggregate In combination with
33. val tp2:(Array[T],Array[T])=array.partititon(t:T=>Boolean) // Divide the elements in the array into two arrays , Form a tuple , Those that meet the conditions are in a group , Two groups that do not meet the conditions
34. val it:Interator[Array[T]]=array.grouped(num) // Divide the array into num An array
35. val map:Map[Int,Array[T]]=array,groupBy(key:B) // Press... In the array key Grouping (T Pairs are tuples or spline classes ), Form key value pairs
36. val it:Interator[Array[T]]=array.sliding(size:Int,step:Int) // By each group of elements size individual , Every step step Regroup elements 1,2,3 2,3,4 3,4,5
array.par.foreach(print);println() ==> 165432
val tp1: (Array[Int], Array[Int]) = array.partition(a => a > 3)
tp1._1.foreach(print);println() ==> 456
tp1._2.foreach(print);println() ==> 123
val tp2: Iterator[Array[Int]] = array.grouped(3)
for (elem <- tp2) {elem.foreach(print);println()} ==> 123 456
val arr=Array((1,"a"),(1,"b"),(2,"a"))
val map: Map[Int, Array[(Int, String)]] = arr.groupBy(_._1)
map.map(a=>{print(a._1);a._2.foreach(print);println()})
==>2(2,a) 1(1,a)(1,b)
map.map(a=>println(a._1,a._2.count(a=>true))) ==>(2,1) (1,2)
边栏推荐
猜你喜欢

微信小程序编译页面空白bug的原因

Recommend several 0 code, free, learning and using visualization tools

LeetCode+ 51 - 55 回溯、动态规划专题

How to open UMD, KMD log and dump diagrams in CAMX architecture
![[C language] detailed explanation of C language to obtain array length](/img/cf/75c314bb622b8a1745f43cc07cb02e.png)
[C language] detailed explanation of C language to obtain array length

Overview, implementation and use of CRC32

Interpretation of Blog

Linked list (III) - reverse linked list

Yesterday, I went to a large factory for an interview and asked me to do four arithmetic operations. Fortunately, I am smart enough

《微信小程序-基础篇》带你了解小程序中的生命周期(一)
随机推荐
Eyebeam advanced settings
最后的二十九天
推荐10个好用到爆的Jupyter Notebook插件,让你效率飞起
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
普歌--三大基础排序,冒泡·选择·快速
js正则表达式系统讲解(全面的总结)
Iphone6plus enters the list of antique products netizen: I'm still using it
实现这个 issue 得700块钱人民币,有人做嘛?
What is a consistent hash? What scenarios can it be applied to?
【Rust日报】 2020-04-23 Rust 1.43.0 发布
Promotion intégrale et ordre des octets de fin de taille
VM332 WAService.js:2 Error: _vm.changeTabs is not a function报错
[interval DP] stone consolidation
Niubi 666, this project makes web page making as simple as building blocks
Note that JPA uses a custom VO to receive jpql query results
Singleton singleton mode
微信小程序编译页面空白bug的原因
我的MVVM开源项目《出行防疫App》已发布
Puge -- singleton mode
The code is correct, and the rendering page does not display the reason