简体   繁体   English

java.lang.ClassCastException:java.lang.Integer无法强制转换为java.lang.Double

[英]java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

I'm Facing Issues at the Run time to compile this piece of code Which is throwing me an error that java.lang.Integer cannot be cast to Java.lang.Double. 我正在运行时遇到问题来编译这段代码这给我一个错误,即java.lang.Integer无法强制转换为Java.lang.Double。 I'll more than happy if anyone help me out to correct this code 如果有人帮我修改这段代码,我会非常高兴

 double x;

 public Double getMethod() {

     HashMap hashmap= new HashMap();

     hashmap = SumCal();

     List  listabc = (List) hashmap.get("abclist");
     int total=(Integer) hashmap.get("all_total");
     x = (Double) listabc.get(0)*100/total;
     return x;
    }

You can do as below, But I would advice you to go with Generics . 你可以这样做,但我建议你去Generics

x = ((Integer) listabc.get(0) * 100 / total);

If you had used the Generics like below you dont need any casting. 如果您使用过如下的Generics,则不需要任何投射。

List<Integer> listabc 
HashMap<String, Integer> hashmap 
x = listabc.get(0) * 100 / total;

In that case you dont need any casting. 在那种情况下,你不需要任何铸造。 One of the reason Integer, Double etc wrapper classes were introduced is to avoid casting. 引入Integer,Double等包装类的原因之一是避免强制转换。

A slight change in your code will grant success: 您的代码稍有变化即可获得成功:

x = (double) listabc.get(0)*100/total;

Integer and Double are not cast compatible, but int and double are widening compatible. Integer和Double不兼容,但int和double兼容扩展。 In your current code, the right hand side (RHS) of your expression is getting autoboxed up to an Integer, then you specify a cast of that result to Double . 在当前代码中,表达式的右侧(RHS)将自动升级为整数,然后将该结果的强制转换指定为Double This cast fails. 这次演员失败了。

By instead specifying a widening conversion, the Integer result gets unboxed into an int , then converted to a double . 通过改为指定扩展转换,将Integer结果取消装入int ,然后转换为double Then finally this line: 最后这一行:

return x;

Boxes up the value of x into a Double and returns it as the result of the method. x的值填入Double并将其作为方法的结果返回。

First, you should not use a generic HashMap , because then the compiler can't help you with types. 首先,您不应该使用泛型HashMap ,因为编译器无法帮助您使用类型。 Try declaring the following: 尝试声明以下内容:

HashMap<String, Integer> hashmap = new HashMap();

I would not recommend storing a HashMap of differently-valued types, as it just invites programming errors. 我不建议存储具有不同值的类型的HashMap,因为它只会引发编程错误。 Also, there's no need to cast an int to a Double . 此外,没有必要将int转换为Double You can do any of the following: 您可以执行以下任何操作:

x = listabc.get(0) * 100.0 / total; // multiply by a floating-point number

x = (double) listabc.get(0) * 100 / total; // cast with (double)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate:java.lang.ClassCastException:java.lang.Integer无法强制转换为java.lang.Double - Hibernate : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double java.lang.Integer无法转换为java.lang.Double - java.lang.Integer cannot be cast to java.lang.Double java.lang.ClassCastException:无法强制转换java.lang.Integer - java.lang.ClassCastException: java.lang.Integer cannot be cast java.lang.ClassCastException:无法转换为java.lang.Integer - java.lang.ClassCastException: cannot be cast to java.lang.Integer Hive JSON SerDe — ClassCastException:无法将java.lang.Integer强制转换为java.lang.Double - Hive JSON SerDe — ClassCastException: java.lang.Integer cannot be cast to java.lang.Double java.lang.ClassCastException:java.lang.String 无法转换为 java.lang.Double - java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double java.lang.ClassCastException:java.lang.Double无法强制转换为java.lang.String - java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String java.lang.ClassCastException:java.lang.Long 不能在 java 1.6 中转换为 java.lang.Integer - java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer in java 1.6 java.lang.ClassCastException:无法在Tableau中将java.lang.String强制转换为java.lang.Integer? - java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer in tableau? java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM