15

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?

3
  • Casting to ArrayList<?> will not give this warning. Do you need it to be an ArrayList<String>? Can you call toString on the resulting elements instead? Commented Feb 3, 2011 at 15:00
  • for similar cases I have "unchecked" disabled by default :) Commented Feb 3, 2011 at 16:54
  • @ finnw: I need it for other types than just strings as well @ bestsss: i like the solution of cadrian better :) Commented Feb 3, 2011 at 20:46

1 Answer 1

34
names = new ArrayList<String>(_names);

Or use @SuppressWarnings("unchecked")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.