Scala 数组和List
Posted AK47Sonic
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala 数组和List相关的知识,希望对你有一定的参考价值。
Scala 数组和List:
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.Buffer object ArrayLearning { def main (args: Array[String]): Unit = { // useZipUnzip // listOps // arrayOps // ArrayBufferOps } private def useZipUnzip = { //zip and unzip val nums = List(1, 2, 3, 4) val chars = List(‘a‘, ‘b‘, ‘c‘, ‘d‘) val combinList = nums.zip(chars) println(combinList) println(combinList.unzip) } private def ArrayBufferOps = { //创建可变数组 val strArrayVar = ArrayBuffer[String]() //在尾部添加元素 strArrayVar += "aaa" println(strArrayVar) strArrayVar += ("bbb","ccc") println(strArrayVar) //追加任意集合 strArrayVar ++= List("ddd", "eee") println(strArrayVar) //删除尾部3个元素 strArrayVar.trimEnd(3) println(strArrayVar) //可变和不可变数组转换 val arr: Array[String] = strArrayVar.toArray val arrBuffer: Buffer[String] = arr.toBuffer } private def arrayOps = { //创建定长数组 val numberArray = new Array[Int](10) val stringArray = new Array[String](10) //数组赋值 stringArray(0) = "hello" println(stringArray(0)) //用Array apply方法创建数组 val strArray = Array("aaa", "bbb") } private def listOps = { val xss = List(List(1,2,3),List("a","b","c")) val fList: List[Any] = xss.flatten println(fList) println(List.concat(List(1,2,3),List(4,5,6))) println(List(1,2,3):::List(4,5,6):::Nil) } }
以上是关于Scala 数组和List的主要内容,如果未能解决你的问题,请参考以下文章
linux打开终端如何启动scala,如何在终端下运行Scala代码片段?