zoukankan      html  css  js  c++  java
  • scala的多种集合的使用(8)之队列和栈的操作方法

    1.使用队列

    队列是一种那个先进先出的队列。
    1)创建一个队列。

    scala> import scala.collection.mutable.Queue
    import scala.collection.mutable.Queue
    
    scala> var fruits = Queue[String]()
    fruits: scala.collection.mutable.Queue[String] = Queue()
    
    scala> fruits += "apple"
    res66: scala.collection.mutable.Queue[String] = Queue(apple)
    
    scala> fruits += ("orange","banana")
    res67: scala.collection.mutable.Queue[String] = Queue(apple, orange, banana)
    
    scala> fruits ++= List("cherry","cocount")
    res68: scala.collection.mutable.Queue[String] = Queue(apple, orange, banana, cherry, cocount)
    
    scala> fruits.enqueue("pine")
    
    scala> fruits
    res70: scala.collection.mutable.Queue[String] = Queue(apple, orange, banana, cherry, cocount, pine)

    2)dequeue每次从队列头部删除一个元素。

    scala> val next = fruits.dequeue
    next: String = apple
    
    scala> fruits
    res72: scala.collection.mutable.Queue[String] = Queue(orange, banana, cherry, cocount, pine)

    3)dequeueFirst和dequeueAll方法可以从队列中删除多个元素。

    scala> var fruits = Queue[String]()
    fruits: scala.collection.mutable.Queue[String] = Queue()
    
    scala> fruits ++= List("cherry","cocount")
    res76: scala.collection.mutable.Queue[String] = Queue(cherry, cocount)
    
    scala> fruits ++= List("orange","apple")
    res77: scala.collection.mutable.Queue[String] = Queue(cherry, cocount, orange, apple)
    
    scala> fruits.dequeueFirst(_.startsWith("a"))
    res79: Option[String] = Some(apple)
    
    scala> fruits
    res80: scala.collection.mutable.Queue[String] = Queue(cherry, cocount, orange)
    
    scala> fruits.dequeueAll(_.length > 6)
    res81: scala.collection.mutable.Seq[String] = ArrayBuffer(cocount)
    
    scala> fruits
    res82: scala.collection.mutable.Queue[String] = Queue(cherry, orange)

    2.使用栈

    栈是一种后进先出的数据结构。用push方法将元素入栈,用pop方法将元素出栈。
    1)创建一个任意数据类型空的可变栈。

    scala> import scala.collection.mutable.Stack
    import scala.collection.mutable.Stack
    
    scala> var ints = Stack[Int]()
    ints: scala.collection.mutable.Stack[Int] = Stack()

    2)在创建时给栈初始元素。

    scala> val ints = Stack(1,2,3)
    ints: scala.collection.mutable.Stack[Int] = Stack(1, 2, 3)

    3)用push方法向栈中填充元素。

    scala> var fruits = Stack[String]()
    fruits: scala.collection.mutable.Stack[String] = Stack()
    
    scala> fruits.push("apple")
    res2: scala.collection.mutable.Stack[String] = Stack(apple)
    
    scala> fruits.push("apple","orange","banana")
    res3: scala.collection.mutable.Stack[String] = Stack(banana, orange, apple, appl

    4)用pop方法将元素出栈。

    scala> val next = fruits.pop
    next: String = banana
    
    scala> fruits
    res4: scala.collection.mutable.Stack[String] = Stack(orange, apple, apple)

    5)用top方法查看下一个元素。

    scala> fruits.top
    res5: String = orange
    
    scala> fruits
    res6: scala.collection.mutable.Stack[String] = Stack(orange, apple, apple)

    6)用clear清空一个可变栈。

    scala> fruits.clear
    
    scala> fruits
    res8: scala.collection.mutable.Stack[String] = Stack()
    

      

  • 相关阅读:
    Java接收Cordys中webservice接口的返回数据并解析xml获取相应节点数据
    初识jenkins之-jenkins的安装与配置
    Linux(centos6.8)下Tomcat的安装与配置
    [置顶] Linux下(centos6.8)JDK1.8的安装与配置
    小白五一期间个人建站
    声明一些事
    js中apply()的用法淺談
    angularJS中的服务
    PHP中的常用关键字
    PHP中三大特性---继承性
  • 原文地址:https://www.cnblogs.com/zhaohadoopone/p/9534996.html
Copyright © 2011-2022 走看看