scala中的数组分为定长数组(Array)和可变长数组(ArrayBuffer)
定长数组(Array):
1.声明
a.利用Array类中定义的apply方法直接声明
scala> val arr = Array("leon","ada","jill")
arr: Array[String] = Array(leon, ada, jill)
如果为空数组则数据类型默认为Nothing
scala> val arr6 = Array()
arr6: Array[Nothing] = Array()
b.类似java中的声明方法,需要初始化数组长度
scala> val arr7 = new Array(4)
arr7: Array[Nothing] = Array(null, null, null, null)
默认的数据类型也是Nothing
也可以定义数据类型
scala> val arr7 = new Array(4)
arr7: Array[Nothing] = Array(null, null, null, null)
注明:如果声明数组中的元素类型不同,则数组类型取他们的公共父类
scala> val arr3 = Array("kris",30)
arr3: Array[Any] = Array(kris, 30)
2.访问元素
使用()访问数组中元素对应的下角标
scala> println(arr(2))
jill
3.修改元素
scala> arr(2) = "wilson"
scala> println(arr(2))
wilson
4.元素类型判断和转换
scala> val arr3 = Array("kris",30)
arr3: Array[Any] = Array(kris, 30)
scala> val name = arr3(0).asInstanceOf[String]
name: String = kris
scala> val age = arr3(1).asInstanceOf[Int]
age: Int = 30
scala> print(s"name: ${name}, age: ${age}")
name: kris, age: 30
scala> val flag = arr3(1).isInstanceOf[Int]
flag: Boolean = true
可变长数组(ArrayBuffer):
1.声明
需要导包
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> val arr4 = ArrayBuffer[String]()
arr4: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
2.添加去除元素
scala> arr4 += "joey"
res3: arr4.type = ArrayBuffer(joey)
scala> arr4 -= "joey"
res9: arr4.type = ArrayBuffer()
3.添加去除数组
scala> arr4 ++= arr
res4: arr4.type = ArrayBuffer(joey, leon, ada, jill)
scala> arr4 --= arr
res10: arr4.type = ArrayBuffer(joey)
4.常用方法
insert:在数组某个地方插入一个或者多个元素
scala> arr4.insert(1,"cris")
scala> arr4
res6: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(joey, cris, leon, ada, jill)
scala> arr4.insert(2,"yasuo","mangsen")
scala> arr4
res8: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(joey, cris, yasuo, mangsen, leon, ada, jill)
remove:与insert使用方法相同,作用相反,为去除删除元素
scala> arr4.remove(2)
res11: String = mangsen
scala> arr4
res12: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(cris, yasuo)
scala> arr4.remove(0,2)
scala> arr4
res14: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
注意:删除单个元素的时候返回的是删除的那个元素
补充:
a.ArrayBuffer与Array的区别:
1.ArrayBuffer是变长的,Array是定长的
2.ArrayBuffer使用是需要导包
3.Array类似与java中的数组,ArrayBufffer类似于java中的ArrayList
4.一般在构造数组的时候采用ArrayBuffer,不用考虑数组长度,添加删除元素也很方便,在使用的时候一般转化为Array来查询,至于为什么,也不是很清楚,猜测在底层的数据结构上Array占用的内存空间较少?且通过下角标访问元素效率更高?总之先学会怎么用,后面再深入研究数据结构吧
b.ArrayBuffer与Array之间转化:
通过toArray和toBuffer方法,比较方便
scala> val arr = Array(1,2,3)
arr: Array[Int] = Array(1, 2, 3)
scala> arr.toBuffer
res24: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3)
scala> res24.toArray
res25: Array[Int] = Array(1, 2, 3)
c.构造多维数组:
一般实际使用中一二维数组最常用,二维数组以上的不常见
构造二维数组:
scala> val arr2 = Array(Array(1,'a'),Array(2,'b'),Array(3,'c'))
arr2: Array[Array[Int]] = Array(Array(1, 97), Array(2, 98), Array(3, 99))
scala> val arr2:Array[Array[Any]] = Array(Array(1,'a'),Array(2,'b'),Array(3,'c'))
arr2: Array[Array[Any]] = Array(Array(1, a), Array(2, b), Array(3, c))
scala> print(arr2(1)(1))
b
或者通过数组的方法ofDim构造
如构造一个2行3列的多维字符串数组:
scala> val arr3 = Array.ofDim[String](2,3)
arr3: Array[Array[String]] = Array(Array(null, null, null), Array(null, null, null))
d.数组的遍历
三种方法:map for foreach
scala> arr4
res36: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(koea, hello, scala)
scala> arr4.map(e => {
| println("name is " + e)
| })
name is koea
name is hello
name is scala
res34: scala.collection.mutable.ArrayBuffer[Unit] = ArrayBuffer((), (), ())
scala> for(i <- 0 until arr4.length) println(s"name is ${arr4(i)}")
name is koea
name is hello
name is scala
scala> for(i <- arr4) println(s"name is ${i}")
name is koea
name is hello
name is scala
scala> arr4.foreach(e => println(s"name is ${e}"))
name is koea
name is hello
name is scala
e.yield方法构造新的集合
scala> arr
res39: Array[Int] = Array(1, 2, 3)
scala> val arrNew = for (i <- arr) yield i * 2
arrNew: Array[Int] = Array(2, 4, 6)