zoukankan      html  css  js  c++  java
  • scala的多种集合的使用(4)之列表List(ListBuffer)的操作

    1.List列表的创建和添加元素

    1)最常见的创建list的方式之一。

    scala> val list = 1 :: 2 :: 3 :: Nil
    list: List[Int] = List(1, 2, 3)
    

    2)最常见的创建list的方式之一。 

    scala> val list = List(1,2,3)
    list: List[Int] = List(1, 2, 3)
    

    3)集合混合类型组成。

    scala> val list = List(1,2.0,33D,4000L)
    list: List[Double] = List(1.0, 2.0, 33.0, 4000.0)
    

    4)集合混合类型组成,可以有自己控制。下面的例子的集合保持了原有集合的类型。

    scala> val list = List[Number](1,2.0,33D,4000L)
    list: List[Number] = List(1, 2.0, 33.0, 4000)
    

    5)range创建和填充集合。

    scala> val list = List.range(1,10)
    list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    

    6)fill创建和填充集合。

    scala> val list = List.fill(3)("hello")
    list: List[String] = List(hello, hello, hello)
    

    7)tabulate创建和填充集合。

    scala> val list = List.tabulate(5)(i => i * i)
    list: List[Int] = List(0, 1, 4, 9, 16)
    

    8)将集合转化为List的形式。

    scala> val list = collection.mutable.ListBuffer(1,2,3).toList
    list: List[Int] = List(1, 2, 3)
    

    9)将集合转化为List的形式。

    scala> "hello".toList
    res41: List[Char] = List(h, e, l, l, o)
    

    10)创建可变的list,方法是使用ListBuffer,再将ListBuffer转化为List。

    scala> import scala.collection.mutable.ListBuffer
    import scala.collection.mutable.ListBuffer
    
    scala> var fruits = new ListBuffer[String]()
    fruits: scala.collection.mutable.ListBuffer[String] = ListBuffer()
    
    scala> fruits += "apple"
    res42: scala.collection.mutable.ListBuffer[String] = ListBuffer(apple)
    
    scala> fruits += "orange"
    res43: scala.collection.mutable.ListBuffer[String] = ListBuffer(apple, orange)
    
    scala> fruits += ("banana","grape","pear")
    res44: scala.collection.mutable.ListBuffer[String] = ListBuffer(apple, orange, banana, grape, pear)
    	
    scala> val fruitsList = fruits.toList
    fruitsList: List[String] = List(apple, orange, banana, grape, pear)
    

    11) 使用::方法在列表前添加元素。

    scala> var list = List(2)
    list: List[Int] = List(2)
    
    scala> list = 1 :: list
    list: List[Int] = List(1, 2)
    
    scala> list = 9 :: list
    list: List[Int] = List(9, 1, 2)

    2.从List(ListBuffer)中删除元素

    1)List是不可变的,不能从中删除元素,但是可以过滤掉不想要的元素,然后将结果赋给一个新的变量。

    scala> val list = List(4,5,2,1,3)
    list: List[Int] = List(4, 5, 2, 1, 3)
    
    scala> val newList = list.filter(_ > 2)
    newList: List[Int] = List(4, 5, 3)
    

    2)像这样反复的操作结果赋给变量的方式是可以避免的,通过声明变量var,然后将每次操作的结果返回给该变量。

    scala> var list = List(5,2,3,4,1)
    list: List[Int] = List(5, 2, 3, 4, 1)
    
    scala> list = list.filter(_ > 2)
    list: List[Int] = List(5, 3, 4)
    

    3)如果列表经常变动,使用ListBuffer是一个不错的选择。ListBuffer是可变的,因此可以使用可变序列的所有方法从中删除元素。

    import scala.collection.mutable.ListBuffer
    
    scala> val x = ListBuffer(1,2,3,4,5,6,7,8,9)
    x: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)
    
    //可以按值每次删除一个元素:
    scala> x -= 5
    res45: x.type = ListBuffer(1, 2, 3, 4, 6, 7, 8, 9)
    
    //可以一次删除两个或者更多的元素:
    scala> x -= (2,3,4)
    res46: x.type = ListBuffer(1, 6, 7, 8, 9)
    
    //可以按位置删除元素:
    scala> x.remove(0)
    res47: Int = 1
    scala> x
    res48: scala.collection.mutable.ListBuffer[Int] = ListBuffer(6, 7, 8, 9)
    
    //remove可以从定始位置删除指定数量的元素:
    scala> x.remove(1,3)
    scala> x
    res50: scala.collection.mutable.ListBuffer[Int] = ListBuffer(6)
    
    //可以用--=的方法从指定的集合中删除元素。	
    scala> val x = ListBuffer(1,2,3,4,5,6,7,8,9)
    x: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8,9)
    scala> x --= Seq(1,2,3)
    res51: x.type = ListBuffer(4, 5, 6, 7, 8, 9)

    3.列表的合并或者连接

    1)使用++方法合并两个列表

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

    2)使用:::合并两个列表

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

    3)使用concat合并两个列表

    scala> val a = List(1,2,3)
    a: List[Int] = List(1, 2, 3)
    
    scala> val b = List(4,5,6)
    b: List[Int] = List(4, 5, 6)
    	
    scala> val c = List.concat(a,b)
    c: List[Int] = List(1, 2, 3, 4, 5, 6)
    所谓的人生开挂,不过是厚积薄发! 欢迎评论和转载!
  • 相关阅读:
    10. 分离链接散列表
    9. avltree
    8.二叉查找树
    socket listen参数中的backlog 的意义!
    TCP 三次握手和四次挥手中的ACK 为什么总是SYN + 1 或者 FIN +1 而不是+ 其他数值?
    eclipse 快捷键
    内部类
    rabbitmq AmqpClient 使用Topic 交换机同一个channel 同时多个队列 ,多个交换机,C++代码示例
    rabbitMQ 问题
    rabbitmq AmqpClient 使用Topic 交换机投递与接收消息,C++代码示例
  • 原文地址:https://www.cnblogs.com/zhaohadoopone/p/9529795.html
Copyright © 2011-2022 走看看