If in Scala IDE try the following:
val chars = Array[Char](256)
it is all fine. But if I do this:
val len = 256
val chars = Array[Char](len)
it says that it expects a Char instead of len? Why? I expect the behavior to be the same! Why does it think that I want to put that thing in the array instead of specifying it’s size? As far as I know, there is no constructor for arrays that takes a single argument to place it inside the array.
This works because 256 treated as a
Charand it creates one-element array (with code 256)Here len is
Int, so it failsTo create array of specified size you need something like this
where
{0}is a function to produce elementsIf the contents of the Array don’t matter you can also use
newinstead offill: