Scala:函数内的ArrayBuffer的+ =错误

时间:2018-04-09 02:19:18

标签: scala

在向Scala中的ArrayBuffer追加值时出错。在下面给出代码:

def test()
  : (String,ArrayBuffer[(String,String,String)])
{
  val outstr=mutable.ArrayBuffer[(String,String,String)]()

  val testnmae="TESTING"
  val t=("A1","B1","C1");
  val t2=("A2","B2","C2");
  val t3=("A3","B3","C3");
  outstr += t;
<console>:9: error: illegal start of declaration (possible cause: missing `=' in front of current method body)
   outstr :+= t;
   ^

但是在REPL中执行相同操作时不要出错:

scala> val t=("A1","B1","C1");
t: (String, String, String) = (A1,B1,C1)
outstr += t;
res34: outstr.type = ArrayBuffer((A1,B1,C1))

1 个答案:

答案 0 :(得分:0)

就在那里:

  

(possible cause: missing '=' in front of current method body)

在方法正文之前添加=

另一个注意事项:考虑使用ArrayBuffer.empty[(String, String, String)]而不是在没有参数的情况下调用apply函数,它可能更清晰。