How to Find Array Size of an Element in Scala

Suraj P Feb 02, 2024 Scala Scala Array
  1. length and size Operator in Scala
  2. Get Size of the Element Inside an Array in Scala
How to Find Array Size of an Element in Scala

This article will explain finding an element’s size in an array. First, let’s understand how to get the size of the array and then extend that concept to get the size of elements present inside the array.

ADVERTISEMENT

length and size Operator in Scala

  1. length operator

Syntax:

Scala
 scalaCopyarray_name.length

We can use Scala’s length operator to calculate the array’s length in Scala. It returns the number of elements in an array.

Let’s have an example to understand it better.

Example code:

Scala
 scalaCopyobject MyClass {

    def main(args: Array[String]) {


       val myArr = Array("a", "b", Array(1,2,3,4,5,6), "c")
       println(myArr.length)


    }
}

Output:

 textCopy4

We have declared an array myArr, which contains 4 elements and one element is an array. Then we have used the length operator to get the array’s length.

  1. size operator

We can use the size property of the array to get its size. It returns the number of elements the array contains.

It is quite similar to the length property of an array.

Syntax:

Scala
 scalaCopyarray_name.size

Let’s see an example to understand it better.

Example code:

Scala
 scalaCopyobject MyClass {

    def main(args: Array[String]) {

    val myArr1 = Array(3, 2, 5, 7)
    val fruits = Array("Apple", "Orange",Array("watermelon","tomato"))
    val rate : Array[Double] = Array(4.6, 7.8, 9.5, 4.5)

    println(myArr1.size)
    println(fruits.size)
    println(rate.size)

    }
}

Output:

 textCopy4
3
4

In the above code, we have declared three arrays with some elements, and we have used the size operator to get their respective sizes.

Get Size of the Element Inside an Array in Scala

When we create an array of different elements, it is created with java.io.Serializable.

Example:

Scala
 scalaCopymyArr = Array("a", "b" , Array(11,22,33), "c")

Now, if we refer to the 2nd element of myArr, Array(11,22,33), its reference will be Serializable, and there is no length or size for it. And if we try to use them, we will get the error.

Example code:

Scala
 scalaCopyobject MyClass {

    def main(args: Array[String]) {


       val myArr = Array("a", "b", Array(11,22,33), "c")
       println(myArr(2).size)


    }
}

Output: We can see that we get the error.

 textCopyerror: value size is not a member of java.io.Serializable
       println(myArr(2).size)

To fix this, we have to explicitly typecast the second element of the array using asInstanceOf.

Example code one:

Scala
 scalaCopyobject MyClass {

    def main(args: Array[String]) {


       val myArr = Array("a", "b", Array(11,22,33), "c")

      if (myArr(2).isInstanceOf[Array[Int]])
        println(myArr(2).asInstanceOf[Array[Int]].size)


    }
}

Output: We can see that we get the size of the second element of the array myArr.

 textCopy3

In the above code, using the if condition, first, we check the type and the instance of the 2nd element of the array, and then we use asInstanceOf to typecast and then use the size operator to get its size.

Example code two:

Scala
 scalaCopyobject MyClass {

    def main(args: Array[String]) {


       val myArr = Array("a", List("tony","stark","hulk","iron man"), "c")

      if (myArr(1).isInstanceOf[List[String]])
        println(myArr(1).asInstanceOf[List[String]].size)


    }
}

Output:

 textCopy4
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - Scala Array