Question

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?

Was it helpful?

Solution

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).

OTHER TIPS

The call to get returns a raw (un-generic) ArrayList.

Not enough context has been given to suggest the best approach. The get method could be modified to declare its return type as a List<Integer>. If temp is a Map, refer to it with the proper generic type arguments: Map<Token, List<Integer>>. Or, if you can't modify the return type, you can assign the result to a List<?>, and cast its contents to Integer when you use them.

Note that I'm suggesting List instead of ArrayList. In general declaring, APIs with abstract types—rather than the implementation classes you happened to use—ensures future flexibility.

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().

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top