3数组和集合
Posted lybpy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3数组和集合相关的知识,希望对你有一定的参考价值。
数组
------------------------------------------------------------------
一维数组
object Demo2 { def main(args: Array[String]): Unit = { //定义数组,并指定长度 val arr1 = new Array[Int](5) arr1(0) = 1 arr1(2) = 4 printarr(arr1) //初始化并赋值 val arr2 = Array[String]("lyb","qqq") val arr22 = Array.apply[String]("lyb","qqq") val arr3 = Array("qq","ee") val arr33 = Array.apply("qq","ee") } def printarr(array: Array[Int]): Unit ={ for(i <- 0 until array.length)print(array(i) + " ") } }
多维数组
scala> val arr = Array.ofDim[String](2,3) arr: Array[Array[String]] = Array(Array(null, null, null), Array(null, null, null))
scala> arr(0)(1)="qq" scala> arr res34: Array[Array[String]] = Array(Array(null, qq, null), Array(null, null, null))
数组缓冲区
----------------------------------------------------------------
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> val ab1 = new ArrayBuffer[Int](5) ab1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
append
scala> ab1.append def append(elems: Int*): Unit scala> ab1.append(1,2,3) scala> ab1 res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
prepend
scala> ab1.prepend def prepend(elems: Int*): Unit scala> ab1.prepend(4,5) scala> ab1 res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 5, 1, 2, 3)
length
scala> ab1.length res5: Int = 5
+= 修改自身
scala> ab1 += 6 res6: ab1.type = ArrayBuffer(4, 5, 1, 2, 3, 6)
.++= 修改自身
scala> ab1.++= (7 to 8) res7: ab1.type = ArrayBuffer(4, 5, 1, 2, 3, 6, 7, 8)
-= 删除前面的值
scala> ab1 res13: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 5, 1, 2, 3, 6, 7, 8, 7, 8) scala> ab1.-=8 <console>:1: error: ‘;‘ expected but integer literal found. ab1.-=8 ^ scala> ab1.-=(8) res14: ab1.type = ArrayBuffer(4, 5, 1, 2, 3, 6, 7, 7, 8)
trimEnd
scala> ab1.trimEnd def trimEnd(n: Int): Unit scala> ab1.trimEnd(3) scala> ab1 res16: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 5, 1, 2, 3, 6)
insert
scala> ab1.insert def insert(n: Int,elems: Int*): Unit scala> ab1.insert(1,0 to 3:_*) scala> ab1 res18: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 0, 1, 2, 3, 5, 1, 2, 3, 6)
toArray
scala> ab1.toArray def toArray[B >: Int](implicit evidence$1: scala.reflect.ClassTag[B]): Array[B] scala> ab1.toArray res19: Array[Int] = Array(4, 0, 1, 2, 3, 5, 1, 2, 3, 6)
reverse
scala> ab1 res23: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 0, 1, 2, 3, 5, 1, 2, 3, 6) scala> ab1.reverse res24: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(6, 3, 2, 1, 5, 3, 2, 1, 0, 4) scala> ab1 res25: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 0, 1, 2, 3, 5, 1, 2, 3, 6)
sum、max...
scala> ab1.sum res26: Int = 27 scala> ab1.max res27: Int = 6
mkString
scala> ab1.mkString def mkString(sep: String): String def mkString: String def mkString(start: String,sep: String,end: String): String scala> ab1.mkString(",") res30: String = 4,0,5,1,2,3
以上是关于3数组和集合的主要内容,如果未能解决你的问题,请参考以下文章