scala 在scala中,如何找到数组元素的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37787176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In scala, how can I find the size of an array element
提问by Ezer K
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? Thanks
我究竟做错了什么?谢谢
采纳答案by rajuGT
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 aaatype is created with java.io.Serializable
由于元素的类型不同,所以aaa使用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 Serializableand there is no size property in it. So we need to explicity typecast/convert the 2nd element to Array using asInstanceOf. As shown below
因此,当您引用第二个元素时,引用的类型将为 ofSerializable并且其中没有 size 属性。所以我们需要使用asInstanceOf明确地将第二个元素类型转换/转换为数组。如下所示
if (aaa(2).isInstanceOf[Array[Int]])
aaa(2).asInstanceOf[Array[Int]].size
回答by mavarazy
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.
在这种情况下,所有字符串都将转换为字符序列。
回答by Dima
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}")
}

