Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Safely typecast a Clone of an ArrayList?

I would like to make a clone of my arraylist by this code:

ArrayList<String> _names;

ArrayList<String> names = (ArrayList<String>) _names.clone();

As far as I know, nothing special. My compiler however, gives the following warning:

Type safety: Unchecked cast from Object to ArrayList<String>

Does anybody know a cleaner solution that does not give me a warning?

like image 677
Len Avatar asked Feb 03 '11 12:02

Len


1 Answers

names = new ArrayList<String>(_names);

Or use @SuppressWarnings("unchecked")

like image 157
cadrian Avatar answered Oct 10 '22 10:10

cadrian