Grumblesaurus
Grumblesaurus

Reputation: 3119

Type Safety - A Generic Array is created for varargs parameter - Is this a good solution?

If I add columns to a JavaFX TableView:

tableView.getColumns().addAll( col1, col2, col3 );

I get this warning:

Type safety: A generic array of TableColumn< T, ? > is created for a varargs parameter

If I manually put the vargs into a list, I don't get the warning:

tableView.getColumns().addAll( Arrays.asList( col1, col2, col3 ) );

Is this a good way to deal with this warning, or is there a better way? It doesn't feel right to get a warning for just using a varargs method.

Upvotes: 1

Views: 2743

Answers (3)

RegreDanger
RegreDanger

Reputation: 11

You can still use the asList() method of the Arrays class if you know all the columns you'll add to the table, if you want an unmodifiable list you can also use the of() method of the List class.

In any case, both solve the warning and it´s correct to use.

Upvotes: 1

Jayanth
Jayanth

Reputation: 816

Judging from the way you have coded i.e.

made a collection of columns individually, then I add them to a table. I never use them in the context of a list except to add them to the table using the table's addAll vararg method.

the way you have made use of Arrays could be justified . I could not think of a better reason myself. From my knowledge standpoint you are good to go.

If you come to know at some point of time, that this can be achieved in a more discreet manner, feel free to update me here.

Upvotes: 1

Mureinik
Mureinik

Reputation: 312086

This may be a personal preference of mine, but wouldn't create a list just to avoid this warning. I'd just slap @SuppressWarnings("unchecked") on the method (or possibly even the class, if you have a lot of these), and move on.

Upvotes: 2

Related Questions