java 将 Object arrayList 对象转换为 Integer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13171459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:44:52  来源:igfitidea点击:

Convert Object arrayList object to Integer

javaobjectarraylist

提问by fean

I have created an ArrayList of Object and it contains both Integer and String.

我创建了一个对象的 ArrayList,它包含整数和字符串。

List<Object> arrayList = new ArrayList<>();

Using the below condition I have checked whether it is integer or not

使用以下条件我检查了它是否为整数

if(arrayList.get(indexValue) instanceof Integer){
 // Here how convert the object into integer
}

But the problem is, how can I convert the object into integer ?

但问题是,如何将对象转换为整数?

Thanks

谢谢

回答by Subhrajyoti Majumder

You can explicitly cast it to Integer.

您可以将其显式转换为 Integer。

if(arrayList.get(indexValue) instanceof Integer){
  Integer i = (Integer)arrayList.get(indexValue);
}