您的位置:首页 > 其它

Scala数组,定长数组和变长数组,增强for循环,until用法,数组转换,数组常用算法,数组其它操作

2017-06-28 01:52 447 查看

1. 数组

1.1. 定长数组和变长数组

package cn.toto.scala

//可变数组的长度时需要引入这个包
import scala.collection.mutable.ArrayBuffer

/**
* Created by toto on 2017/6/27.
*/
object ArrayDemo {

def main(args: Array[String]): Unit = {
//初始化一个长度为8的定长数组,其所有元素均为0
val arr1 = new Array[Int](8)
//直接打印定长数组,内容为数组的的hashcode值
println(arr1)

//将数组转换成数组缓冲,就可以看到原数组中的内容了。
//toBuffer会将数组转换成数组缓冲
println(arr1.toBuffer)

//注意:如果没有new,相当于调用了数组的apply方法,直接为数组赋值
//赋初始一个长度为1的定长数组
val arr2 = Array[Int](10)
println(arr2.toBuffer)

//定义一个长度为3的定长数组
var arr3 = Array("hadoop","storm","spark")
//使用()来访问元素
println(arr3(2))

////////////////////////////////////////////////////////////////
//变长数组(数组缓冲)
//如果想使用数组缓冲,需要导入import scala.collection.mutable.ArrayBuffer包
val ab = ArrayBuffer[Int]()
//向数组缓冲的尾部追加一个元素
//+=尾部追加元素
ab += 1
println(ab)
//追加多个元素
ab += (2,3,4,5)
println(ab)
//追加一个数组++=
ab ++= Array(6,7)
println(ab)
//追加一个数组缓冲
ab ++= ArrayBuffer(8,9)
//打印数组缓冲ab
println(ab)

//在数组某个位置插入元素用inseret,其中第一个参数是其实位置,后面两个参数是要添加进入的值
ab.insert(0,-1,0)
println(ab)

//删除数组某个位置的元素用remove,下面的含义是从0这个位置开始,删除2个元素
ab.remove(0,2)
println(ab)
}
}


运行后的结果如下:

[I@4563e9ab
ArrayBuffer(0, 0, 0, 0, 0, 0, 0, 0)
ArrayBuffer(10)
spark
ArrayBuffer(1)
ArrayBuffer(1, 2, 3, 4, 5)
ArrayBuffer(1, 2, 3, 4, 5, 6, 7)
ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)
ArrayBuffer(-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)


1.2. 遍历数组

1.增强for循环

2.好用的until会生成脚标,0 until 10 包含0不包含10



package cn.toto.scala

/**
* Created by toto on 2017/6/28.
*/
object ForArrayDemo {

def main(args: Array[String]): Unit = {
//初始化一个数组
val arr = Array(1,2,3,4,4,5,6,7,8)
//增强for循环
for(i <- arr)
print(i + " ")

println("")

//好用的until会生成一个Range
//reverse是将前面生成的Range反转
for(i <- (0 until arr.length).reverse)
print(arr(i) + " ")
}
}


运行后的结果如下:

1 2 3 4 4 5 6 7 8
8 7 6 5 4 4 3 2 1


1.3. 数组转换

yield关键字将原始的数组进行转换会产生一个新的数组,原始的数组不变



package cn.toto.scala

/**
* Created by toto on 2017/6/28.
*/
object ArrayYieldDemo {

def main(args: Array[String]): Unit = {
//定义一个数组
val arr = Array(1,2,3,4,5,6,7,8,9)
//将偶数取出乘以10后再生成一个新的数组
val res = for(e <- arr if e % 2 == 1) yield e * 10
println(res.toBuffer)

//更高级的写法,用着更爽
//filter是过滤,接收一个返回值为boolean的函数
//map相当于将数组中的每一个元素取出来,应用传进去的函数
val r = arr.filter(_ % 2 == 0).map(_ * 10)
println(r.toBuffer)
}
}


运行后的结果如下:

ArrayBuffer(10, 30, 50, 70, 90)
ArrayBuffer(20, 40, 60, 80)


1.4. 数组常用算法

在Scala中,数组上的某些方法对数组进行相应的操作非常方便!



其它数组操作:

scala> var arr = Array(1,6,5,4,7,9,2,25,22,11)
arr: Array[Int] = Array(1, 6, 5, 4, 7, 9, 2, 25, 22, 11)

升序排序
scala> arr.sorted
res13: Array[Int] = Array(1, 2, 4, 5, 6, 7, 9, 11, 22, 25)

降序
scala> arr.sorted.reverse
res14: Array[Int] = Array(25, 22, 11, 9, 7, 6, 5, 4, 2, 1)

scala> arr.sortWith(_>_)
res15: Array[Int] = Array(25, 22, 11, 9, 7, 6, 5, 4, 2, 1)

上面的等价下面的,相当于是降序:
scala> arr.sortWith((x,y) => x > y)
res16: Array[Int] = Array(25, 22, 11, 9, 7, 6, 5, 4, 2, 1)

下面是升序
scala> arr.sortWith((x,y) => x < y)
res17: Array[Int] = Array(1, 2, 4, 5, 6, 7, 9, 11, 22, 25)

scala> val a = Array("hadoop",1.0,2)
a: Array[Any] = Array(hadoop, 1.0, 2)

scala> a(1).asInstanceOf[Double]
res20: Double = 1.0

scala> val arr1 = new Array[Int](8)
arr1: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)

scala> arr1(1) = 6

scala> arr1
res26: Array[Int] = Array(0, 6, 0, 0, 0, 0, 0, 0)

定义变长数组,需要引入包:
scala> val ab = new ArrayBuffer[Int]()
ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> ab += 1
res27: ab.type = ArrayBuffer(1)

scala> ab
res28: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)

scala> ab += 2
res30: ab.type = ArrayBuffer(1, 2)

scala> ab += (2,3,4,5)
res31: ab.type = ArrayBuffer(1, 2, 2, 3, 4, 5)

scala>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: