在Scala中,scala.collection.mutable實現了Stack數據結構。 :\ 方法將二元運算符應用於起始值以及此可遍曆對象或迭代器的所有元素,從右到左。
Method Definition - def :\[B](z: B)(op: (A, B) ? B): B
Returns - the result of inserting op between consecutive elements of this traversable or iterator.
示例#1:
// Scala program of mutable stack :\()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
val st1 = Stack(1, 2, 3)
// Applying :\() method
val result1 = (st1 :\ 16 )(_+_)
val result2 = (st1 :\ 16)(_-_)
// Display output
print(result1)
print("\n")
print(result2)
}
}
輸出:
22 -14
示例#2:
// Scala program of mutable stack :\() method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a stack
val q2 = List(11, 12, 13, 14, 15)
// Applying :\() method
val result = (q2 :\ 6)(_+_)
// Display output
print(result)
}
}
輸出:
71
相關用法
- Scala Stack :+()用法及代碼示例
- Scala Stack apply()用法及代碼示例
- Scala Stack clear()用法及代碼示例
- Scala Stack clone()用法及代碼示例
- Scala Stack contains()用法及代碼示例
- Scala Stack copyToArray()用法及代碼示例
- Scala Stack count()用法及代碼示例
- Scala Stack diff()用法及代碼示例
- Scala Stack distinct()用法及代碼示例
- Scala Stack drop()用法及代碼示例
- Scala Stack dropRight()用法及代碼示例
- Scala Stack dropWhile()用法及代碼示例
- Scala Stack equals()用法及代碼示例
- Scala Stack exists()用法及代碼示例
- Scala Stack filter()用法及代碼示例
- Scala Stack find()用法及代碼示例
- Scala Stack forall()用法及代碼示例
- Scala Stack foreach()用法及代碼示例
- Scala Stack head()用法及代碼示例
- Scala Stack init()用法及代碼示例
- Scala Stack intersect()用法及代碼示例
- Scala Stack last()用法及代碼示例
- Scala Stack map()用法及代碼示例
- Scala Stack max()用法及代碼示例
- Scala Stack min()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Scala Stack :() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。