1

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.

Grumblesaurus
  • 3,021
  • 3
  • 31
  • 61
  • i guess it all depends on how you initialized the array? Do you happen to know the length of the array in advance ? also, why did you go for Arrays.asList if you want a list all along, why not use a list directly? – Jayanth Nov 12 '18 at 08:37
  • I 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. – Grumblesaurus Nov 12 '18 at 08:42

2 Answers2

2

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    I did that previously, and then removed the annotation for some reason, and found a bunch of other warnings I wish I hadn't suppressed. – Grumblesaurus Nov 12 '18 at 08:29
1

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.

Jayanth
  • 746
  • 6
  • 17