我想将Integer添加到Float类型的类型安全ArrayList中。
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)*/
Run Code Online (Sandbox Code Playgroud)
将ArrayList的类型更改为:ArrayList<Number>。
因为Number是基类的Float和Integer。因此,您可以将它们都存储在列表中。
或将您转换Integer为Float价值obj.add(Float.valueOf(b));
| 归档时间: |
|
| 查看次数: |
3052 次 |
| 最近记录: |