如何在Scala中弹出并返回堆栈的顶部元素

嗨,我正在尝试从我正在编写的某些Scala代码中弹出堆栈中的顶部元素,但是内置Stack类的songs = playlist[1:] # Extracts everything in the playlist except the genre. random.shuffle(songs) playlist[1:] = songs # Updates song portion of list with shuffled version. 函数会返回整个Stack减去顶部元素,而不是我在追求什么。

似乎还建议在Scala中实现堆栈的推荐方法是使用pop,然后将Listtail用作::和{{1} }。 pop还会返回一个列表,减去“ top”元素。

如何才能将元素返回以便可以将其存储在变量中?

编辑

我有这段代码来演示正在发生的事情:

push
yyb4051 回答:如何在Scala中弹出并返回堆栈的顶部元素

import scala.collection.mutable.Stack

val stack = Stack[String]("a","b","c")

val popped = stack.pop

println(popped)// a

这就是您想要的,我想您正在将尾巴与流行音乐混淆。

本文链接:https://www.f2er.com/3134471.html

大家都在问