I want to access via one-liner a list-to-array element by index.
This gives me an error:
Array(1,2,3)(2) // 3
List(1,2,3).toArray(2) // error
(List(1,2,3).toArray)(2) // error
// error: type mismatch;
// found : scala.this.Int(2)
// required: reflect.this.ClassTag[?]
However, this works:
val a = Array(1,2,3)
val b = List(1,2,3).toArray
println(a(2)) // 3
println(b(2)) // 3
Why is that so and how to make a solution without making extra val's?
b(2)? If you are only going to access it one time, it would be more efficient this way. Other alternative (if you still want to make the conversion) isb.toArray.apply(2).