Scala - creating a type parametrized array of specified length Scala - creating a type parametrized array of specified length arrays arrays

Scala - creating a type parametrized array of specified length


val chars = Array[Char](256)

This works because 256 treated as a Char and it creates one-element array (with code 256)

val len = 256val chars = Array[Char](len)

Here len is Int, so it fails

To create array of specified size you need something like this

val chars = Array.fill(256){0}

where {0} is a function to produce elements

If the contents of the Array don't matter you can also use new instead of fill:

val chars = new Array[Char](256)


Use Array.ofDim[Char](256).

See API docs here.