将数组中的值提取到元组中 [英] Extract values from Array into Tuple

查看:53
本文介绍了将数组中的值提取到元组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以将列表的值提取到 Scala 中的元组中?

Is there a simple way to extract the values of a list into a tuple in Scala?

基本上类似

"15,8".split(",").map(_.toInt).mkTuple //(15, 8)

或者我可以做的其他方式

Or some other way I can do

val (x, y) = "15,8".split(",").map(_.toInt)

推荐答案

如果你把它们放在一个数组中,你可以像这样在变量名前面写Array:

If you have them in an array you can write Array in front of the variable names like so:

val Array(x, y) = "15,8".split(",").map(_.toInt)

如果您有其他集合类型,只需替换为 Seq 或类似的.

Just replace with Seq or similar if you have another collection-type.

它基本上就像一个提取器在幕后工作.另请参阅此相关主题:scala zip list to tuple

It basically works just like an extractor behind the scenes. Also see this related thread: scala zip list to tuple

这篇关于将数组中的值提取到元组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆