Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In scala, how can I find the size of an array element

Scala newbie, Have an array where one element is an array:

val aaa = Array("a", "b", Array(1, 2, 3), "c")

This works:

In []: aaa(2)
Out[]: Array(1, 2, 3)

This works:

In []: Array(1, 2, 3).size
Out[]:3

This does not:

In []: aaa(2).size
Out[]:
Name: Compile Error
Message: <console>:15: error: value size is not a member of                     
java.io.Serializable
          aaa(2).size
                 ^

What am I doing wrong? Thanks

like image 262
Ezer K Avatar asked Jun 13 '16 10:06

Ezer K


People also ask

What is an array in Scala?

Scala | Arrays. Array is a special kind of collection in scala. it is a fixed size data structure that stores elements of the same data type. The index of the first element of an array is zero and the last element is the total number of elements minus one.

How to get the size of an array in JavaScript?

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. Let’s see an example to understand it better. In the above code, we have declared three arrays with some elements, and we have used the size operator to get their respective sizes.

What is generic array in Scala?

Scala arrays can be generic. which mean we can have an Array [T], where T is a type parameter or abstract type. Scala arrays are compatible with Scala sequences – we can pass an Array [T] where a Seq [T] is required. Scala arrays also support all sequence operations. The following figure shows how values can be stored in array sequentially :

How to concatenate two arrays in Scala?

Here, arr1 is an array of four elements and arr2 is another array of four elements now we concatenate these two array in arr3 by using concat () method. The Multidimensional arrays contains more than one row to store the values. Scala has a method Array.ofDim to create Multidimensional arrays in Scala .


3 Answers

When you create an array using the following literal

val aaa = Array("a", "b", Array(1, 2, 3), "c")

Since the type of the elements are different, the array aaa type is created with java.io.Serializable

aaa: Array[java.io.Serializable] = Array(a, b, Array(1, 2, 3), c)

So when you refer back the 2nd element, the type of the reference will be of Serializable and there is no size property in it. So we need to explicity typecast/convert the 2nd element to Array using asInstanceOf. As shown below

 if (aaa(2).isInstanceOf[Array[Int]]) 
       aaa(2).asInstanceOf[Array[Int]].size
like image 166
rajuGT Avatar answered Sep 19 '22 13:09

rajuGT


Most common type for your declaration is serializable

val aaa = Array("a", "b", Array(1, 2, 3), "c")
Array[java.io.Serializable] 

If you want to use it with size, you can explicitly define:

val aaa: Array[Seq[Any]] = Array("a", "b", Array(1, 2, 3), "c")

all Strings will be converted to Sequences of Chars in this case.

like image 24
mavarazy Avatar answered Sep 22 '22 13:09

mavarazy


As mentioned in the comments, it is not a good idea to mix arrays and non-arrays (and, in general, elements of different types) in an array. Sometimes, there are corner cases, when you can't get around having to do that, but as a rule, arrays (and other scala containers) are meant to hold homogenous types.

So, I would recommend to begin with splitting your array into two:

val (arrays, nonArrays) =  
  Array("a", "b", Array(1, 2, 3), "c").partition { 
     case a: Array[_] => true
     case _ => false
  }

Now, you can easily tell the lengths of all your arrays:

  arrays.foreach { println(_.size) }

If you wanted to preserve the original position information, you could zip the original array with indexes first:

 val (arrays, nonArrays) = Array("a", "b", Array(1, 2, 3), "c")
   .zipWithIndex
   .partition {
      case (a: Array[_], _) => true
      case _ => false
   }

 arrays.foreach { 
   case (array, index) => 
     prinlnt(s"Array length at index $index is ${array.size}")
 }
like image 39
Dima Avatar answered Sep 22 '22 13:09

Dima