scala - 如何在scala中循环遍历元组

标签 scala loops

我在 scala 中有一个元组

val captainStuff = ("One", "Two", "Three", "Four", "Five")

我如何遍历 for 循环?遍历列表和 map 很容易。但是如何循环元组。

谢谢!!

最佳答案

您可以将其转换为迭代器,例如:

val captainStuff = ("One", "Two", "Three", "Four", "Five")

captainStuff.productIterator.foreach(x => {
  println(x)
})

顺便说一句,这个问题是重复的:

Scala: How to convert tuple elements to lists

关于scala - 如何在scala中循环遍历元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44701879/

相关文章:

C 条件后增量

R Shiny : How to have sequential Modals in for loop

javascript - 如何使用闭包在循环中使用 then()

java - 在 Scala 中使用 Java 库?

scala - 有没有办法使用 Scala 的 XML 库执行 XPath 字符串查询?

java - 斯卡拉/ jack 森 : how to deserialize into generic type

java - 循环来自另一个类的变量

scala - 如何从一个 Observable 生成另一个 Observable

json - Spark 如何在 Scala 的两个 JSONS 中更改键的数量?

javascript - 如何在此循环中使用 jQuery 添加类