1

What's the easiest way to convert an ArrayList to ArrayList<Type> without the "unchecked or unsafe operations" warning? More specifically, what's the easiest way to deal with the situation below?

public static void doStuff(ArrayList... a) {
    // stuff
}

I want to be able to call the method with any number of parameters, which means a will be an array and so I can't specify the type. So unless there's an alternate solution, I have an array of ArrayLists that I need to convert into ArrayList<Type>.

3
  • @SuppressWarning("unchecked") Commented Feb 2, 2015 at 22:57
  • There's no way to have a generic array (varargs is an array) without unchecked conversion. Whether or not it's safe depends on what // stuff does. Commented Feb 2, 2015 at 23:03
  • 1
    I recommend you post the real question. Always a bad idea to think you have to do something then ask about that. There probably is a better overall solution. Commented Feb 2, 2015 at 23:26

2 Answers 2

2

If you need to convert a plain ArrayList to an ArrayList<Type> then it logically is an unsafe conversion, because the compiler can't guarantee the types will line up. If it is not an issue in this case, ignore or suppress the warning. It is just telling you to be careful: it doesn't necessarily mean there is a problem with your code.

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

Comments

1

Provided that its not required that anything be added or removed from the List you could do

public static void doStuff(List<?>... list) {

Given that ArrayList is a variable size collection it probably makes more sense to use

public static void doStuff(List<?> list) {

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.