Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canonical way for empty Array in Scala?

Tags:

scala

What is the canonical way to get an empty array in Scala? new Array[String](0) is too verbose.

like image 561
Landon Kuhn Avatar asked Jun 22 '10 14:06

Landon Kuhn


People also ask

How do you create an empty list in Scala?

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.

How do I fill an array in Scala?

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.

What is array empty?

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).

What does an empty array contains C?

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.


1 Answers

Array[String]() 

You can leave out the [String] part if it can be inferred (e.g. methodThatAlwaysTakesAStringArray( Array() )).

like image 170
sepp2k Avatar answered Sep 28 '22 07:09

sepp2k