java. lang.ClassCastException:java. Lang. integer cannot be cast to Java lang.Double
•
Java
I'm running into a problem compiling this code, which gives me an error, Java Lang. integer cannot be cast to Java lang.Double. If someone helped me modify this code, I would be very happy
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; }
Solution
You can do this, but I'll suggest you go to generics
x = ((Integer) listabc.get(0) * 100 / total);
If you have used the following generics, no projection is required
List<Integer> listabc HashMap<String,Integer> hashmap x = listabc.get(0) * 100 / total;
In that case, you don't need any casting One of the reasons for introducing wrapper classes such as integer and double is to avoid coercion
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码