将元组附加到 Scala 中的缓冲区 [英] Appending tuple to a buffer in Scala
本文介绍了将元组附加到 Scala 中的缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Scala 中,
test("Appending a tuple to a Buffer"){
val buffer = ArrayBuffer[Int]()
val aTuple = (2, 3)
println(buffer += (2, 3)) // Result : ArrayBuffer(2, 3)
println(buffer += aTuple ) // doesn't compile
}
为什么要行
println(buffer += (2, 3))
工作,但行
println(buffer += aTuple )
不编译?
推荐答案
因为您没有添加 Tuple
,所以您使用两个参数调用 +=
方法:
Because you are not adding a Tuple
, you are calling the +=
method with two parameters:
buffer += (3, 4)
// is equivalent here to
buffer.+=(3, 4)
该方法已定义 带和不带可变参数,并将其添加到缓冲区中:
And that method is defined both with varargs and without, and adds to the buffer everything it is given:
def +=(elem: A): ArrayBuffer.this.type
def +=(elem1: A, elem2: A, elems: A*): ArrayBuffer.this.type
这篇关于将元组附加到 Scala 中的缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文