+= 在 Scala 2.7.7 中追加到堆栈; :+似乎在 Scala 2.8.0 中不起作用

发布于 2024-09-14 11:29:26 字数 265 浏览 3 评论 0原文

使用 Scala 2.7.7,这可以按预期工作:

import scala.collection.mutable.Stack
...
var x = new Stack[String]
x += "Hello"
println(x.top)

更改为 Scala 2.8.0 后,+= 应替换为 :+。但是,这不会附加到堆栈:java.util.NoSuchElementException:空列表的头。

我是否忽略了一些基本的东西?

Using Scala 2.7.7, this works as expected:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2024-09-21 11:29:26

:+,在 SeqLike 中定义,复制堆栈并将元素追加到新堆栈中,然后返回。所以 x 没有被修改。

也许您需要 .push()示例)。

var x = new Stack[String]
x.push("Hello")
println(x.top)

:+, 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文