简体   繁体   中英

Storing Double in ArrayList

我有一个Integer类型的ArrayList,是否可以在其中存储一个值(例如1.25)而不必将其强制转换为int,因此在小数点后丢失位?

No, you cannot store doubles in ArrayList<Integer> without loss of precision. You can, however, store them in ArrayList<Double> .

Use ArrayList<Number> instead. In this way, you will be able to store both Double and Integer instances since both classes derive from Number class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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