2

I want to add Integer to an Type safe ArrayList Of Float type.

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)*/
2
  • 2
    Would it suffice to convert the integer to a float? Commented Jan 13, 2015 at 6:40
  • 2
    You can't. A list of Float can only, logically, contain Floats. Luckily both integers and floats are numbers so it's not too hard to convert an integer to a float. Commented Jan 13, 2015 at 6:42

5 Answers 5

7

Change the type of ArrayList to: ArrayList<Number>.

Because Number is the base class of Floatand Integer. So you can store both in the list.

or convert your Integer to a Float value obj.add(Float.valueOf(b));

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, both is possible. But the valueOf forms “should generally be used in preference to the constructor[s as these methods are] likely to yield significantly better space and time performance by caching frequently requested values.” I just think it is better to not give misleading examples to beginners even though it arguably doesn't matter in the context of this question.
I cannot change the type of ArrayList<Float> to ArrayList<Number>
@jonesjalapat Than you have to use my second approach. Convert the Integer to a Float
0

you can not specify the type of the ArrayList like :

    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);
    ArrayList newobj = (ArrayList) obj;
    newobj.add(b);

    for (Object object : newobj) {
        System.out.println(object.getClass());
    }

Would output:

class java.lang.Float
class java.lang.Integer

Or you can Use an ArrayList<Number>:

    Float a = new Float(1.1);
    ArrayList<Number> obj = new ArrayList<Number>();
    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);

    for (Number object : obj) {
        System.out.println(object.getClass());
    }

Would output:

class java.lang.Float
class java.lang.Integer

Comments

0

Try this

obj.add((float) b);

This get the float number of the integer

Or

obj.add(Float.parseInt(b));

Comments

0

What about

obj.add(b.floatValue());

Or use an ArrayList<Number>.

Comments

0

This is how i finally added Integer without changing ArrayList Type, But warnings are generated

public class MyArrayList{
public static void main(String[] args) {
    Float a = new Float(1.1);
    ArrayList<Float> obj = new ArrayList<Float>();
    obj.add(a);
    function1(obj);
    for (Object obj2 : obj) {
        System.out.println(obj2);
    }
}
private static void function1(ArrayList list) {
    Integer b = new Integer(1);
    list.add(b);
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.