ClassCastException, casting Integer to Double

后端 未结 10 1934
我寻月下人不归
我寻月下人不归 2020-12-24 05:33
ArrayList marks = new ArrayList();
Double sum = 0.0;
sum = ((Double)marks.get(i));

Everytime I try to run my program, I get a ClassCastException th

相关标签:
10条回答
  • 2020-12-24 05:39

    Well the code you've shown doesn't actually include adding any Integers to the ArrayList - but if you do know that you've got integers, you can use:

    sum = (double) ((Integer) marks.get(i)).intValue();
    

    That will convert it to an int, which can then be converted to double. You can't just cast directly between the boxed classes.

    Note that if you can possibly use generics for your ArrayList, your code will be clearer.

    0 讨论(0)
  • 2020-12-24 05:44
    sum = Double.parseDouble(""+marks.get(i));
    
    0 讨论(0)
  • 2020-12-24 05:46

    Changing an integer to a double

    int abc=12; //setting up integer "abc"
    
    System.out.println((double)abc); 
    

    The code will output integer "abc" as a double, which means that it will display as "12.0". Notice how there is a decimal place, indicating that this precision digit has been stored.

    Same with double if you want to change it back,

    double number=13.94;
    
    System.out.println((int)number); 
    

    This code will print on one line, "number" as an integer. The output will be "13". Notice that the value has not been rounded up, the data has actually been omitted.

    0 讨论(0)
  • 2020-12-24 05:53

    This means that your ArrayList has integers in some elements. The casting should work unless there's an integer in one of your elements.

    One way to make sure that your arraylist has no integers is by declaring it as a Doubles array.

        ArrayList<Double> marks = new ArrayList<Double>();
    
    0 讨论(0)
  • 2020-12-24 05:55

    2 things to understand here -

    1) If you are casting Primitive interger to Primitive double . It works. e.g. It works fine.

    int pri=12; System.out.println((double)pri);

    2) if you try to Cast Integer object to Double object or vice - versa , It fails.

    Integer a = 1; Double b = (double) a; // WRONG. Fails with class cast excptn
    

    Solution -

    Soln 1) Integer i = 1; Double b = new Double(i);
    soln 2) Double d = 2.0; Integer x = d.intValue();
    
    0 讨论(0)
  • 2020-12-24 05:56

    The code posted in the question is obviously not a a complete example (it's not adding anything to the arraylist, it's not defining i anywhere).

    First as others have said you need to understand the difference between primitive types and the class types that box them. E.g. Integer boxes int, Double boxes double, Long boxes long and so-on. Java automatically boxes and unboxes in various scenarios (it used to be you had to box and unbox manually with library calls but that was deemed an ugly PITA).

    http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

    You can mostly cast from one primitive type to another (the exception being boolean) but you can't do the same for boxed types. To convert one boxed type to another is a bit more complex. Especially if you don't know the box type in advance. Usually it will involve converting via one or more primitive types.

    So the answer to your question depends on what is in your arraylist, if it's just objects of type Integer you can do.

    sum = ((double)(int)marks.get(i));
    

    The cast to int will behind the scenes first cast the result of marks.get to Integer, then it will unbox that integer. We then use another cast to convert the primitive int to a primitive double. Finally the result will be autoboxed back into a Double when it is assigned to the sum variable. (asside, it would probablly make more sense for sum to be of type double rather than Double in most cases).

    If your arraylist contains a mixture of types but they all implement the Number interface (Integer, Short, Long, Float and Double all do but Character and Boolean do not) then you can do.

    sum = ((Number)marks.get(i)).doubleValue();
    

    If there are other types in the mix too then you might need to consider using the instanceof operator to identify them and take appropriate action.

    0 讨论(0)
提交回复
热议问题