Fill ArrayBuffer with pairs of Double

时间:2017-06-09 12:54:47

标签: scala arraybuffer

I need some guidance here, please.

What I have:

import scala.collection.mutable.ArrayBuffer
var buffer = ArrayBuffer.empty[(Double, Double)]

and I want to fill the buffer with pairs. I'm trying this but it doesn't work:

for(someCycle){
    buffer += (someDouble, someOtherDouble)
}

the error:

 error: type mismatch;
 found   : Double
 required: (Double, Double)
              buffer += (someDouble, otherDouble)

I understand the error but I can't figure out the right syntax.

1 个答案:

答案 0 :(得分:4)

由于buffer.+=(someDouble, someOtherDouble) 是一个函数,编译器将其推断为:

+=

让它认为您正在尝试将两个参数传递给buffer += ((someDouble, someOtherDouble)) 而不是一个(错误消息有点误导)。

您需要一个额外的括号:

buffer += (someDouble -> someOtherDouble)

或者:

{{1}}