简介
Scala 语言中提供的数组是用来存储固定大小的同类型元素。声明数组是声明一个就像numbers这样的变量,然后使用 numbers[0]、numbers[1]、…、numbers[99] 来表示一个个单独的变量。数组中某个指定的元素是通过索引来访问的。
数组的第一个元素索引为0,最后一个元素的索引为元素总数减1。
声明数组
数组语法格式:
var z:Array[String] = new Array[String](3)
或
var z = new Array[String](3)
z(0) = "Runoob"; z(1) = "Baidu"; z(4/2) = "Google"
或
var z = Array("Runoob", "Baidu", "Google")
简单实例
定长数组(Array)
//定义一个长度为10的数值数组
scala> val numberArray=new Array[Int](10)
numberArray: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
//定义一个长度为10的String类型数组
scala> val strArray=new Array[String](10)
strArray: Array[String] = Array(null, null, null, null, null, null, null, null,
null, null)
//可以看出:复杂对象类型在数组定义时被初始化为null,数值型被初始化为0
//数组元素赋值
scala> strArray(0)="First Element"
//需要注意的是,val strArray=new Array[String](10)
//这意味着strArray不能被改变,但数组内容是可以改变的
scala> strArray
res62: Array[String] = Array(First Element, null, null, null, null, null, null,
null, null, null)
//另一种定长数组定义方式
//这种调用方式其实是调用其apply方法进行数组创建操作
scala> val strArray2=Array("First","Second")
strArray2: Array[String] = Array(First, Second)
变长数组(ArrayBuffer)
//要使用ArrayBuffer,先要引入scala.collection.mutable.ArrayBuffer
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
//创建String类型ArrayBuffer数组缓冲
scala> val strArrayVar=ArrayBuffer[String]()
strArrayVar: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
//+=意思是在尾部添加元素
scala> strArrayVar+="Hello"
res63: strArrayVar.type = ArrayBuffer(Hello)
//+=后面还可以跟多个元素的集合
//注意操作后的返回值
scala> strArrayVar+=("World","Programmer")
res64: strArrayVar.type = ArrayBuffer(Hello, World, Programmer)
//显示完整数组内容
scala> strArrayVar
res65: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Hello, World,
Programmer)
//++=用于向数组中追加内容,++=右侧可以是任何集合
//追加Array数组
scala> strArrayVar++=Array("Wllcome","To","XueTuWuYou")
res66: strArrayVar.type = ArrayBuffer(Hello, World, Programmer, Wllcome, To, Xue
TuWuYou)
//追加List
scala> strArrayVar++=List("Wellcome","To","XueTuWuYou")
res67: strArrayVar.type = ArrayBuffer(Hello, World, Programmer, Wllcome, To, Xue
TuWuYou, Wellcome, To, XueTuWuYou)
//删除末尾n个元素
scala> strArrayVar.trimEnd(3)
scala> strArrayVar
res69: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Hello, World,
Programmer, Wllcome, To, XueTuWuYou)
//创建整型数组缓冲
scala> var intArrayVar=ArrayBuffer(1,1,2)
intArrayVar: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
//在数组索引为0的位置插入元素6
scala> intArrayVar.insert(0,6)
scala> intArrayVar
res72: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(6, 1, 1, 2)
//在数组索引为0的位置插入元素7,8,9
scala> intArrayVar.insert(0,7,8,9)
scala> intArrayVar
res74: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(7, 8, 9, 6, 1, 1,2)
//从索引0开始,删除4个元素
scala> intArrayVar.remove(0,4)
scala> intArrayVar
res77: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
//转成定长数组
scala> intArrayVar.toArray
res78: Array[Int] = Array(1, 1, 2)
//将定长数组转成ArrayBuffer
scala> res78.toBuffer
res80: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 1, 2)
数组遍历
//to
scala> for(i <- 0 to intArrayVar.length-1) println("Array Element: " +intArrayVar(i))
Array Element: 1
Array Element: 1
Array Element: 2
//until
scala> for(i <- 0 until intArrayVar.length) println("Array Element: " +intArrayVar(i))
Array Element: 1
Array Element: 1
Array Element: 2
//数组方式(推荐使用)
scala> for(i <- intArrayVar) println("Array Element: " + i)
Array Element: 1
Array Element: 1
Array Element: 2
//步长为2
scala> for(i <- 0 until (intArrayVar.length,2)) println("Array Element: " +intA
rrayVar(i))
Array Element: 1
Array Element: 2
//倒序输出
scala> for( i<- (0 until intArrayVar.length).reverse) println("Array Element: "+
intArrayVar(i))
Array Element: 2
Array Element: 1
Array Element: 1
数组转换
//生成新的数组,原数组不变
//缓冲数据转换后产生的仍然是缓冲数组
scala> var intArrayVar2=for(i <- intArrayVar) yield i*2
intArrayVar2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 2, 4)
//定长数组转转后产生的仍然是定长数组,原数组不变
scala> var intArrayNoBuffer=Array(1,2,3)
intArrayNoBuffer: Array[Int] = Array(1, 2, 3)
scala> var intArrayNoBuffer2=for(i <- intArrayNoBuffer) yield i*2
intArrayNoBuffer2: Array[Int] = Array(2, 4, 6)
//加入过滤条件
scala> var intArrayNoBuffer2=for(i <- intArrayNoBuffer if i>=2) yield i*2
intArrayNoBuffer2: Array[Int] = Array(4, 6)
常用运算
//定义一个整型数组
scala> val intArr=Array(1,2,3,4,5,6,7,8,9,10)
intArr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
//求和
scala> intArr.sum
res87: Int = 55
//求最大值
scala> intArr.max
res88: Int = 10
scala> ArrayBuffer("Hello","Hell","Hey","Happy").max
res90: String = Hey
//求最小值
scala> intArr.min
res89: Int = 1
//toString()方法
scala> intArr.toString()
res94: String = [I@141aba8
//mkString()方法
scala> intArr.mkString(",")
res96: String = 1,2,3,4,5,6,7,8,9,10
scala> intArr.mkString("<",",",">")
res97: String = <1,2,3,4,5,6,7,8,9,10>
多维数组
//定义2行3列数组
scala> var multiDimArr=Array(Array(1,2,3),Array(2,3,4))
multiDimArr: Array[Array[Int]] = Array(Array(1, 2, 3), Array(2, 3, 4))
//获取第一行第三列元素
scala> multiDimArr(0)(2)
res99: Int = 3
//多维数组的遍历
scala> for(i <- multiDimArr) println( i.mkString(","))
1,2,3
2,3,4