We can get the length of an array in Scala by using the length
property. The length
property returns the length of an array in Scala.
array.length
An integer that represents the number of elements in an array.
object Main extends App {// create some arraysval randomNumbers = Array(34, 2, 5, 70, 1)val fruits = Array("Pineapple", "Banana", "Orange")val cgpas : Array[Double] = Array(3.4, 4.5, 2.5, 0.5)val names = new Array[String](3)names(0) = "Amara"names(1) = "Funke"names(2) = "Ade"// print length of arraysprintln(randomNumbers.length)println(fruits.length)println(cgpas.length)println(names.length)}
length
property and then print these lengths to the console.