Scala: new byte array

To create a new byte arrays in scala, you have two options.

To initialize it with data, you can do this:

val data = Array[Byte](10, -32, 17, 22)

If you want to initialize it to a specific length like a Java array, do this:

val length = 1000
Array.fill[Byte](length)(0)

3 Replies to “Scala: new byte array”

  1. Please remove the Array.fill example – I just tried it and performance is really bad! You should use:

    new Array[Byte](length)

    1. Thanks for the research. I was trying to use this to generate byte arrays for ND4S, so if you’re doing something like that, you can also initialize them from sequences, maybe that would perform better?

Leave a Reply

Your email address will not be published. Required fields are marked *