I can't make an arraylist of integers?!

I started using arraylists a few weeks ago for my game because it seemed like the thing to do
I love them! I am pretty acquainted with arraylists

Previously I’ve been using them to store entities in a room. I have a problem that I decided is best solved by an arraylist of integers
But this code errors:

private static ArrayList<int> occupiedRowList = new ArrayList<int>();

Looks good to me. What’s wrong with it?

Thanks

You need to have an ArrayList of Integer not int, int is a primitive data type not an object.

Generics in Java have the limitation of only supporting objects, no primitives as Swattkidd7 mentioned.

For primitive types, you have to use their class counterparts and rely on auto-boxing and auto-unboxing magic, for example:


ArrayList<Integer> myList = new ArrayList<Integer>();

myList.add(3); //the compiler automatically turns this into myList.add(Integer.valueOf(3));
int i = myList.get(0); //the compiler automatically turns this into int i = myList.get(0).intValue();

This works the same for all the other primitive types and their class counterparts.

Huh… weird. Well, it does work. Thanks everybody

You might want to use the Apache Commons Collections API:

http://commons.apache.org/collections/
http://commons.apache.org/collections/api/
http://commons.apache.org/primitives/apidocs/org/apache/commons/collections/primitives/IntList.html

I wouldn’t recommend apache collections anymore, it is quite outdated. From Java 2 times?
Take a look at google guava http://code.google.com/p/guava-libraries, which is activly developed and has a lot of more cool features.

I never used them either, but on the other hand, how many bugs can you write in IntList ?