Scala学习笔记(一):Array函数整理


Scala语言是一门基于JVM的编程语言,具有强大的功能,它即具有类似Java的面向对象的特性,而且也类似于C语言面向过程,函数也是一等公民,即不需要在隶属于哪一个类就可以执行。基于JVM的特性,使得scala和Java可以无缝互操作,scala可以任意操作Java的代码,两者的联系紧密。

一、聚合

aggregate			一般和 par 搭配使用,第一个参数是初始值,用于带入计算,第二个参数是两个函数,
					当不使用 par 时,只执行 seqop ,当使用 par 时,先执行 seqop ,再执行 combop 。
	def aggregate[B](z: => B)(seqop: (B, Int) => B,combop: (B, B) => B): B
	
max					最大值
	def max[B >: Int](implicit cmp: Ordering[B]): Int
	
maxBy				满足条件的第一个值
	def maxBy[B](f: Int => B)(implicit cmp: Ordering[B]): Int
	
min					最小值
	def min[B >: Int](implicit cmp: Ordering[B]): Int
	
minBy				不满足条件的第一个值
	def minBy[B](f: Int => B)(implicit cmp: Ordering[B]): Int
	
count				统计满足参数条件的元素数量
	def count(p: Int => Boolean): Int
	
product				求积
	def product[B >: Int](implicit num: Numeric[B]): B
	
sum					求和
	def sum[B >: Int](implicit num: Numeric[B]): B
	
fold				
	def fold[A1 >: Int](z: A1)(op: (A1, A1) => A1): A1
	
foldLeft			注意 B 类型在参数函数 op 的位置,初始值 z 为参数函数 op 的第一个入口参数,
					且参数函数 op 每次的返回值作为下一次计算的第一个入口参数
	override def foldLeft[B](z: B)(op: (B, Int) => B): B
	
foldRight			注意 B 类型在参数函数 op 的位置,初始值 z 为参数函数 op 的第二个入口参数,
					且参数函数 op 每次的返回值作为下一次计算的第二个入口参数
	override def foldRight[B](z: B)(op: (Int, B) => B): B
	
reduce				同 fold ,但没有初始值参数
	def reduce[A1 >: Int](op: (A1, A1) => A1): A1
	
reduceLeft			同 foldLeft ,但没有初始值参数
	override def reduceLeft[B >: Int](op: (B, Int) => B): B
	
reduceRight			同 foldRight ,但没有初始值参数
	override def reduceRight[B >: Int](op: (Int, B) => B): B
	
reduceOption		返回 Option
	def reduceOption[A1 >: Int](op: (A1, A1) => A1): Option[A1]
	
reduceLeftOption	返回 Option
	def reduceLeftOption[B >: Int](op: (B, Int) => B): Option[B]
	
reduceRightOption	返回 Option
	def reduceRightOption[B >: Int](op: (Int, B) => B): Option[B]
	
scan				同 fold ,但是返回的是数组类型
	def scan[B >: Int, That](z: B)(op: (B, B) => B)(implicit cbf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def scan[B >: Int, That](z: B)(op: (B, B) => B)(implicit cbf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That
	
scanLeft			同 foldLeft ,但是返回的是数组类型
	def scanLeft[B, That](z: B)(op: (B, Int) => B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def scanLeft[B, That](z: B)(op: (B, Int) => B)(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That
	
scanRight			同 foldRight ,但是返回的是数组类型
	def scanRight[B, That](z: B)(op: (Int, B) => B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def scanRight[B, That](z: B)(op: (Int, B) => B)(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That

二、查询

1.元素

apply			根据索引取元素
	def apply(i: Int): Int
	
applyOrElse		根据索引取元素,若越界则返回第二个参数(偏函数)
	def applyOrElse[A1 <: Int, B1 >: Int](x: A1,default: A1 => B1): B1
	
charAt			根据索引取字符
	def charAt(index: Int): Char
	
head			数组中第一个元素
	override def head: Int
	
tail			数组中除了第一个元素的其他元素
	override def tail: Array[Int]
	
tails			对数组从右开始进行迭代,返回 Iterator
	def tails: Iterator[Array[Int]]
	
last			数组中最后一个元素
	override def last: Int
	
init			数组中除了最后一个元素的其他元素
	override def init: Array[Int]
	
inits			对数组从左开始进行迭代,返回 Iterator
	def inits: Iterator[Array[Int]]
	
headOption		返回数组中第一个元素的Option类型(Some())
	def headOption: Option[Int]
	
lastOption		返回数组中最后一个元素的Option类型(Some())
	def lastOption: Option[Int]
	
find			查找符合参数条件的第一个元素并返回Option类型(Some())
	override def find(p: Int => Boolean): Option[Int]

2.数组

diff			差集
	def diff[B >: Int](that: scala.collection.GenSeq[B]): Array[Int]
	
intersect		交集
	def intersect[B >: Int](that: scala.collection.GenSeq[B]): Array[Int]
	
union			并集
	override def union[B >: Int, That](that: scala.collection.GenSeq[B])(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	override def union[B >: Int, That](that: scala.collection.GenSeq[B])(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That
	
distinct		去重
	def distinct: Array[Int]
	
filter			筛选符合参数条件的元素,返回数组类型
	def filter(p: Int => Boolean): Array[Int]
	
filterNot		筛选不符合参数条件的元素,返回数组类型
	def filterNot(p: Int => Boolean): Array[Int]
	
withFilter		同 filter ,但 withFilter 的返回值要加 .map()
	def withFilter(p: Int => Boolean): scala.collection.generic.FilterMonadic[Int,Array[Int]]
	
slice			截取 from~until 之间的元素,返回数组类型
	override def slice(from: Int,until: Int): Array[Int]
	
view			截取 from~until 之间的元素,返回SeqView(...)类型
	override def view(from: Int,until: Int): scala.collection.mutable.IndexedSeqView[Int,Array[Int]]
	override def view: scala.collection.mutable.IndexedSeqView[Int,Array[Int]]
	
sliding			返回 Iterator 类型,sliding(3,3)=grouped(3)
	def sliding(size: Int,step: Int): Iterator[Array[Int]]
	def sliding(size: Int): Iterator[Array[Int]]
	
take			从下标0开始取参数n数量的元素
	override def take(n: Int): Array[Int]
	
takeRight		
	override def takeRight(n: Int): Array[Int]
	
takeWhile		从下标0开始连续满足参数条件的元素,返回数组类型
	override def takeWhile(p: Int => Boolean): Array[Int]
	
collect			满足参数(偏函数)条件的所有元素,返回数组类型
	def collect[B, That](pf: PartialFunction[Int,B])(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def collect[B, That](pf: PartialFunction[Int,B])(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That
	
collectFirst	Some()	同find,但collectFirst参数是偏函数
	def collectFirst[B](pf: PartialFunction[Int,B]): Option[B]
	
// 不改变集合元素和数量
foreach			返回 Unit
	override def foreach[U](f: Int => U): Unit
	
indices			索引集合
	def indices: scala.collection.immutable.Range
	
iterator		返回 Iterator 类型
	override def iterator: Iterator[Int]
	
reverse
	override def reverse: Array[Int]
	
reverseIterator	反向 Iterator
	override def reverseIterator: Iterator[Int]
	
seq				返回 WrappedArray 类型
	def seq: scala.collection.mutable.IndexedSeq[Int]

3.索引

indexOf				参数元素(可从指定位置开始查找)
	def indexOf[B >: Int](elem: B,from: Int): Int
	def indexOf[B >: Int](elem: B): Int
	
indexOfSlice		参数序列(可从指定位置开始查找)
	def indexOfSlice[B >: Int](that: scala.collection.GenSeq[B],from: Int): Int
	def indexOfSlice[B >: Int](that: scala.collection.GenSeq[B]): Int
	
indexWhere			满足参数条件的元素(可从指定位置开始查找)
	override
	def indexWhere(p: Int => Boolean,from: Int): Int
	def indexWhere(p: Int => Boolean): Int
	
lastIndexOf			参数元素(查找截止到指定位置)
	def lastIndexOf[B >: Int](elem: B,end: Int): Int
	def lastIndexOf[B >: Int](elem: B): Int
	
lastIndexOfSlice	参数序列(查找截止到指定位置)
	def lastIndexOfSlice[B >: Int](that: scala.collection.GenSeq[B],end: Int): Int
	def lastIndexOfSlice[B >: Int](that: scala.collection.GenSeq[B]): Int
	
lastIndexWhere		满足参数条件的元素(查找截止到指定位置)
	override
	def lastIndexWhere(p: Int => Boolean,end: Int): Int
	def lastIndexWhere(p: Int => Boolean): Int

4.数量

length
	def length: Int
	
size
	override def size: Int
	
lengthCompare		实际元素数量和参数 len 的差值
	override def lengthCompare(len: Int): Int
	
prefixLength		从下标0开始连续满足参数条件的元素数量
	def prefixLength(p: Int => Boolean): Int
	
segmentLength		从下标 from 开始连续满足参数条件的元素数量
	override def segmentLength(p: Int => Boolean,from: Int): Int

三、操作

1.替换

copyToArray		将数组从0位置开始的长度为 len 的部分替换为参数数组中的 start 位置开始的长度为 len 的部分
	override
	def copyToArray[U >: Int](xs: Array[U],start: Int,len: Int): Unit
	def copyToArray[B >: Int](xs: Array[B]): Unit
	def copyToArray[B >: Int](xs: Array[B],start: Int): Unit
	
patch			将序列中从 from 位置开始的 replaced 个元素替换为 GenSeq
	def patch[B >: Int, That](from: Int,patch: scala.collection.GenSeq[B],replaced: Int)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def patch[B >: Int, That](from: Int,patch: scala.collection.GenSeq[B],replaced: Int)(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That
	
update			将 index 位置的元素替换为 elem ,不返回新的数组
	def update(i: Int,x: (String, Int)): Unit
	
updated			将 index 位置的元素替换为 elem ,返回新的数组
	def updated[B >: (String, Int), That](index: Int,elem: B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[(String, Int)],B,That]): That
	def updated[B >: (String, Int), That](index: Int,elem: B)(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[(String, Int)],B,That]): That

2.增加

copyToBuffer	将数组中的所有元素添加到参数数组中(Buffer类型)
	def copyToBuffer[B >: Int](dest: scala.collection.mutable.Buffer[B]): Unit
	
padTo			若数组长度小于第一个参数,则扩展数组长度并使用第二个参数填充
	def padTo[B >: Int, That](len: Int,elem: B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def padTo[B >: Int, That](len: Int,elem: B)(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That

3.删除

drop			删除数组左侧的n个元素
	override def drop(n: Int): Array[Int]
	
dropRight		删除数组右侧的n个元素
	override def dropRight(n: Int): Array[Int]
	
dropWhile		删除数组中第一个符合条件的元素直至第一个不符合条件的元素
	override def dropWhile(p: Int => Boolean): Array[Int]

4.变形

flatMap			先映射(map)再压平(flatten),map后的每个元素必须是集合
	def flatMap[B, That](f: Int => scala.collection.GenTraversableOnce[B])(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def flatMap[B, That](f: Int => scala.collection.GenTraversableOnce[B])(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That
	
flatten			扁平化(降维),二维数组转一维数组
	def flatten[U](implicit asTrav: Int => Traversable[U],implicit m: scala.reflect.ClassTag[U]): Array[U]
	def flatten[B](implicit asTraversable: Int => scala.collection.GenTraversableOnce[B]): scala.collection.mutable.IndexedSeq[B]
	
transpose		矩阵转置,二维数组行列转换
	def transpose[U](implicit asArray: Int => Array[U]): Array[Array[U]]
	def transpose[B](implicit asTraversable: Int => scala.collection.GenTraversableOnce[B]): scala.collection.mutable.IndexedSeq[scala.collection.mutable.IndexedSeq[B]]
	val arr = Array(Array("a",1),Array("b",2),Array("c",3))
	arr.transpose
		Array(Array(a, b, c), Array(1, 2, 3))
		
unzip			将含有两个二元组的数组,元祖中的数据一一组队,返回包含这两个数组的元祖
	def unzip[T1, T2](implicit asPair: ((String, Int)) => (T1, T2),implicit ct1: scala.reflect.ClassTag[T1],implicit ct2: scala.reflect.ClassTag[T2]): (Array[T1], Array[T2])
	def unzip[A1, A2](implicit asPair: ((String, Int)) => (A1, A2)): (scala.collection.mutable.IndexedSeq[A1], scala.collection.mutable.IndexedSeq[A2])
	scala> val arr = Array(("henry",18),("laura",20))
	arr4: Array[(String, Int)] = Array((henry,18), (laura,20))
	scala> arr.unzip
	res50: (Array[String], Array[Int]) = (Array(henry, laura),Array(18, 20))
	
unzip3			将含有两个三元组的数组,元祖中的数据一一组队,返回包含这三个数组的元祖
	def unzip3[T1, T2, T3](implicit asTriple: ((String, Int)) => (T1, T2, T3),implicit ct1: scala.reflect.ClassTag[T1],implicit ct2: scala.reflect.ClassTag[T2],implicit ct3: scala.reflect.ClassTag[T3]): (Array[T1], Array[T2], Array[T3])
	def unzip3[A1, A2, A3](implicit asTriple: ((String, Int)) => (A1, A2, A3)): (scala.collection.mutable.IndexedSeq[A1], scala.collection.mutable.IndexedSeq[A2], scala.collection.mutable.IndexedSeq[A3])

5.修改

map				遍历映射
	def map[B, That](f: Int => B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def map[B, That](f: Int => B)(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That
	
reverseMap		反向 map
	def reverseMap[B, That](f: Int => B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Int],B,That]): That
	def reverseMap[B, That](f: Int => B)(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Int],B,That]): That
	
transform		同 map ,但返回类型不同,且不返回新集合
	def transform(f: Int => Int): scala.collection.mutable.WrappedArray[Int]

6.组合

addString		拼接字符串,创建 StringBuilder 对象,返回StringBuilder类型
	def addString(b: StringBuilder): StringBuilder
	def addString(b: StringBuilder,sep: String): StringBuilder
	def addString(b: StringBuilder,start: String,sep: String,end: String): StringBuilder
	
mkString		拼接字符串,返回String类型
	def mkString: String
	def mkString(sep: String): String
	def mkString(start: String,sep: String,end: String): String
	
combinations	任意n个元素不考虑顺序进行排序组合,返回 Iterator 类型
	def combinations(n: Int): Iterator[Array[Int]]
	
permutations	所有元素考虑顺序进行排序组合,返回 Iterator 类型
	def permutations: Iterator[Array[Int]]
	
zip				将两个序列对应位置上的元素组成一个元祖数组,要求两个序列长度相同
	override def zip[A1 >: Char, B, That](that: scala.collection.GenIterable[B])(implicit bf: scala.collection.generic.CanBuildFrom[Array[Char],(A1, B),That]): That
	override def zip[A1 >: Char, B, That](that: scala.collection.GenIterable[B])(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Char],(A1, B),That]): That
	val arr1 = Array(1,2,3)
	val arr2 = Array('a','b','c')
	arr2.zip(arr1)
		Array((a,1), (b,2), (c,3))
		
zipAll			同zip,但是允许两个序列长度不同,第一个序列长度不足时,填充第一个参数元素;第二个序列长度不足时,填充第二个参数元素
	def zipAll[B, A1 >: Char, That](that: scala.collection.GenIterable[B],thisElem: A1,thatElem: B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[Char],(A1, B),That]): That
	def zipAll[B, A1 >: Char, That](that: scala.collection.GenIterable[B],thisElem: A1,thatElem: B)(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Char],(A1, B),That]): That
	val arr1 = Array(1,2,3)
	val arr2 = Array('a','b','c')
	val arr3 = Array(1,2)
	val arr4 = Array('a','b')
	arr4.zipAll(arr1,'c',0)
		Array((a,1), (b,2), (c,3))
	arr2.zipAll(arr3,'c',0)
		Array((a,1), (b,2), (c,0))
		
zipWithIndex	序列中的每个元素和它的索引组成的一个元祖数组
	override def zipWithIndex[A1 >: Char, That](implicit bf: scala.collection.generic.CanBuildFrom[Array[Char],(A1, Int),That]): That
	override def zipWithIndex[A1 >: Char, That](implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.mutable.WrappedArray[Char],(A1, Int),That]): That

7.分组

par				返回一个并行序列ParArray[Int],相当于hashcode partitioner,便于多线程执行
	override def par: scala.collection.parallel.mutable.ParArray[Int]
	
partition		根据参数条件拆分成两个数组并返回元祖类型
	def partition(p: Int => Boolean): (Array[Int], Array[Int])
	
span			根据参数条件将序列拆分为两个数组(从下标0开始直到第一个不满足条件的元素为止)
	override def span(p: Int => Boolean): (Array[Int], Array[Int])
	
splitAt			从指定参数位置将序列拆分成两个数组
	override def splitAt(n: Int): (Array[Int], Array[Int])
	
groupBy			根据参数函数(pattern match)进行分组,并返回Map类型
	def groupBy[K](f: Int => K): scala.collection.immutable.Map[K,Array[Int]]
	
grouped			根据参数按照元素顺序进行分组,返回 Iterator 类型
	def grouped(size: Int): Iterator[Array[Int]]

8.排序

sortBy			
	def sortBy[B](f: Int => B)(implicit ord: scala.math.Ordering[B]): Array[Int]
	
sorted			
	def sorted[B >: Int](implicit ord: scala.math.Ordering[B]): Array[Int]
	
sortWith		
	def sortWith(lt: (Int, Int) => Boolean): Array[Int]
	
// arr.sortBy(x=>x) <=> arr.sorted <=> arr.sortWith(_<_)
// arr.sortBy(x=>-x) <=> arr.sorted(Ordering.Int.reverse) <=> arr.sortWith(_>_)

四、判断

canEqual			是否可以比较
	override def canEqual(that: Any): Boolean
	
contains			是否包含参数元素
	def contains[A1 >: Int](elem: A1): Boolean
	
containsSlice		是否包含子序列(序列,有序,连续)
	def containsSlice[B](that: scala.collection.GenSeq[B]): Boolean
	
corresponds			数组长度相等且同一位置元素符合参数条件
	def corresponds[B](that: scala.collection.GenSeq[B])(p: (Int, B) => Boolean): Boolean
	val a1 = Array(3,5,8,9,11)
	val a2 = Array(2,6,7,10,13)
	val a3 = Array(4,6,12,10,13)
	a1.corresponds(a2)(_<_)
		Boolean = false
	a1.corresponds(a3)(_<_)
		Boolean = true
		
endsWith			数组是否以参数序列结尾
	override def endsWith[B](that: scala.collection.GenSeq[B]): Boolean
	
startsWith			数组[从指定位置开始]是否以子序列开始
	override
	def startsWith[B](that: scala.collection.GenSeq[B],offset: Int): Boolean
	def startsWith[B](that: scala.collection.GenSeq[B]): Boolean
	
exists				数组是否包含符合参数条件的元素
	override def exists(p: Int => Boolean): Boolean
	
forall				数组元素是否都满足参数条件
	override def forall(p: Int => Boolean): Boolean
	
hasDefiniteSize		数组长度是否固定
	def hasDefiniteSize: Boolean
	
isDefinedAt			是否包含参数指定的索引
	def isDefinedAt(idx: Int): Boolean
	
isEmpty				数组是否为空
	override def isEmpty: Boolean
	
nonEmpty			数组是否不为空
	def nonEmpty: Boolean
	
isTraversableAgain	是否可以重复遍历
	final def isTraversableAgain: Boolean
	
runWith				以第二参数下标提取序列元素,作为第一参数函数的入口,true,异常,false
	def runWith[U](action: Int => U): Int => Boolean
	arr.runWith(x=>x match {
		case x if x%2==0 => x
	})(17)
	
sameElements		两个序列的元素是否相同(长度相同,相同位置值相同)
	override def sameElements[B >: Int](that: scala.collection.GenIterable[B]): Boolean

五、类型转换

to				返回集合的 Vector 类型,对比 toVector
	override def to[Col[_]](implicit cbf: scala.collection.generic.CanBuildFrom[Nothing,Int,Col[Int]]): Col[Int]
	
toArray       
	override def toArray[U >: Int](implicit evidence$1: scala.reflect.ClassTag[U]): Array[U]
	
toBuffer        
	override def toBuffer[A1 >: Int]: scala.collection.mutable.Buffer[A1]
	
toIndexedSeq
	def toIndexedSeq: scala.collection.immutable.IndexedSeq[Int]
	
toIterable
	override def toIterable: Iterable[Int]
	
toIterator
	override def toIterator: Iterator[Int]
	
toList
	override def toList: List[Int]
	
toMap
	def toMap[T, U](implicit ev: Int <:< (T, U)): scala.collection.immutable.Map[T,U]
	
toSeq
	override def toSeq: Seq[Int]
	
toSet
	def toSet[B >: Int]: scala.collection.immutable.Set[B]
	
toStream
	override def toStream: scala.collection.immutable.Stream[Int]
	
toString
	def toString(): String
	
toTraversable
	def toTraversable: Traversable[Int]
	
toVector
	def toVector: Vector[Int]

六、其他

andThen
compose
clone
orElse
lift
genericBuilder
stringPrefix
++
++:
+:
:+
\:
:\
array
deep
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值