What is the canonical way to get an empty array in Scala? new Array[String](0)
is too verbose.
If you want to use the add operation, you would have to declare an ArrayList. Vals in scala are essentially immutable, so you can't add to them. iirc val is more like final, you can add to them if you use the mutable Collections.
Creating and Filling Array Random Values in ScalaUse the nextInt() method to fill the array with random values. It generates the random integer values, and to get filled, we used the fill keyword.
An empty array is an array of length zero; it has no elements: int[] emptyArray = new int[0]; (and can never have elements, because an array's length never changes after it's created).
There's no such thing as an "empty array" or an "empty element" in C. The array always holds a fixed pre-determined number of elements and each element always holds some value. The only way to introduce the concept of an "empty" element is to implement it yourself.
Array[String]()
You can leave out the [String]
part if it can be inferred (e.g. methodThatAlwaysTakesAStringArray( Array() )
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With