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

时间:2022-10-07 15:04:44

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

Scala新手,有一个数组,其中一个元素是一个数组:

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? Thnaks

我究竟做错了什么? Thnaks

3 个解决方案

#1


2  

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

由于元素的类型不同,因此使用java.io.Serializable创建数组aaa类型

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

因此,当您引用第二个元素时,引用的类型将是Serializable,并且其中没有size属性。所以我们需要使用asInstanceOf明确地将第二个元素转换为/转换为Array。如下所示

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

#2


2  

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.

在这种情况下,所有字符串都将转换为字符序列。

#3


1  

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.

正如评论中所提到的,在数组中混合数组和非数组(以及通常不同类型的元素)并不是一个好主意。有时,有一些极端情况,当你无法做到这一点时,但通常,数组(和其他scala容器)意味着保持同类型。

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}")
 }

#1


2  

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

由于元素的类型不同,因此使用java.io.Serializable创建数组aaa类型

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

因此,当您引用第二个元素时,引用的类型将是Serializable,并且其中没有size属性。所以我们需要使用asInstanceOf明确地将第二个元素转换为/转换为Array。如下所示

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

#2


2  

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.

在这种情况下,所有字符串都将转换为字符序列。

#3


1  

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.

正如评论中所提到的,在数组中混合数组和非数组(以及通常不同类型的元素)并不是一个好主意。有时,有一些极端情况,当你无法做到这一点时,但通常,数组(和其他scala容器)意味着保持同类型。

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}")
 }