How do I get rid of this warning?
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 tried to pass it to ArrayList, but it doesn't matter, it appears anyway.
How can I get rid of this?
source to share
ArrayList is a Java collection and can contain any type of object. The get temp method is supposed to return a simple ArrayList, but you specify that tempArray will be an ArrayList containing only integers.
If you wrote a class for temp then get method must be declared to return
ArrayList<Integer>
If you haven't, you will have to make tempArray a simple ArrayList without a generic integer type.
See http://java.sun.com/docs/books/tutorial/java/generics/index.html for more information on generic types in Java.
Warning. A common type of check in java is compile time, it is missing at runtime (aka style erasure).
source to share
The call get
returns unhandled (not generic) ArrayList
.
Not enough context to suggest a better approach. The method get
can be modified to declare its return type as List<Integer>
. If temp
a Map
contact to him with the appropriate generic arguments: Map<Token, List<Integer>>
. Or, if you cannot change the return type, you can assign the result List<?>
and pass its contents in Integer
when you use them.
Note that I am suggesting List
instead ArrayList
. In general, API declarations with abstract types - rather than the implementation classes you used to use - provide future flexibility.
source to share
This usually happens because the return type of the get () method on the temporary object returns an ArrayList without the generics specification, and tempArray an ArrayList of integers. Potentially the ArrayList assigned by tempArray at runtime can contain objects that are not integral. At compile time, Java cannot determine what type of object is in the ArrayList returned by get ().
source to share
It looks like you are missing the description of the temp variable declaration. My guess is that temp is a map (based on the fact that it has a get method.) If so, then you are probably not fully declaring generic types for that map. If type 'theToken' is String then your map will map between String and ArrayList. Thus, the declaration should look something like this:
Map<String, ArrayList<Integer>> temp = new HashMap<String, ArrayList<Integer>>();
To improve your style a bit, you can go from referencing a specific type of "ArrayList" to a list of interfaces by changing these two lines to:
Map<String, List<Integer>> temp = new HashMap<String, List<Integer>>();
This makes it so that you can switch from one kind of list (like ArrayList) to another (like Vector or LinkedList) without changing the code that uses them.
source to share