Reputation: 191159
I'm trying to build an array of an array to give it as a argument to a method. The value of inner arrays are any kind of data (AnyVal) such as Int or Double.
The method's signature is as follows:
def plot[T <: AnyVal](config:Map[String, String], data:Array[Array[T]]): Unit = {
This is the code:
val array1 = (1 to 10).toArray
val array2 = ArrayBuffer[Int]()
array1.foreach { i =>
array2 += (getSize(summary, i))
}
val array3 = new Array[Int](summary.getSize())
val arrays = ArrayBuffer[Array[AnyVal]](array1, array2.toArray, array3) # <-- ERROR1
Gnuplotter.plot(smap, arrays.toArray) # <-- ERROR2
However, I have two errors:
What might be wrong?
Upvotes: 1
Views: 1483
Reputation: 108169
Array
, being a mutable data structure, is not covariant (here's why)
So Array[Int]
is not a subtype of Array[AnyVal]
, hence you cannot pass it where an Array[AnyVal]
is expected.
Would a List
do for you purposes?
In case performance matters and you really need to use Array
, you can simply cast everything to an Array[Any]
and be done with it.
Alternatively, if you just need an Array[Any]
as the final type to pass to the plot
function, you can do everything with List
, and convert it with toArray[Any]
at the very end.
Upvotes: 6