简体   繁体   English

如何将对象转换为int

[英]How convert an object to int

I want to convert an object to int type.... eg: Object obj=........; 我想将一个对象转换为int类型。...例如:Object obj = ........; int count = Integer.parseInt((String) obj); int count = Integer.parseInt((String)obj);

when i use above code ai got cast exception. 当我使用上面的代码时,ai获得了强制转换异常。

Anyone know how to cast object to int...? 有谁知道如何将对象转换为int ...?

Use obj.hashCode() , to get an int that represents the object. 使用obj.hashCode() ,获取表示该对象的int。 But what is your purpose? 但是你的目的是什么? The code you posted does not work - you try to cast the object to a string rather than calling toString(), but even then, unless the toString representation of the object is itself an integer, calling Integer.parseInt on it will throw an exception. 您发布的代码不起作用-您尝试将对象转换为字符串而不是调用toString(),但是即使那样,除非对象的toString表示本身是整数,否则对其调用Integer.parseInt将会引发异常。

So what are you aiming at? 那么您的目标是什么?

If tableres is a Hashtable, the values are prolly Integer, not int. 如果tableres是一个Hashtable,则值应为Integer,而不是int。 In that case, try: 在这种情况下,请尝试:

int i = ((Integer) tableres.get ("mCount")).intValue(); int i =(((Integer)tableres.get(“ mCount”))。intValue();

Good luck, - MS 祝你好运-MS

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM