tags:

views:

217

answers:

4

I am getting the following warning from my Java code:

Lexer.java:591: warning: [unchecked] unchecked conversion
found   : java.util.ArrayList
required: java.util.ArrayList<java.lang.Integer>
ArrayList<Integer> tempArray = temp.get(theToken);

I've tried casting it to ArrayList but this doesn't matter, it still appears.

How can I get rid of this?

+6  A: 
erickson
A: 

This usually happens because the return type from the get() method in the temp object returns ArrayList without the generics specification while tempArray is an ArrayList of Integers. Potentially the ArrayList assigned to tempArray during run time can contain objects that are Not Integers. During compile time Java cannot determine what type of object is in the ArrayList returned by get().

Vincent Ramdhanie
+3  A: 

ArrayList is a Java Collection and can hold any type of object. The get method of temp is presumably declared to return a plain ArrayList but you are specifying tempArray be an ArrayList holding only integers.

If you wrote the class for temp then the get method needs to be declared to return an ArrayList<Integer>

If you didn't then you will have to make tempArray a plain ArrayList without a generic type of integer.

See http://java.sun.com/docs/books/tutorial/java/generics/index.html for more information on generic types in Java.

On caveat - The generic type checking in java is compile time only, it isn't present at runtime (so called type erasure).

Alex Stoddard
A: 

It looks like you're missing a generic declaration on the temp variable. I'll assume that temp is a map (based on the fact that it has a get method.) If this is the case, then you are probably not completely declaring the generic types for this map. If the type of 'theToken' is String, then your map appears to be mapping between String and ArrayList. As such it's declaration should look something like this:

Map<String, ArrayList<Integer>> temp = new HashMap<String, ArrayList<Integer>>();

To improve your stile a little you could switch from referencing the specific type 'ArrayList' to the interface List by changing those two lines to look like this:

Map<String, List<Integer>> temp = new HashMap<String, List<Integer>>();

What this does is make it so that you can change from one kind of list (like ArrayList) to another (like Vector or LinkedList) without changing any of the code that uses them.

Spina