Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unchecked assignment for 'java.util.ArrayList'

I get the warning:

Unchecked assignment for 'java.util.ArrayList' to 'java.util.ArrayList < com.test.mytest >'

for:

private ArrayList<LocoList> myLocations = new ArrayList();

How to fix it?

like image 237
gogoloi Avatar asked May 19 '17 19:05

gogoloi


People also ask

What is unchecked assignment in Java?

Unchecked assignment: 'java.util.List' to 'java.util.List<java.lang.String>' It means that you try to assign not type safe object to a type safe variable. If you are make sure that such assignment is type safe, you can disable the warning using @SuppressWarnings annotation, as in the following examples.


1 Answers

You want new ArrayList<>(); so that you use the right generic type. At the moment you're using the raw type on the right hand side of =. So you want:

private ArrayList<LocoList> myLocations = new ArrayList<>();

Or just be explicit:

private ArrayList<LocoList> myLocations = new ArrayList<LocoList>();

(You might also consider making the type of the variable List<LocoList> instead of ArrayList<LocoList>, unless you're using ArrayList-specific members. I'd also ditch the "my" prefix, personally.)

like image 97
Jon Skeet Avatar answered Sep 21 '22 03:09

Jon Skeet