数组
------------------------------------------------------------------
一维数组
object Demo2 {def main(args: Array[String]): Unit = {//定义数组,并指定长度val arr1 = new Array[Int](5)arr1(0) = 1arr1(2) = 4printarr(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.ArrayBufferscala> val ab1 = new ArrayBuffer[Int](5) ab1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
append
scala> ab1.appenddef append(elems: Int*): Unitscala> ab1.append(1,2,3)scala> ab1 res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
prepend
scala> ab1.prependdef prepend(elems: Int*): Unitscala> 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.trimEnddef trimEnd(n: Int): Unitscala> ab1.trimEnd(3)scala> ab1 res16: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 5, 1, 2, 3, 6)
insert
scala> ab1.insertdef insert(n: Int,elems: Int*): Unitscala> 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.toArraydef 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 = 27scala> ab1.max res27: Int = 6
mkString
scala> ab1.mkStringdef mkString(sep: String): String def mkString: String def mkString(start: String,sep: String,end: String): Stringscala> ab1.mkString(",") res30: String = 4,0,5,1,2,3
java和scala互操作
--------------------------------------------------------------------------------
1、java的List 转 scala的Buffer
scala> val list:java.util.List[String] = new java.util.ArrayList() list: java.util.List[String] = []scala> list.add("qq") res27: Boolean = truescala> list.add("ww") res28: Boolean = truescala> list res29: java.util.List[String] = [qq, ww]scala> import scala.collection.JavaConversions.asScalaBuffer import scala.collection.JavaConversions.asScalaBufferscala> val buff = list.toBuffer buff: scala.collection.mutable.Buffer[String] = ArrayBuffer(qq, ww)scala> buff res30: scala.collection.mutable.Buffer[String] = ArrayBuffer(qq, ww)
2、scala的Buffer 转 java的List
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBufferscala> val arr = ArrayBuffer("qq","ww") arr: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(qq, ww)scala> import scala.collection.JavaConverters.bufferAsJavaListConverter import scala.collection.JavaConverters.bufferAsJavaListConverterscala> arr.asJava res0: java.util.List[String] = [qq, ww]