[英]Storing Double in ArrayList
我有一个Integer类型的ArrayList,是否可以在其中存储一个值(例如1.25)而不必将其强制转换为int,因此在小数点后丢失位?
No, you cannot store doubles in ArrayList<Integer>
without loss of precision. 不,您不能在不损失精度的情况下在
ArrayList<Integer>
存储双精度数。 You can, however, store them in ArrayList<Double>
. 但是,您可以将它们存储在
ArrayList<Double>
。
Use ArrayList<Number>
instead. 请改用
ArrayList<Number>
。 In this way, you will be able to store both Double
and Integer
instances since both classes derive from Number
class. 这样,由于两个类都是从
Number
类派生的,因此您将能够存储Double
和Integer
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.