Alex Rivera | Logout

Store int in ArrayList and get it back to primitive variable int - Java

Asked 2011-10-11T13:07:29.850
10

I'm getting an error, and can't find out how to solve it.

I add a int to an ArrayList.

int n = 1;
ArrayList list = new ArrayList();
list.add( n );

Further down, I try to put it back in another int:

grid[ y ][ x ] = list.get(0);

I also tried this:

grid[ y ][ x ] = (int) list.get(0);

But it doesn't work, I get this error:

found   : java.lang.Object
required: int
grid[ y ][ x ] = (int)list.get(0);
                              ^

I hope someone can help me.

Edit
Report

1 Answer

4

(Integer)list.get(0) will do the trick. Auto-unboxing will then convert it to an int automatically

answered 2011-10-11T13:11:04.383

Your Answer