# 数组、集合函数整理。。。

### Array

val arr = Array(1,2,3,4) //声明一个数组对象
//arr: Array[Int] = Array(1, 2, 3, 4)
val first = arr(0)//读取第一个元素
arr(3)=100//第三个元素
val newarr = arr.map(_ * 2) //所有元素乘2
println(newarr.mkString(",")) //打印数组，结果为：2,4,6,200


### ++

val a = Array(1, 2)
val b = Array(3, 4)
val c = a ++ b
println(c.mkString(","))


### ++:

val a = Array(1,2)
val b = scala.collection.mutable.ListBuffer(3,4)
val c = a++:b
println(c.getClass.getName)


### +:

val a = List(1, 2)
val b = 1 +: a
println(b.mkString)


### :+

val a = List(1, 2)
val b = a :+ 9
println(b.mkString)


### /:

val a = List(1,2,3,4)
val b = (10/:a)((x,y)=>x+y)//(10/:a)(_+_)


### :\

val a  = List(1,2,3,4)
val b = (a:\10)(_-_)
println(b)


val a = List(1,2,3,4)
val b = new StringBuilder()


### aggregate

def seq(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
}

val a = List(1,2,3,4)
val b = a.aggregate(5)(seq,combine)
val c = a.par.aggregate(5)(seq,combine)


### apply

val a = List(1,2,3,4)
val b = a.apply(1)//a.apply(i)同a(i)
println(b) //2


### canEqual

val a = List()
val b = Array()
println(a.canEqual(b))//true


### charAt

val chars = Array('a','b','c','d')
println(chars.charAt(2))//c


### clone

val a = Array(1,2,3,4)
val b = a.clone()
print(b.mkString(","))//1,2,3,4


### collect

#### collectFirst

val fun: PartialFunction[Char, Char] = {
case 'a' => 'A'
case x => x
}
val a = Array('a', 'b', 'c','a')
val b = a.collect(fun)
println(b.mkString(","))//A,b,c
println(a.collectFirst(fun).mkString(","))//A


### combinations

combinations 表示组合，这个排列组合会选出所有包含字符不一样的组合，但不考虑顺序，对于 “abc”、“cba”，视为相同组合，参数 n 表示序列长度，就是几个字符为一组

val a = Array('a','b','c')
val b = a.combinations(2)//返回一个迭代器
b.foreach(x=> println(x.mkString(",")))
//a,b
//a,c
//b,c


### contains

val a = List(1,2,3,4)
println(a.contains(1)) //true


### containsSlice

val a = List(1,2,3,4)
val b = List(2,3)
println(a.containsSlice(b))//true


### copyToArray(xs)

#### copyToArray(xs, start, len)

val a = Array(1,2,3)
val b:Array[Int] = new Array(5)
println(b.mkString(","))//0,0,0,0,0
a.copyToArray(b)
println(b.mkString(","))//1,2,3,0,0
a.copyToArray(b,1)
println(b.mkString(","))//0,1,2,3,0
a.copyToArray(b,1,2)
println(b.mkString(","))//0,1,2,0,0


### copyToBuffer

val a = Array(1, 2, 3, 4)
val b: ArrayBuffer[Int] = ArrayBuffer()
a.copyToBuffer(b)
println(b.mkString(",")) // 1,2,3,4


### corresponds

val a = Array(1,2,3,4)
val b = Array(2,3,4,5)
println(a.corresponds(b)(_<_))
//true


### count

val a = Array(1,2,3,4)
println(a.count(x => x > 2))
//2


### diff

//diff
val a = Array(1,2,3,4)
val b = Array(2,3,4,5)
println(a.diff(b).mkString(","))
//1


### distinct

val a = Array(1, 2, 2, 3, 4, 4)
val b = a.distinct
println(b.mkString(",")) // 1,2,3,4


### drop

#### dropWhile

val a = Array(1, 2, 3, 4)
val b = a.drop(2)
println(b.mkString(","))
val c = a.dropRight(2)
println(c.mkString(","))
val d = a.dropWhile(x => x > 2)
val e = a.dropWhile(x => x < 2)
println(d.mkString(","))
println(e.mkString(","))
//3,4
//1,2
//1,2,3,4
//2,3,4


### endsWith

val a = Array(1,2,3,4)
val b = 4
println(a.endsWith(Array.fill(1)(b)))


### exists

//exists
val a = Array(1,2,3,4)
println(a.exists(x => x > 3))
//true


### filter

//filter
val a = Array(1, 2, 3, 4)
val b = a.filter(x => x == 2)
println(b.mkString(","))
//2


### find

val a = Array(1,2,3,4)
println(a.find(_.>(3)))
//Some(4)


### flatMap

val a = Array("hello world","hello java","hello hao","hello hao nian java")
a.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.length)).toList.sortBy(_._1).foreach(println)



### flatten

val a = Array(Array(1,2),Array(2,3),Array(3,4))
println(a.flatten.mkString(","))
//1,2,2,3,3,4


### fold

def fun(m:Int,n:Int):Int={
val s = "seq_exp=%d+%d"
println(s.format(m,n))
m+n
}
val a = Array(1,2,3,4)
val b = a.fold(5)(fun) //不分区
println(b)
val  c = a.par.fold(5)(fun)//分区
//seq_exp=5+1
//seq_exp=6+2
//seq_exp=8+3
//seq_exp=11+4
//15
//seq_exp=5+2
//seq_exp=5+4
//seq_exp=5+3
//seq_exp=5+1
//seq_exp=8+9
//seq_exp=6+7
//seq_exp=13+17


### foldLeft

#### foldRight

def fun(m: Int, n: Int): Int = {
val s = "seq_exp=%d+%d"
println(s.format(m, n))
m + n
}

val a = Array(1, 2, 3, 4)
val b = a.foldLeft(5)(fun)
val b = a.foldRight(5)(fun)
println(b)
//foldLeft
//seq_exp=5+1
//seq_exp=6+2
//seq_exp=8+3
//seq_exp=11+4

//foldRight
//seq_exp=4+5
//seq_exp=3+9
//seq_exp=2+12
//seq_exp=1+14


### forall

val a = Array(1,2,3,4)
println(a.forall(_.>(0)))//true
println(a.forall(_.>(2)))//false


### foreach

val a = Array(1,2,3,4)
a.foreach(x=>println(x*10))
//10
//20
//30
//40


### groupBy

val a = Array(1, 2, 3, 4)
val b = a.groupBy(x => x match {
case x if x > 3 => "big"
case _ => "small"
})
b.foreach(x=>println(x._1+": "+x._2.mkString(",")))
//small: 1,2,3
//big: 4


### grouped

val a = Array(1,2,3,4)
val b = a.grouped(3).toList
b.foreach(x=>println("第"+(b.indexOf(x)+1)+"组"+x.mkString(",")))
//第一组 1,2,3
//第二组 4


val a = Array(1,2,3,4)
println(a.hasDefiniteSize)
//true


val a  = Array(1,2,3,4)
//1


val a  = Array(1,2,3,4)
//Some(1)


### indexOf(elem)

#### indexOf(elem,from)

val a = Array(1,2,3,4,1)
println(a.indexOf(1))		//0
println(a.indexOf(1, 1))	//4


### indexOfSlice(that)

#### indexOfSlience(that,from)

val a = Array(1,2,3,2,3,4)
val b = Array(2,3)
println(a.indexOfSlice(b))		//1
println(a.indexOfSlice(b, 2))	//3


### indexWhere§

#### indexWhere(p,from)

val a = Array(1,2,3,4)
println(a.indexWhere(_.equals(2)))//1
println(a.indexWhere(x => x > 1, 2))//2


### indices

val a = Array(1,2,3,4)
println(a.indices.mkString(","))
//0,1,2,3


### init

val a  =Array(1,2,3,4)
println(a.init.mkString(","))
//1,2,3


### inits

val a = Array(1, 2, 3, 4)
//    println(a.init.mkString(","))
val b = a.inits.toList
for (i <- 0 until b.length) {
val s = "第 %d 个值：%s"
println(s.format(i + 1, Option(b(i).mkString(","))))
}
//第 1 个值：Some(1,2,3,4)
//第 2 个值：Some(1,2,3)
//第 3 个值：Some(1,2)
//第 4 个值：Some(1)
//第 5 个值：Some()


### intersect

val a = Array(1,2,3,4)
val b = Array(2,3,4,5)
a.intersect(b).mkString(",").foreach(print)


### isDefinedAt

val a = Array(1,2,3,4)
println(a.isDefinedAt(1))	//true
println(a.isDefinedAt(4))	//false


### isEmpty

val a = Array(1, 2, 3, 4)
val b = Array[Int]()
println(a.isEmpty)		//false
println(b.isEmpty)		//true


### isTraversableAgain

val a = Array(1, 2, 3, 4)
println(a.isTraversableAgain) //true


### iterator

val a = Array(1, 2, 3, 4)
val b = a.iterator
println(b.mkString(","))//1,2,3,4


### last

val a = Array(1,2,3,4)
println(a.last)//4


### lastIndexOf(elem)

#### lastIndecOf(elem,end)

val a = Array(1,2,3,4,1)
println(a.lastIndexOf(1))	//4
println(a.lastIndexOf(1, 3))//0


### lastIndexOfSlice(that)

#### lastIndexOfSlice(that,end)

val a = Array(1, 2, 3, 4,1,2)
val b = ("a")
val c = Array(1,2)
println(a.lastIndexOfSlice(b))		//-1
println(a.lastIndexOfSlice(c))		//4
println(a.lastIndexOfSlice(c, 3))	//0


### lastIndexWhere§

#### lastIndexWhere(p,end)

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


### lastOption

val a = Array(1, 2, 3, 4, 1, 2)
println(a.lastOption)//Some(2)


### length

val a = Array(1, 2, 3, 4)
println(a.length)//4


### lenghCompare(len)

val a = Array(1, 2, 3, 4, 1, 2)
println(a.lengthCompare(3))
println(a.lengthCompare(6))
println(a.lengthCompare(10))


### map

val a = Array(1, 2, 3, 4, 1, 2)
a.map(_.*(10)).mkString(",").foreach(print)
10,20,30,40,10,20


### max

val a = Array(1, 2, 3, 4, 1, 2)
println(a.max)	//4


### maxBy

val a = Array(1, 2, 3, 4, 1, 2)
println(a.maxBy(_.>(2)))	//3


### min

val a = Array(1, 2, 3, 4, 1, 2)
println(a.min)					//1


### minBy

val a = Array(1, 2, 3, 4, 1, 2)
println(a.minBy(_.<(2)))		//2


### mkString

#### mkString(start,sep,end)

val a = Array(1, 2, 3, 4, 1, 2)
println(a.mkString)
println(a.mkString(","))
println(a.mkString("start",",","end"))


### nonEmpty

val a = Array(1,2,3,4)
val b = new Array[Int](0)
println(a.nonEmpty)
println(b.nonEmpty)


val a = Array(1, 2, 3, 4)
println(b.mkString(","))
//1,2,3,4,73,73,73,73,73,73


### par

val a = Array(1, 2, 3, 4)
val b = a.par
println(b.mkString(","))
//1,2,3,4


### partition

val a = Array(1, 2, 3, 4)
val b: (Array[Int], Array[Int]) = a.partition(_%2==0)
b._1.mkString(",").foreach(print)
println
b._2.mkString(",").foreach(print)


### patch

val a = Array(1, 2, 3, 4)
val b = Array(36,7,9)
println(a.patch(1, b, 1).mkString(","))


### permutations

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

val a = Array(1, 2, 3)
a.permutations.toList.foreach(x=> println(x.mkString(",")))
/*
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1*/


### prefixLength

val a = Array(1, 2, 3, 4)
println(a.prefixLength(x => x < 3)) // 2


### product

val a = Array(1, 2, 3, 4)
println(a.product) // 1*2*3*4=24


### reduce

def fun(m:Int,n:Int):Int={
val s = "seq_exp = %d + %d"
println(s.format(m, n))
m + n
}

val a = Array(1,2,3,4)
val b = a.reduce(fun)
println(b)
//seq_exp = 1 + 2
//seq_exp = 3 + 3
//seq_exp = 6 + 4
//10


### reduceLeft

#### reduceRightOption

def fun(m:Int,n:Int):Int={
val s = "seq_exp = %d - %d"
println(s.format(m, n))
m - n
}

val a = Array(1,2,3,4)
println(a.reduceLeft(fun))
println(a.reduceLeftOption(fun))
println(a.reduceRight(fun))
println(a.reduceRightOption(fun))
/*
seq_exp = 1 - 2
seq_exp = -1 - 3
seq_exp = -4 - 4
-8
seq_exp = 1 - 2
seq_exp = -1 - 3
seq_exp = -4 - 4
Some(-8)
seq_exp = 3 - 4
seq_exp = 2 - -1
seq_exp = 1 - 3
-2
seq_exp = 3 - 4
seq_exp = 2 - -1
seq_exp = 1 - 3
Some(-2)*/


### reserve

val a = Array(1, 2, 3, 4)
val b = a.reverse
println(b.mkString(",")) // 4,3,2,1


### reverseIterator

val a = Array(1, 2, 3, 4)
val b = a.reverseIterator
b.foreach(x => print(x + " ")) // 4 3 2 1


### reverseMap

al 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 = a.clone
val c = a.reverse
println(a.sameElements(b))//true
println(a.sameElements(c))//true


### scan

val a = Array(1, 2, 3, 4)
val b = a.scan(5)(_ + _)
println(b.mkString(",")) // 5,6,8,11,15


### scanLeft

#### scanRight

val a = Array(1, 2, 3, 4)
val b = a.scanLeft(5)(_ + _)
val c = a.scanRight(5)(_ + _)
println(b.mkString(",")) // 5,6,8,11,15
println(c.mkString(",")) // 15,14,12,9,5


### segmentLength

val a = Array(1,2,3,4)
println(a.segmentLength(_.>(1), 1))	//3


### seq

val a = Array(1, 2, 3, 4)
val b = a.seq
println(b.mkString(",")) // 1,2,3,4


### sliding(size)

val a = Array(1, 2, 3, 4, 5)
a.sliding(3).toList.foreach(x=> println(x.mkString(",")))
//1,2,3
//2,3,4
//3,4,5


#### sliding(size, step)

val a = Array(1, 2, 3, 4, 5)
a.sliding(3,3).toList.foreach(x=> println(x.mkString(",")))


### sortBy

val a = Array(1,2,3,4)
println(a.sortBy(x => x).mkString(","))//从大到小
println(a.sortBy(x => -x).mkString(","))//从小到大


### sorted

val a = Array(3, 2, 1, 4)
val b = a.sorted // 默认升序排列
println(b.mkString(",")) // 1,2,3,4


### span

val a = Array(1,3,2,4)
val b = a.span(_.<(2))
println(b._1.mkString(","))
println(b._2.mkString(","))
//1
//3,2,4


### splitAt

val a = Array(1, 2, 3, 4)
val b = a.splitAt(2)
println(b._1.mkString(",")) //  1,2
println(b._2.mkString(",")) //  3,4


### startsWith(that)

val a = Array(1, 2, 3, 4)
val b = Array(1, 2)
println(a.startsWith(b)) // true


#### startsWith(that, offset)

val a = Array(1, 2, 3, 4)
val b = Array(2, 3)
println(a.startsWith(b, 1)) // true


### stringPrefix

val a = Array(1, 2, 3, 4)
println(a.toString()) // [I@3ab39c39
println(a.stringPrefix) // [I


### sum

val a = Array(1, 2, 3, 4)
println(a.sum) // 10


### tail

val a = Array(1, 2, 3, 4)
val b = a.tail
println(b.mkString(",")) // 2,3,4


### tails

 val a = Array(1, 2, 3, 4)
val b = a.tails.toList
for (i <- 0 until b.length) {
val s = "第 %d 个值: %s"
println(s.format(i + 1, b(i).mkString(",")))
}
/**
* 第 1 个值: 1,2,3,4
* 第 2 个值: 2,3,4
* 第 3 个值: 3,4
* 第 4 个值: 4
* 第 5 个值:
*/


### take

val a = Array(1, 2, 3, 4)
val b = a.take(3)
println(b.mkString(",")) // 1,2,3


### takeRight

val a = Array(1, 2, 3, 4)
val b = a.takeRight(3)
println(b.mkString(",")) // 2,3,4


### takeWhile

val a = Array(1, 2, 3, 4)
val b = a.takeWhile(x => x < 3)
print(b.mkString(",")) // 1,2


### transpose

val a = Array(Array(1,2,3),Array(4,5,6),Array(7,8,9))
a.foreach(x=> println(x.mkString(",")))
println("---------------------------------------")
a.transpose.foreach(x=> println(x.mkString(",")))
/*
1,2,3
4,5,6
7,8,9
---------------------------------------
1,4,7
2,5,8
3,6,9
*/


### unzip

val chars = Array(("a", "b"), ("c", "d"))
val b = chars.unzip
println(b._1.mkString(",")) // a,c
println(b._2.mkString(",")) // b,d


#### unzip3

val chars = Array(("a", "b", "x"), ("c", "d", "y"), ("e", "f", "z"))
val b = chars.unzip3
println(b._1.mkString(",")) // a,c,e
println(b._2.mkString(",")) // b,d,f
println(b._3.mkString(",")) // x,y,z


### view

val a = Array(1, 2, 3, 4)
val b = a.view(1, 3)
println(b.mkString(",")) // 2,3


### withFilter

val a = Array(1, 2, 3, 4)
val b = a.withFilter(x => x > 2).map(x => x)
println(b.mkString(",")) // 3,4


### zip

val a = Array(1, 2, 3, 4)
val b = Array(4, 3, 2, 1)
val c = a.zip(b)
println(c.mkString(",")) // (1,4),(2,3),(3,2),(4,1)


### zipAll

val a = Array(1, 2, 3, 4, 5, 6, 7)
val b = Array(5, 4, 3, 2, 1)
val c = a.zipAll(b, 8, 9) // (1,5),(2,4),(3,3),(4,2),(5,1),(6,9),(7,9)
println(c.mkString(","))

val x = Array(1, 2, 3, 4)
val y = Array(6, 5, 4, 3, 2, 1)
val z = x.zipAll(y, 8, 9) // (1,6),(2,5),(3,4),(4,3),(8,2),(8,1)
println(z.mkString(","))


### zipWithIndex

 val a = Array('a', 'b', 'c', 'd')
val b = a.zipWithIndex
println(b.mkString(",")) // (a,0),(b,1),(c,2),(d,3)
`
06-08 2852
08-16 978

08-08 2295
10-23 439
08-13 622
02-13 100
12-03 45
12-20 110
02-11 129
12-20 761