H.v.M.
H.v.M.

Reputation: 1659

ArrayList to ArrayList<Type>

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>.

Upvotes: 1

Views: 103

Answers (2)

Reimeus
Reimeus

Reputation: 159754

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) {

Upvotes: 1

khelwood
khelwood

Reputation: 59114

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.

Upvotes: 2

Related Questions