Unchecked conversion when popping stack Java
-
29-09-2019 - |
Question
I'm new to Java and have a question about a warning:
My general code:
private Stack<ArrayList> stackFrame = new Stack<ArrayList>();
private ArrayList<Object> curBlocKList = new ArrayList<Object>();
...
curBlockList = stackFrame.pop();
I'm getting:
Parser.java:78: warning: [unchecked] unchecked conversion
found : java.util.ArrayList
required: java.util.ArrayList<java.lang.Object>
curBlockList = stackFrame.pop();
I don't know how to syntactically make this work without a warning, as I'm working on a homework assignment and errors aren't allowed in compiling, and inserting
@SurpressWarning("unchecked")
is not allowed either.
What do I need to do to get rid of this warning?
Also, I want curBlocKList to hold a reference to the current top of the stack. Will this be accomplished by
curBlockList = stackFrame.pop();
or is there something else I need to do?
Solution
@SurpressWarning("unchecked")
should be put before the method prototype to work.
But you should try to change
private Stack<ArrayList> stackFrame = new Stack<ArrayList>();
Into
private Stack<ArrayList<Object>> stackFrame = new Stack<ArrayList<Object>>();
OTHER TIPS
You need to parameterize ArrayList within your parameterization of Stack. Also, I recommend using List instead of ArrayList; you should program to the interface, not the implementation.
private Stack<List<?>> stackFrame = new Stack<List<?>>();
private List<?> curBlockList = new ArrayList<Object>();