Using Scala 2.7.7, this works as ex开发者_高级运维pected:
import scala.collection.mutable.Stack
...
var x = new Stack[String]
x += "Hello"
println(x.top)
After changing to Scala 2.8.0, the += should be replaced by :+. However, this does not append to the stack: java.util.NoSuchElementException: head of empty list.
Am I overlooking something basic?
:+, defined in SeqLike, copies the stack and append the element into the new stack, and return that. So x is not modified.
Probably you want .push() instead (example).
var x = new Stack[String]
x.push("Hello")
println(x.top)
加载中,请稍侯......
精彩评论