简体   繁体   English

Scala Cast List of Any 到 Int 列表

[英]Scala Cast List of Any to list of Int

Given a List of Any:给定一个列表:

val l = List(2.9940714E7, 2.9931662E7, 2.993162E7, 2.9931625E7, 2.9930708E7, 2.9930708E7, 2.9931477E7)

I need to cast each item to Int.我需要将每个项目转换为 Int。

Works:作品:

l(1).asInstanceOf[Double].toInt

Not:不是:

l.foreach{_.asInstanceOf[Double].toInt}
> java.lang.String cannot be cast to java.lang.Double

If如果

l.foreach{_.asInstanceOf[String].toDouble.toInt}
> java.lang.Double cannot be cast to java.lang.String

I'm new to Scala.我是 Scala 的新手。 Please tell me what I'm missing.请告诉我我缺少什么。 Why I can cast one item from list, but can't do this via iterator?为什么我可以从列表中投射一项,但不能通过迭代器做到这一点? Thanks!谢谢!

Your given List is of type Double .您给定的ListDouble类型。 You can use simply map operation to convert it to Int type.您可以使用简单的map操作将其转换为Int类型。 try following code,尝试以下代码,

val l: List[Double] = List(2.9940714E7, 2.9931662E7, 2.993162E7, 2.9931625E7, 2.9930708E7, 2.9930708E7, 2.9931477E7)

//simply map the list and use toInt
val intLst: List[Int] = l.map(_.toInt)

print(intLst)

//output
//List(29940714, 29931662, 29931620, 29931625, 29930708, 29930708, 29931477)

But suppose you have same List as List[Any] instead then you can use following to convert it to Int .但是假设您有与List List[Any]相同的List ,那么您可以使用以下方法将其转换为Int

val l: List[Any] = List(2.9940714E7, 2.9931662E7, 2.993162E7, 2.9931625E7, 2.9930708E7, 2.9930708E7, 2.9931477E7)

val intLst: List[Int] = l.map(_.asInstanceOf[Double].toInt)

It will give same output as above.它将给出与上面相同的输出。

It seems as if a String somehow ended up in your List l .似乎String以某种方式最终出现在您的List l

Given a list that is structured like this (with mixed integers, Double s, and String s):给定一个结构如下的列表(带有混合整数、 Double s 和String s):

val l = List[Any](2.9940714E7, 2.9931625E7, "2.345E8", 345563)

You can convert it to list of integers as follows:您可以将其转换为整数列表,如下所示:

val lAsInts = l.map {
  case i: Int => i
  case d: Double => d.toInt
  case s: String => s.toDouble.toInt
}

println(lAsInts)

This works for Double s, Int s and String s.这适用于Double s、 Int s 和String s。 If it still crashes with some exceptions during the cast, then you can add more cases.如果在转换过程中它仍然因某些异常而崩溃,那么您可以添加更多案例。 You can, of course, do the same in a foreach if you want, but in this case it wouldn't do anything, because you don't do anything in the body of foreach except casting.当然,如果你愿意,你可以在foreach做同样的事情,但在这种情况下它不会做任何事情,因为除了强制转换之外,你不会在foreach的主体中任何事情。 This has no useful side-effects (eg it prints nothing).这没有任何有用的副作用(例如它什么都不打印)。

Another option would be:另一种选择是:

lAsInts = l.map{_.toString.toDouble.toInt}

This is in a way even more forgiving to all kind of weird input, at least as long as all values are "numeric" in some sense.这在某种程度上对各种奇怪的输入更加宽容,至少只要所有值在某种意义上都是“数字”。

However, this is definitely code-smell: how did you get a list with such a wild mix of values to begin with?然而,这绝对是代码的味道:你是如何得到一个包含如此疯狂混合值的列表的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM