我想将 Integer 添加到类型安全的 ArrayList Of Float 类型。
Float a = new Float(1.1);
ArrayList<Float> obj = new ArrayList<Float>();
obj.add(a);//In the obj object I want to add integer. how can I do that?
Integer b = new Integer(1);
obj.add(b);/*The method add(Float) in the type ArrayList<Float>
is not applicable for the arguments (Integer)*/
最佳答案
将 ArrayList 的类型更改为:ArrayList<Number>
.
因为 Number
是 Float
的基类和 Integer
.因此您可以将两者都存储在列表中。
或转换您的 Integer
到 Float
值 obj.add(Float.valueOf(b));
关于java - 如何将整数添加到 ArrayList<Float>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27916247/