In Scala, the size of an array can be obtained using the length property or the size method. Here are some examples illustrating how to obtain the size of an array in Scala:
Example 1: Using the length property
val nums = Array(1, 2, 3, 4, 5)
val size = nums.length
println("Size of nums array: " + size)
Output:
Size of nums array: 5
Example 2: Using the size method
val fruits = Array("apple", "banana", "orange", "mango")
val size = fruits.size
println("Size of fruits array: " + size)
Output:
Size of fruits array: 4
In both examples, the length property and size method return the number of elements in the array. You can use whichever method you prefer.