scala> val arr = Array[Any](Array(1.0, 2.0))
arr: Array[Any] = Array(Array(1.0, 2.0))
scala> arr(0)
res28: Any = Array(1.0, 2.0)
The array arr is fixed. How to change arr(0) to Array[Double] in this situation?
There are three options:
1) Don't cast to Array[Any] in the first place:
val arr: Array[Array[Double]] = Array(Array(1.0, 2.0))
val arr0: Array[Double] = arr(0)
2) Use match:
val arr0: Array[Double] = arr(0) match {
case a: Array[Double] => a
}
3) Use asInstanceOf:
val arr0: Array[Double] = arr(0).asInstanceOf[Array[Double]]
These are in order of preference, with the option 1 being by far the best solution because it avoids throwing away type information.
Anygenerally means something is wrong