Scala基本语法:数组和集合------List与ListBuffer------(4)

数组Array

scala中数组分为:定长数组Array 变长数组ArrayBuffer

定长和变长中的长是指数组的长度是否可以改变,跟元素值得内容没有关系

变长数组得使用需要导包

**

import scala.collection.mutable.ArrayBuffer

**

定义格式

如果有初始值,不用new,可以直接赋值

//定义一个有两个元素初始值得定长数组,
scala> val a1=Array(10,20)
a1: Array[Int] = Array(10, 20)

//定义一个变长数组,其首先需要导包
scala> import scala.collection.mutable.ArrayBu
ArrayBuffer   ArrayBuilder

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val a2=ArrayBuffer(50,40)
a2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(50, 40)

如果没有初始值,则需要new

//定义一个定长数组,没有初始值,则需要new关键字眼
scala> val a1=new Array[Int](5)
a1: Array[Int] = Array(0, 0, 0, 0, 0)
//定义一个变长数组,没有初始值,则不需要new,可以直接用
scala> val a5 = ArrayBuffer[Int](5)
a5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5)

数组操作:

定长数组:

scala> val a1=Array(55,66)     //定义一个有初始值得数组
a1: Array[Int] = Array(55, 66)

scala> a1 += 77               //在定长数组a1中不能增加新元素
<console>:14: error: value += is not a member of Array[Int]
       a1 += 77
          ^

scala> a1(0)        //可以通过下标取值
res24: Int = 55

scala> a1(1)        //可以通过下标取值
res25: Int = 66

scala> a1(0)=77     //虽然定长数组不能增加元素,但是可以修改已有元素得值

scala> a1            //虽然定长数组不能增加元素,但是可以修改已有元素得值
res27: Array[Int] = Array(77, 66)

变长数组ArrayBuffer

scala> val a2=ArrayBuffer(11,22,33,44,55)     //定义一个变长数组
a2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(11, 22, 33, 44, 55)

scala> a2 -= 11                                 //删除变长数组中得一个元素
res28: a2.type = ArrayBuffer(22, 33, 44, 55)

scala> a2 += 99                            //给变长数组增加一个元素
res29: a2.type = ArrayBuffer(22, 33, 44, 55, 99)

scala> a2 =-33                       //注意  在增加删除时,要注意符号顺序
<console>:14: error: value =- is not a member of scala.collection.mutable.ArrayBuffer[Int]
       a2 =-33
          ^

scala> a2.remove(2)                       //调用方法删除元素
res31: Int = 44

scala> a2                              //查看全部元素
res32: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(22, 33, 55, 99)

数组遍历:

scala> a2
res32: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(22, 33, 55, 99)

scala> for(i <- a2)println(i)   //第一种遍历方式
22
33
55
99

scala> for(i <- 0 until a2.length)println(a2(i))     //第二种遍历方式  其中until是含头不含尾得方法
22
33
55
99

数组常见得方法:

scala> a2 += 11    //增加元素
res35: a2.type = ArrayBuffer(22, 33, 55, 99, 11)

scala> a2 += 66    //增加元素
res36: a2.type = ArrayBuffer(22, 33, 55, 99, 11, 66)

scala> a2.sum   //求总和
res37: Int = 286

scala> a2.max     //求最大值
res38: Int = 99

scala> a2.min      //求最小值
res39: Int = 11

scala> a2.sorted      //排序,默认为升序
res40: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(11, 22, 33, 55, 66, 99)

scala> a2.sorted.reverse      //后面加上reverse就变成降序
res41: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(99, 66, 55, 33, 22, 11)

元组 tuple

**概念:**用小括号把一些不同类型得数据聚集在一起,称为 元组

元组得元素时不可变得

定义使用:

scala> val a1=(1,2,"hello",true)     //元组得定义格式
a1: (Int, Int, String, Boolean) = (1,2,hello,true)

scala> val a2=("name","kuanshao")   //如果只有两个元素得元组,可以称为  对偶元组
a2: (String, String) = (name,kuanshao)

操作

scala> a1    
res0: (Int, Int, String, Boolean) = (1,2,hello,true)

scala> a1._1    //提取元组的内容,注意这里的int不是下标,不是下标
res1: Int = 1

scala> a1._3
res2: String = hello

scala> a1.   //元组的遍历
_1   _3   canEqual   equals     productArity     productIterator   toString   
_2   _4   copy       hashCode   productElement   productPrefix                

scala> a1.product
productArity   productElement   productIterator   productPrefix

scala> a1.productIterator
res5: Iterator[Any] = non-empty iterator      //先调用的到一个迭代器

scala> a1.productIterator.for       
forall   foreach   formatted

scala> a1.productIterator.foreach(println)     //元组遍历的一般格式
1
2
hello
true

列表:List

​ 在Scala种,列表list分为两大类: list不可变列表 listBuffer可变列表

listbuffer可变列表使用的时候需要自己导包

​ **定义格式:**有初始值 不要new 没有初始值 需要new

​ **所谓的不可变:**列表的元素值和列表的长度时不可变的

scala> val a1=List(66,77,88)    //定义不可变列表,有初始值,不需要new
a1: List[Int] = List(66, 77, 88)

scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer

scala> val a2=ListBuffer(77,88,99)    //定义可变列表,前提需要导包   有初始值,不需要new
a2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(77, 88, 99)

List种几个特殊的方法:

head tail Nil

scala> a1
res7: List[Int] = List(66, 77, 88)

scala> a1.head    //head方法  返回集合种第一个元素
res8: Int = 66

scala> a1.tail      //返回集合中除第一个元素之外的其他元素,构成一个新集合
res9: List[Int] = List(77, 88)


scala> val a2=Nil      //Nil表示一个空元素集合
a2: scala.collection.immutable.Nil.type = List()

操作:

不可变的List

//集合元素的添加操作  注意原来的集合没有改变 操作返回构成一个新的集合
//  ::  在集合的头部追加一个新元素 返回一个新集合
//  +=    在集合的头部追加一个新元素,返回一个新集合

scala> val a1=55::44::33::22::11::Nil     //先定义这个集合为空,然后再一次想头部添加新元素      
a1: List[Int] = List(55, 44, 33, 22, 11)

scala> a1.sorted
res20: List[Int] = List(11, 22, 33, 44, 55)

scala> a1
res21: List[Int] = List(55, 44, 33, 22, 11)

scala> a1.::(99)    //  在头部添加元素
res22: List[Int] = List(99, 55, 44, 33, 22, 11)

scala> a1 :+ 88      //在尾部添加元素
res23: List[Int] = List(55, 44, 33, 22, 11, 88)

scala> a1             //添加只是创建一个新元素,原来的集合不会变
res24: List[Int] = List(55, 44, 33, 22, 11)

scala> val a2=a1 +: 100    //要注意书写格式  这+=事头部添加,所以添加元素要写在集合名和 +:符的前面
<console>:13: error: value +: is not a member of Int
       val a2=a1 +: 100
                 ^

scala> val a2=(a1 :+  100)    //在尾部添加元素
a2: List[Int] = List(55, 44, 33, 22, 11, 100)

scala> a2    
res25: List[Int] = List(55, 44, 33, 22, 11, 100)

scala> val a3= 500 +: a1    //在头部添加
a3: List[Int] = List(500, 55, 44, 33, 22, 11)

scala> a1       //添加只是创建一个新元素,原来的集合不会变
res26: List[Int] = List(55, 44, 33, 22, 11)

scala> a2
res27: List[Int] = List(55, 44, 33, 22, 11, 100)

scala> a3
res28: List[Int] = List(500, 55, 44, 33, 22, 11)

可变ListBuffer:

scala> import scala.collection.mutable.ListBuffer   //先导包  因为事可变ListBuffer 所以先要导包
import scala.collection.mutable.ListBuffer

scala> val a1=ListBuffer(99,88)     //初始化列表
a1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(99, 88)

scala> a1 += 77   //添加元素
res29: a1.type = ListBuffer(99, 88, 77)

scala> a1 -= 99    //删除元素
res30: a1.type = ListBuffer(88, 77)

scala> a1.append(66)   //添加元素

scala> a1
res32: scala.collection.mutable.ListBuffer[Int] = ListBuffer(88, 77, 66)

scala> a1.remove(2)    //删除元素
res33: Int = 66

scala> a1       //查看所有元素
res34: scala.collection.mutable.ListBuffer[Int] = ListBuffer(88, 77)

scala> a1(1)     //根据下标查看元素内容
res35: Int = 77

scala> a1
res36: scala.collection.mutable.ListBuffer[Int] = ListBuffer(88, 77)

scala> a1(1)=666    //根据下标修改元素内容

scala> a1
res38: scala.collection.mutable.ListBuffer[Int] = ListBuffer(88, 666)

scala> for(i <- a1)println(i)    //遍历列表
88
666

scala> for(i <- 0 until a1.length)println(a1(i))    //遍历列表
88
666

扁平化和几个常用方法:


scala> val a=List(List(1,2),List(3),List(4,5))   
a: List[List[Int]] = List(List(1, 2), List(3), List(4, 5))

scala> a.flatten  扁平化一般形式 关键字 flatten   但原来的集合还是不会变的
res0: List[Int] = List(1, 2, 3, 4, 5)

scala> a
res1: List[List[Int]] = List(List(1, 2), List(3), List(4, 5))

scala> val aa=a.flatten    //扁平化后创建一个新的集合
aa: List[Int] = List(1, 2, 3, 4, 5)

scala> aa                      
res2: List[Int] = List(1, 2, 3, 4, 5)

scala> aa.take(2)        //只看前两个元素的内容
res3: List[Int] = List(1, 2)

scala> aa.drop(2)      //只看除前两个以外的内容
res4: List[Int] = List(3, 4, 5)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值