Scala中可变数组和不可变数组的创建和访问
程序员文章站
2022-06-14 18:55:53
...
package com.atguigu.scala.chapter07
import scala.collection.mutable.ArrayBuffer
/**
* @author yymstart
* @create 2020-09-16 16:33
*/
//可变数组
object Test02_ArrayBuffer {
def main(args: Array[String]): Unit = {
//创建可变数组
//可以不传参数,默认16
var arr1:ArrayBuffer[Int] = new ArrayBuffer[Int]()
val arr2 = ArrayBuffer(2,3,4)
//array必须mkstring,这个不用,因为底层又索引
println(arr1) //ArrayBuffer()
println(arr2) //ArrayBuffer(2, 3, 4)
//2.fangwen数组元素进行读写操作
//直接访问角标越界
println(arr2(2))//4
//运行报错,需要使用添加方法
//arr1(0) = 12
//底层同样是update方法
arr2(0) = 12
println(arr2)//ArrayBuffer(12, 3, 4)
println("=====================")
//3.向数组中添加元素,可变集合不推荐
arr1 :+ 200
//ArrayBuffer(),底层b ++= thisCollection
println(arr1)//ArrayBuffer()
//强行使用,不推荐
val newArr1 = arr1 :+ 200
//最好使用的是英文表示
arr1.append(1,2,2,2,2,3)
println(arr1)//ArrayBuffer(1, 2, 2, 2, 2, 3)
//往前加
arr1.prepend(0)
println(arr1)//ArrayBuffer(0, 1, 2, 2, 2, 2, 3)
//往任意位置添加
arr1.insert(1,100000000,200000000)
println(arr1)//ArrayBuffer(0, 100000000, 200000000, 1, 2, 2, 2, 2, 3)
//符号方法的调用
println("符号方法的调用")
println(arr2)//ArrayBuffer(12, 3, 4)
arr2 += 23
println(arr2)//ArrayBuffer(12, 3, 4, 23)
10 +=: arr2
println(arr2)//ArrayBuffer(10, 12, 3, 4, 23)
//4.删除元素,按照索引删除
val a = arr2.remove(2) //按索引位置删除
arr2.remove(2,2)
println(arr2)//ArrayBuffer(10, 12)
println(a)//3
//符号的删除方法,按值删除
arr2 -= 2
println(arr2)//ArrayBuffer(10, 12)
//5.增加另一个数组的所有元素
arr2.appendAll( ArrayBuffer(27,47,80) )
println(arr2)//ArrayBuffer(10, 12, 27, 47, 80)
//谁调用便用谁
arr2++=arr1
println(arr2)//ArrayBuffer(10, 12, 27, 47, 80, 0, 100000000, 200000000, 1, 2, 2, 2, 2, 3)
arr2 ++=: arr1
println(arr1)//ArrayBuffer(10, 12, 27, 47, 80, 0, 100000000, 200000000, 1, 2, 2, 2, 2, 3, 0, 100000000, 200000000, 1, 2, 2, 2, 2, 3)
//6.还可以做一个可变数组和不可变数组的转换
val arr:ArrayBuffer[Int] = ArrayBuffer(2,4,7)
val newArr:Array[Int] = arr.toArray
println(newArr) //[[email protected]
println(newArr.mkString("-"))//2-4-7
println(arr)//ArrayBuffer(2, 4, 7)
val buffer = newArr.toBuffer
println(buffer)//ArrayBuffer(2, 4, 7)
println(newArr) //[[email protected]
println(newArr.mkString(" "))//2 4 7
}
}
不可变数组
package com.atguigu.scala.chapter07
/**
* @author yymstart
* @create 2020-09-16 15:25
*/
object Test01_Array {
def main(args: Array[String]): Unit = {
//1.创建一个不可变数组
//1.new
val arr1:Array[Int] = new Array[Int](5)
//2.用半生对象的apply方法
val arr2 :Array[Int] = Array(1,2,3,4,5)
println("===================")
//2.访问数组中元素,做读写操作.这里的apply是array的方法
println(s"${arr1(0)} ${arr1(1)} ${arr1.apply(2)} ${arr1(3)} ${arr1(4)} ")
//不可变是不能该长度,内容可以改
//底层还是一个方法
arr1(3) = 100000000
arr1.update(4,20000000)
arr1(2) = 270000000
//array继承的是Java的序列化,打印的是地址
println(arr1)
println(arr1.mkString("-"))
//3.遍历所有元素
//1.for循环,遍历索引
for ( i <- 0 until arr2.length )
println(arr2(i) + "\t")
println()
for ( i <- arr2.indices )
println(arr2(i) + "\t")
println()
//2.for循环直接遍历元素
for (elem <- arr2) print(elem +"\t")
println()
//3.迭代器 下面的虚线全是隐式转换
var iter:Iterator[Int] = arr2.iterator
while(iter.hasNext){
print(iter.next() + "\t")
}
println()
//4.foreach方法
arr2.foreach(elem=>print(elem + "\t"))
println()
arr2.foreach(println(_))
//4.向数组中添加元素
//生成一个新的数组,原数组不变
val newArr1:Array[Int] = arr2.:+(20)
println(arr2.mkString(" "))
println(newArr1.mkString(" "))
val newArr2:Array[Int] = arr2.+:(0)
println(newArr2.mkString(" "))
val newArr3 = arr1 :+ 39
//:在后面的话是从右往左调用
//val newArr3 = arr1 +: 15
val newArr4 = 15 +: arr1
}
}
上一篇: Scala方法与函数(二)