开发者

Java: Safely typecast a Clone of an ArrayList?

开发者 https://www.devze.com 2023-02-08 12:57 出处:网络
I would like to make a clone of my arraylist by this code: ArrayList<String> _names; ArrayList<String> names = (ArrayList<String>) _names.clone();

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开发者_StackOverflow to ArrayList<String>

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


names = new ArrayList<String>(_names);

Or use @SuppressWarnings("unchecked")

0

精彩评论

暂无评论...
验证码 换一张
取 消