示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
(12 +: a1).foreach(x=>print(x+"\t"))
示例:
var a2 = Array("a","b","c")
a2.foreach(x=>print(x+"\t"))
println()
(a2 :+ 13).foreach(x=>print(x+"\t"))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
a1.combinations(3).foreach(x=>println(x.mkString("-")))
示例:
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
a2.distinct.foreach(x=>print(x+"\t"))
示例:
删除前n个元素
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
a2.drop(2).foreach(println)
示例:
删除后n个元素
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
a1.dropRight(3).foreach(x=>print(x+"\t"))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
a1.dropWhile(x=>x<3&&x>1).foreach(x=>print(x+"\t"))
println()
a1.dropWhile(x=>x<3&&x>=1).foreach(x=>print(x+"\t"))
示例:
array2.foreach(x=>print(x+"\t"));println()
println(array2.max)
示例:
array2.foreach(x=>print(x+"\t"));println()
println(array2.min)
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.maxBy(_ > 1))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.minBy(_ < 6))
示例:
b1.foreach(x=>print(x+"\t"));println()
b1.padTo(20,"-").foreach(x=>print(x+"\t"))
b1.padTo(4,"-").foreach(x=>print(x+"\t"))
示例:
b1.foreach(x => print(x + "\t"));println()
var i: Array[Int] = Array() //空集合会报错,用这种形式
b1.patch(0, Array(12, 13, 14), 3).foreach(x => print(x + "\t"));println()
b1.patch(2, Array(12, 13, 14), 2).foreach(x => print(x + "\t"));println()
b1.patch(3, i, 1).foreach(x => print(x + "\t"));println()
b1.patch(3, i, 2).foreach(x => print(x + "\t"));println()
b1.patch(3, "", 2).foreach(x => print(x + "\t"));println()
示例:
a1.foreach(x=>print(x+"\t"));println()
a1.permutations.foreach(x => {
println(x.mkString(":", "*", ":"))
})
示例:
b3.foreach(x=>print(x+"\t"));println()
println(b3.prefixLength(_ < 5))
println(b3.prefixLength(_ > 5))
示例:
b7.foreach(x => print(x + "\t"));println()
b7.sorted.transform(Math.pow(_,2).toInt).foreach(x => print(x + "\t"));println()
示例:
b7.foreach(x => print(x + "\t"));println()
b7.update(3,20)
b7.foreach(x => print(x + "\t"));println()
示例:
b7.foreach(x => print(x + "\t"));println()
b7.updated(3,40).foreach(x => print(x + "\t"));println()
b7.foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.head)
示例:
b1.foreach(x=>print(x+"\t"));println()
var b0:Array[Int] = Array()
println(b1.headOption)
println(b0.headOption)
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.last)
示例:
b1.foreach(x=>print(x+"\t"));println()
var b0:Array[Int] = Array()
println(b0.lastOption)
println(b1.lastOption)
示例:
b1.foreach(x=>print(x+"\t"));println()
b1.tail.foreach(x=>print(x+"\t"));println()
示例:
b1.foreach(x=>print(x+"\t"));println()
b1.tails.foreach(x=>{
x.foreach(y=>print(s"$y,"));println()})
示例:
b1.foreach(x=>print(x+"\t"));println()
b1.init.foreach(x=>print(x+"\t"));println()
示例:
b1.foreach(x=>print(x+"\t"));println()
b1.inits.foreach(x=>{
x.foreach(y=>print(s"$y,"));println()})
示例:
b1.foreach(x => print(x + "\t"));println()
b1.take(3).foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.takeRight(3).foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.takeWhile(_>2).foreach(x => print(x + "\t"));println()
b1.takeWhile(_%2!=0).foreach(x => print(x + "\t"));println()
示例:
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
val add = (x:Int,y:Int)=>{
var z=x+y
println(s"$x+$y=$z")
z
}
println(a2./:(0)(add))
println(a2.:\(0)(add))
示例:
aggregate的简化版
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
val add = (x:Int,y:Int)=>{
var z=x+y
println(s"$x+$y=$z")
z
}
println(a2.fold(0)(add))
示例:
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
val add = (x:Int,y:Int)=>{
var z=x+y
println(s"$x+$y=$z")
z
}
println(a2.foldRight(0)(add))
示例:
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
val add = (x:Int,y:Int)=>{
var z=x+y
println(s"$x+$y=$z")
z
}
println("我是fold")
println(a2.fold(0)(add))
println("我是foldRight")
println(a2.foldRight(0)(add))
println("我是foldLeft")
println(a2.foldLeft(0)(add))
示例:
b3.foreach(x=>print(x+"\t"));println()
println(b3.product)
示例:
b3.foreach(x => print(x + "\t"));
println()
println(b3.reduce((x, y) => {
var z = x + y
println(s"$x+$y=$z")
z
}))
示例:
b3.foreach(x => print(x + "\t"));
println()
println(b3.reduceLeft((x, y) => {
var z = x + y
println(s"$x+$y=$z")
z
}))
示例:
b3.foreach(x => print(x + "\t"));
println()
println(b3.reduceRight((x, y) => {
var z = x + y
println(s"$x+$y=$z")
z
}))
示例:
b3.foreach(x => print(x + "\t"));
println()
println(b3.reduceOption((x, y) => {
var z = x + y
println(s"$x+$y=$z")
z
}))
示例:
b3.foreach(x => print(x + "\t"));
println()
println(b3.reduceLeftOption((x, y) => {
var z = x + y
println(s"$x+$y=$z")
z
}))
示例:
b3.foreach(x => print(x + "\t"));
println()
println(b3.reduceRightOption((x, y) => {
var z = x + y
println(s"$x+$y=$z")
z
}))
示例:
b1.foreach(x => print(x + "\t"));println()
b1.scan(0)(_+_).foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.scanLeft(0)(_+_).foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.scanRight(0)(_+_).foreach(x => print(x + "\t"));println()
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"));println()
println(a1.sum)
示例:
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
var builder = new StringBuilder
a2.addString(builder," - ")
println(builder)
a1.addString(builder,"[",",","]")
println(builder)
var builder2 = new StringBuilder
println(a1.addString(builder2, "^"): StringBuilder)
示例:
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println(a1.mkString("[", "-", "]"))
var lst = new ArrayBuffer[Int]
for (elem <- 1 to 5) {
lst.append(Random.nextInt(100))
}
println(lst.mkString(")"))
示例:
b1.foreach(x => print(x + "\t"));println()
b1.stringPrefix.foreach(x=>print(x+"\t"));println()
示例:
聚合
val add = (x:Int,y:Int)=>{
var z=x+y
println(s"$x+$y=$z")
z
}
var lst = new ArrayBuffer[Int]
for (elem <- 1 to 5) {
lst.append(Random.nextInt(100))
}
println(lst.par.aggregate(0)(add, add))
示例:
返回的结果是个偏函数,
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println(a1.andThen(x => {
if (x % 3 == 0) {
"match"
}
})(1))
println(a1.andThen(x => {
if (x % 3 == 0) Some(x)
else None
})(2))
def add(x:Int):Int=x*2
def mul(x:Int):Int=2+x
def rst1 = add _ andThen (mul)
def rst2 = add _ andThen mul _
println(rst1(3))
println(rst2(3))
示例:
def add(x:Int):Int=x*2
def mul(x:Int):Int=2+x
def rst1 = add _ compose(mul)
def rst2 = add _ compose mul _
println(rst1(3))
println(rst2(3))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println(a1.applyOrElse(10, {
i: Int => 200 }))
println(a1.applyOrElse(3, {
i: Int => 200 }))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
a1.collect({
case i if i % 2 == 0 => i }).foreach(x=>{
print(x+"\t")})
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println(a1.collectFirst({
case i if i % 2 == 0 => i }))
示例:
其他案例的输出基本上都是foreach
b1.foreach(x=>print(x+"\t"));println()
b1.foreach(x=>print(s"$x,"));println()
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"));println()
val it = a1.iterator
while (it.hasNext) {
println(it.next() * 3)
}
示例:
var a1 = Array.range(1, 5)
a1.foreach(x => print(x + "\t"));println()
a1.map(x=>x*2).foreach(x => print(x + "\t"));println()
val a2 = Array(("1001", "henry", "male", 100), ("1002", "pola", "female", 130), ("1003", "ariel", "female", 200))//元组的数组
a2.foreach(x=>print(x+"\t"));println()
a2.map(x=>(x._3,x._4)).foreach(x=>print(x+"\t"))
示例:
val pf4: PartialFunction[Int, Int] = (x: Int) => x match {
case x if (x % 2 == 0) => x
}
pf4.runWith(println)(6)
pf4.runWith(println)(5)//偏函数,不输出,因为函数里没设置
pf4.runWith(println)(4)
var b = Array.range(1, 20)
b.runWith(print)(18);println()
b.runWith(print)(21);println()
b.runWith(print)(17);println()
println(b.runWith(x => x match {
case x if x % 2 == 0 => x
})(17))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println(a1.contains(5))
println(a1.contains(3))
var a1 = Array.range(1, 5)
a1.foreach(x => print(x + "\t"))
println()
println(a1.contains(1))
println(a1.contains(0))
println(a1.contains("a"))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println(a1.containsSlice(Array(1,2)))
println(a1.containsSlice(Array(1,3)))
var a1 = Array.range(1, 5)
a1.foreach(x => print(x + "\t"))
println()
var a2 = Array.range(1, 4)
a2.foreach(x => print(x + "\t"))
println()
var a3 = Array.range(2, 6)
a3.foreach(x => print(x + "\t"))
println()
println(a1.containsSlice(a1))
println(a1.containsSlice(a2))
println(a1.containsSlice(a3))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println(a1.count(_ !=0))
println(a1.count(_ > 1))
println(a1.count(x => x > 3 && x < 9))
示例:
var a1 = Array.range(1, 5)
a1.foreach(x => print(x + "\t"))
println()
var a2 = Array.range(1,3)
a2.foreach(x => print(x + "\t"))
println()
var a3 = Array.range(4,5)
a3.foreach(x => print(x + "\t"))
println()
println(a1.endsWith(a2))
println(a1.endsWith(a3))
示例:
var a1 = Array.range(1, 5)
a1.foreach(x => print(x + "\t"))
println()
var a2 = Array.range(1,3)
a2.foreach(x => print(x + "\t"))
println()
var a3 = Array.range(4,5)
a3.foreach(x => print(x + "\t"))
println()
println(a1.startsWith(a2))
println(a1.startsWith(a3))
println(a1.startsWith(a3,3))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"));println()
println(a1.exists(_ > 6))
println(a1.exists(_ > 3))
示例:
var a1 = Array.range(1, 5)
a1.foreach(x => print(x + "\t"))
println()
a1.filter(_ % 2 == 0).foreach(x=>print(x+"\t"))
println()
a1.filter(x=> x % 2 == 0).foreach(x=>print(x+"\t"))
println()
a1.filterNot(x=> x % 2 == 0).foreach(x=>print(x+"\t"))
示例:
b7.foreach(x => print(x + "\t"));println()
b7.withFilter(_>4).foreach(x => print(x + "\t"));println()
示例:
var a1 = Array.range(1, 5)
a1.foreach(x => print(x + "\t"))
println()
println(a1.find(_ > 2))
println(a1.find(_ > 5))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.forall(_ > 0))
println(b1.forall(_ < 0))
println(b1.forall(_ > 2))
println(b1.forall(_ < 10))
println(b1.forall(_.isInstanceOf[Int]))
println(b1.forall(_ % 2 == 0))
示例:
println(b1.hasDefiniteSize)
println(b1.toStream)
println(b1.toStream.hasDefiniteSize)
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.isDefinedAt(5))
println(b1.isDefinedAt(7))
println(b1.isDefinedAt(2))
示例:
a2.foreach(x=>print(x+"\t"));println()
println(a2.isEmpty)
示例:
a2.foreach(x=>print(x+"\t"));println()
println(a2.nonEmpty)
示例:
array2.foreach(x=>print(x+"\t"));println()
println(array2.isTraversableAgain)
println(array2.toIterator.isTraversableAgain)
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println(a1.apply(0))
println(a1.apply(1))
println(a1.apply(3))
示例:
var a3 = Array('a','b','c','d')
a3.foreach(x=>print(x+"\t"))
println()
println(a3.charAt(2))
println(a3.charAt(3))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.indexOf(2))
println(b1.indexOf(8))
println(b1.indexOf(5))
println(b1.indexOf(1,2))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.indexOfSlice(Array(6,5)))
println(b1.indexOfSlice(Array(6,7)))
println(b1.indexOfSlice(Array(8,6)))
println(b1.indexOfSlice(Array(8,6),2))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.indexWhere(_==6))
println(b1.indexWhere(_>2))
println(b1.indexWhere(_==6,3))
println(b1.indexWhere(_>2,3))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.lastIndexOf(2))
println(b1.lastIndexOf(8))
println(b1.lastIndexOf(5))
println(b1.lastIndexOf(1,2))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.lastIndexOfSlice(Array(6,7)))
println(b1.lastIndexOfSlice(Array(8,6)))
println(b1.lastIndexOfSlice(Array(8,6),2))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.lastIndexWhere(_==6))
println(b1.lastIndexWhere(_>2))
println(b1.lastIndexWhere(_==6,3))
println(b1.lastIndexWhere(_>2,3))
示例:
b1.foreach(x=>print(x+"\t"));println()
println(b1.indices)
b1.indices.foreach(x=>print(x+"\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.slice(2,4).foreach(x => print(x + "\t"));println()
示例:
b5.foreach(x => print(x + "\t"));println()
println(b5.subSequence(1, 3))
示例:
b1.foreach(x => print(x + "\t"));println()
b1.view(2,4).foreach(x => print(x + "\t"));println()
println(b1.view(2, 4))
示例:
var add2 = (x: Int, y: Int) => {
val z = x + y
println(s"$x + $y = $z")
z
}
var array2 = Array(1, 2, 3, 5, 7)
println(array2.groupBy(x => x match {
case i if i % 2 == 0 => "even"
case i => "odd"
}).map(x => (x._1, x._2.par.fold(0)(add2))))
示例:
var array2 = Array(1, 2, 3, 5, 7)
array2.grouped(2).foreach(x=>{
x.foreach(y=>print(y+"\t"))
println()
})
var array2 = Array(1, 2, 3, 5, 7)
println(array2.grouped(2).map(x => x.fold(0)(add2)).fold(0)(add2))
示例:
var lst = new ArrayBuffer[Int]
for (elem <- 1 to 5) {
lst.append(Random.nextInt(100))
}
lst.foreach(x=>print(x+"\t"))
println()
println(lst.par)
元组
示例:
b1.foreach(x=>print(x+"\t"));println()
b1.partition(_>5)._2.foreach(x=>print(x+"\t"))
println()
b1.partition(_>5)._1.foreach(x=>print(x+"\t"))
println()
示例:
a3.foreach(x => print(x + "\t"));println()
a3.span(_ <= 2)._1.foreach(x => print(x + "\t"));println()
a3.span(_ <= 2)._2.foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.splitAt(3)._1.foreach(x => print(x + "\t"));println()
b1.splitAt(3)._2.foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.sliding(2).foreach(x =>{
x.foreach(y=>print(y+"\t"));println()});println()
println()
b1.sliding(2,2).foreach(x =>{
x.foreach(y=>print(y+"\t"));println()});println()
println()
b1.sliding(3).foreach(x =>{
x.foreach(y=>print(y+"\t"));println()});println()
println()
b1.sliding(3,2).foreach(x =>{
x.foreach(y=>print(y+"\t"));println()});println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.sortBy(x=>x).foreach(x => print(x + "\t"));println()
b1.sortBy(x=>(-x)).foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.sorted.foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
b1.sortWith(_>_).foreach(x => print(x + "\t"));println()
b1.sortWith(_<_).foreach(x => print(x + "\t"));println()
示例:
array2.foreach(x=>print(x+"\t"));println()
println(array2.length)
println(array2.size)
示例:
array2.foreach(x=>print(x+"\t"));println()
println(array2.length)
println(array2.lengthCompare(5))
println(array2.lengthCompare(3))
println(array2.lengthCompare(7))
示例:
b1.foreach(x => print(x + "\t"));println()
println(b1.segmentLength(_ > 4, 0))
println(b1.segmentLength(_ > 4, 1))
println(b1.segmentLength(_ > 4, 2))
示例:
array2.foreach(x=>print(x+"\t"));println()
println(array2.length)
println(array2.size)
示例:
var a1=Array.range(1,5)
var a2 = Array("a","b","c")
a1.foreach(x=>print(x+"\t"))
println()
a2.foreach(x=>print(x+"\t"))
println()
println("----++---------------")
//++合并
(a1++a2).foreach(x=>print(x+"\t"))
示例:
var a1=Array.range(1,5)
var a2 = Array(1.0,1,2)
a1.foreach(x=>print(x+"\t"))
println()
a2.foreach(x=>print(x+"\t"))
println()
(a1++:a2).foreach(x=>print(x+"\t"))//x为double类型
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
var a2 = Array("a","b","c")
a2.foreach(x=>print(x+"\t"))
println()
println(a2.canEqual(a1))
示例:
var a1=Array.range(1,5)
var a2=Array.range(8,12)
a1.foreach(x=>print(x+"\t"));println()
a2.foreach(x=>print(x+"\t"));println()
a1.union(a2).foreach(x=>print(x+"\t"));println()
(a1 union a2).foreach(x=>print(x+"\t"));println()
示例:
var a1=Array.range(1,5)
var a2=Array.range(8,12)
a1.foreach(x=>print(x+"\t"));println()
a2.foreach(x=>print(x+"\t"));println()
println(a1.corresponds(a2)((x, y) => x + 7 == y))
println(a1.corresponds(a2)((y, x) => x + 7 == y))
示例:
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println("-----diff--------")
//取差集
a1.diff(a2).foreach(println)
println("-----intersect--------")
//取交集
a1.intersect(a2).foreach(println)
示例:
var a2 = Array(1,1,2)
a2.foreach(x=>print(x+"\t"))
println()
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
println("-----diff--------")
//取差集
a1.diff(a2).foreach(println)
println("-----intersect--------")
//取交集
a1.intersect(a2).foreach(println)
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
var a3 = a1.clone()
println(a1 == a3)
//重写same方法,比较值
def same(a1:Array[Int],a2:Array[Int]):Boolean={
var same:Boolean=true
if (a1.size!=a2.size) {
same = false
}else{
breakable{
for (elem <- 0 until a1.size) {
if (a1(elem)!=a2(elem)) {
same=false
break()
}
}
}
}
same
}
println(same(a1, a3))
println(a1.sameElements(a3))
示例:
var a1=Array.range(1,5)
a1.foreach(x=>print(x+"\t"))
println()
var a3 = a1.clone()
println(a1 == a3)
println(a1.sameElements(a3))
示例:
var lst2= new ArrayBuffer[Int]
for (elem <- 1 to 6) {
lst2.append(Random.nextInt(100))
}
var arr1 = Array(0,0,0,0)
var arr2 = Array(0,0,0,0)
var arr3 = Array(0,0,0,0)
var arr4 = Array(0,0,0,0)
lst2.foreach(x=>print(x+"\t"))
println()
lst2.copyToArray(arr1)
lst2.copyToArray(arr2,2)
lst2.copyToArray(arr3,0,2)
lst2.copyToArray(arr4,1,2)
arr1.foreach(x=>print(x+"\t"))
println()
arr2.foreach(x=>print(x+"\t"))
println()
arr3.foreach(x=>print(x+"\t"))
println()
arr4.foreach(x=>print(x+"\t"))
示例:
var lst2= new ArrayBuffer[Int]
for (elem <- 1 to 6) {
lst2.append(Random.nextInt(100))
}
lst2.foreach(x=>print(x+"\t"))
println()
var buff = new ArrayBuffer[Int]
println(buff.size)
lst2.copyToBuffer(buff)
println(buff.size)
buff.foreach(x=>print(x+"\t"))
示例:
b1.foreach(x => print(x + "\t"));println()
b1.seq.foreach(x => print(x + "\t"));println()
println(b1.seq)
示例:
b1.foreach(x => print(x + "\t"));println()
b1.reverse.foreach(x => print(x + "\t"));println()
示例:
b1.foreach(x => print(x + "\t"));println()
println(b1.isTraversableAgain)
b1.reverseIterator.foreach(x => print(x + "\t"));println()
println(b1.reverseIterator.isTraversableAgain)
示例:
b1.foreach(x => print(x + "\t"));println()
b1.map(x=>print(x + "\t"));println()
b1.reverseMap(x=>print(x + "\t"));println()
示例:
val a2 = Array(Array(1,3,5),Array(2,4,6))
a2.flatMap(x=>x.map(x=>(x,1))).foreach(x=>print(x+"\t"))
println()
a2.flatMap(x=>x.map(x=>(if(x%2==0) "even" else "odd",1))).foreach(x=>print(x+"\t"))
println()
示例:
val a2 = Array(Array(1,3,5),Array(2,4,6),Array(7,8,9))
a2.foreach(x => {
x.foreach(y=>print(y + "\t"))
println()
})
a2.flatten.foreach(x => print(x + "\t"))
示例:
var b9=Array(Array(1,2,3),Array(4,5,6),Array(7,8,9),Array(11,12,13),Array(21,22,3))
b9.foreach(x =>{
x.foreach(y=>print(y+"\t"));println()});println()
println()
b9.transpose.foreach(x =>{
x.foreach(y=>print(y+"\t"));println()});println()
示例:
var b10=Array(("henry",19),("aaaa",12))
b10.unzip._1.foreach(x => print(x + "\t"));println()
b10.unzip._2.foreach(x => print(x + "\t"));println()
示例:
var b11=Array(("henry",19,4),("aaaa",12,4))
b11.unzip3._1.foreach(x => print(x + "\t"));println()
b11.unzip3._2.foreach(x => print(x + "\t"));println()
b11.unzip3._3.foreach(x => print(x + "\t"));println()
示例:
var c1=Array(1,2,3)
var c2=Array("a","b","c")
c1.zip(c2).foreach(x=>print(x+"\t"));println()
示例:
var c3=Array(1,2,3)
var c4=Array("a","b","c","d")
c4.zipAll(c3,0,"z").foreach(x=>print(x+"\t"));println()
c3.zipAll(c4,0,"z").foreach(x=>print(x+"\t"));println()
定义:def zipWithIndex: Array[(A, Int)]
描述:序列中的每个元素和它的索引组成一个元组数组
示例:
var c5=Array("a","b","c","d")
c5.zipWithIndex.foreach(x=>print(x+"\t"));println()
示例:
示例:
示例:
示例:
示例:
示例:
示例:
val b8 = Array((1,"a"),(3,"c"),(4,"d"))
b8.foreach(x=>print(x+"\t"));println()
println(b8.toMap)
示例:
示例:
var b7=Array(2,1,4,3,5,2,1)
b7.foreach(x => print(x + "\t"));println()
println(b7.toSet)
示例:
b7.foreach(x => print(x + "\t"));println()
println(b7.toStream)
println(b7.toStream.head)
println(b7.toStream.tail)
println(b7.inits.toStream)
示例: