码迷,mamicode.com
首页 > 编程语言 > 详细

Scala 数组和List

时间:2017-12-03 19:05:20      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:private   str   apply   tab   mutable   nbsp   ops   scala   table   

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

标签:private   str   apply   tab   mutable   nbsp   ops   scala   table   

原文地址:http://www.cnblogs.com/AK47Sonic/p/7966608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!