Scala | 集合类型(Collection)

本文介绍了Scala中的集合类型Collection,包括Array、List、Set、Range、Iterator、Map和Tuple。其中,Array分为不可变的定长和可变的变长两种,下标从0开始,使用圆括号操作符获取元素。
摘要由CSDN通过智能技术生成

集合类型 - Collection

scala的集合类型(Collection)包含如下常见的类型:
Array,List,Set,Range,Iterator,Map,Tuple

知识点:

  1. scala的Array分为定长数组和变长数组
  2. 定长:immutable
  3. 变长:mutable
  4. 通过下标操作Array,下标从0开始。通过(index),不同于java的[index]
object Demo03 {
   
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  
  //创建一个定长数组并赋值
  val a1=Array(1,2,3,4)                           //> a1  : Array[Int] = Array(1, 2, 3, 4)
  //创建一个定长数组,指定泛型以及长度
  val a2=new Array[Int](3)                        //> a2  : Array[Int] = Array(0, 0, 0)
  //创建变长数组并赋值。可以追加新元素的
  val a3=scala.collection.mutable.ArrayBuffer(1,2,3,4)
                                                  //> a3  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
  //通过下标取值
  a1.apply(0)                                     //> res0: Int = 1
  a1(0)                                           //> res1: Int = 1
  //通过下标赋值
  a1(0)=10
  a1                                              //> res2: Array[Int] = Array(10, 2, 3, 4)
  //对变长Array追加数据
  a3.append(5,6)
  a3                                              //> res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5,
                                 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值