在Scala中,scala.collection.mutable實現了Stack數據結構。 :+ 方法用於創建此堆棧的副本並附加一個元素。
Method Definition - def :+(elem: A)
Returns - a new stack consisting of all elements of this stack followed by elem.
示例#1:
// 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 :+ 100
// Display output
print(result)
}
}
輸出:
List(11, 12, 13, 14, 15, 100)
示例#2:
// 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)
val st2 = Stack("Geeks", "For")
// Applying :+() method
val result1 = st2 :+ st1
// Display output
print(result1)
}
}
輸出:
Stack(Geeks, For, Stack(1, 2, 3))
相關用法
- 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。