1

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

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

2
  • Since you are sure the object is of Integer type, you can simply Cast it! Commented Nov 1, 2012 at 5:30
  • 1
    Use Integer i = Integer.valueOf(someObject); docs.oracle.com/javase/6/docs/api/java/lang/… Commented Nov 1, 2012 at 5:59

1 Answer 1

3

You can explicitly cast it to Integer but dont need Java does does it for you.

if(arrayList.get(indexValue) instanceof Integer){
  Integer i = arrayList.get(indexValue);
}
Sign up to request clarification or add additional context in comments.

3 Comments

you don't need to even do that. Java will box and unbox from Integer to int
If I want to convert array[0} to integer can I convert `(int)array[0}' like this ???
yes you can, But as @Tom suggested, it box automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.