Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在scala中的元组数组中使用map函数?_Arrays_Scala_Collections_Tuples - Fatal编程技术网

Arrays 如何在scala中的元组数组中使用map函数?

Arrays 如何在scala中的元组数组中使用map函数?,arrays,scala,collections,tuples,Arrays,Scala,Collections,Tuples,我尝试在Scala shell中执行以下代码: var chars = ('a' to 'z').toArray.zipWithIndex chars: Array[(Char, Int)] = Array((a,0), (b,1), (c,2), (d,3), (e,4), (f,5), (g,6), (h,7), (i,8), (j,9), (k,10), (l,11), (m,12), (n,13), (o,14), (p,15), (q,16), (r,17), (s,18), (

我尝试在Scala shell中执行以下代码:

 var chars = ('a' to 'z').toArray.zipWithIndex
 chars: Array[(Char, Int)] = Array((a,0), (b,1), (c,2), (d,3), (e,4), (f,5), (g,6),
(h,7), (i,8), (j,9), (k,10), (l,11), (m,12), (n,13), (o,14), (p,15), (q,16), 
(r,17), (s,18), (t,19), (u,20), (v,21), (w,22), (x,23), (y,24), (z,25))
现在我希望上面提到的元组数组中每个字符的索引都更新为1,即索引1处的“a”,索引26处的z。如何使用map函数实现这一点

像这样

('a' to 'z').toArray.zipWithIndex.map(t => (t._1, t._2 + 1))
像这样

('a' to 'z').toArray.zipWithIndex.map(t => (t._1, t._2 + 1))

使用拉链而不是zipWithIndex。下面的示例

scala> var chars = ('a' to 'z').toArray.zip(Stream from 1)
chars: Array[(Char, Int)] = Array((a,1), (b,2), (c,3), (d,4), (e,5), (f,6), (g,7), (h,8), (i,9), (j,10), (k,11), (l,12), (m,13), (n,14), (o,15), (p,16), (q,17), (r,18), (s,19), (t,20), (u,21), (v,22), (w,23), (x,24), (y,25), (z,26))

scala>
scala>  var chars = ('a' to 'z').toArray.zip(Stream from 100)
chars: Array[(Char, Int)] = Array((a,100), (b,101), (c,102), (d,103), (e,104), (f,105), (g,106), (h,107), (i,108), (j,109), (k,110), (l,111), (m,112), (n,113), (o,114), (p,115), (q,116), (r,117), (s,118), (t,119), (u,120), (v,121), (w,122), (x,123), (y,124), (z,125))

scala>

使用拉链而不是zipWithIndex。下面的示例

scala> var chars = ('a' to 'z').toArray.zip(Stream from 1)
chars: Array[(Char, Int)] = Array((a,1), (b,2), (c,3), (d,4), (e,5), (f,6), (g,7), (h,8), (i,9), (j,10), (k,11), (l,12), (m,13), (n,14), (o,15), (p,16), (q,17), (r,18), (s,19), (t,20), (u,21), (v,22), (w,23), (x,24), (y,25), (z,26))

scala>
scala>  var chars = ('a' to 'z').toArray.zip(Stream from 100)
chars: Array[(Char, Int)] = Array((a,100), (b,101), (c,102), (d,103), (e,104), (f,105), (g,106), (h,107), (i,108), (j,109), (k,110), (l,111), (m,112), (n,113), (o,114), (p,115), (q,116), (r,117), (s,118), (t,119), (u,120), (v,121), (w,122), (x,123), (y,124), (z,125))

scala>

您也可以这样做:

('a' to 'z') zip (Stream from 1)

这将产生一个
向量
。如果需要阵列,只需将
应用到阵列即可。

您也可以这样做:

('a' to 'z') zip (Stream from 1)
这将产生一个
向量
。如果需要数组,只需将
应用到数组中即可。

使用

使用


... 或者分解元组,以避免诉诸那些难看的
\u1
\u2
方法:
.map{case(c,i)=>(c,i+1)}
@jubobs完全同意。我讨厌那些方法。。。。或者分解元组,以避免诉诸那些难看的
\u1
\u2
方法:
.map{case(c,i)=>(c,i+1)}
@jubobs完全同意。我讨厌这些方法。你想解决这个特定的问题(将字母映射到基于1的索引),还是这只是你需要解决的一个例子?你的答案是双向的。你想解决这个特定的问题(将字母映射到基于1的索引),还是这只是你需要解决的问题的一个例子?你的答案是双向的。谢谢你,两个答案都一样,第二个更容易理解。请你详细说明一下第一个问题好吗。我正在学习,所以没有得到第一个答案。再次感谢。请检查一下解释,现在可能已经很清楚了。谢谢,你的两个答案都是一样的,第二个更容易理解。请你详细说明一下第一个问题好吗。我正在学习,所以没有得到第一个答案。再次感谢您,请查看解释,现在您可能已经明白了。