Scala 错误:'=' 预期但 ';' 成立

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27952290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:49:53  来源:igfitidea点击:

Scala error: '=' expected but ';' found

scalaapache-spark

提问by Jiang Xiang

Not sure what caused this problem: error: '=' expected but ';' found.

不确定是什么导致了这个问题:错误:'='预期但';' 成立。

val vectors = filtered_data_by_key.map( x => {
    var temp
    x._2.copyToArray(temp)  // Error occurs here
    (x._1, temp)
})

回答by George Simms

var tempisn't a statement.

var temp不是声明。

If you're trying to declare temp without assigning anything to it, do

如果您尝试声明 temp 而不为其分配任何内容,请执行

var temp :Array[_] = _

But is temp supposed to be an array? then try var temp = Array(). tempneeds something assigned to it before being passed into copyToArray. Also as you're not destructively assigning to temp it doesn't need to be a var.

但是 temp 应该是一个数组吗?然后尝试var temp = Array()temp在传入之前需要分配给它的东西copyToArray。此外,由于您没有破坏性地分配给 temp,因此它不需要是 var。

回答by pzecevic

If filtered_data_by_key is an RDD of (T, Iterable), or in other words, a result of groupByKey transformation, then this can be written simply like this:

如果filtered_data_by_key是(T, Iterable)的RDD,或者说是groupByKey转换的结果,那么可以简单写成这样:

val vectors = filtered_data_by_key.map( { case (x, iter) => (x, iter.toArray) })