Is it possible to solve the “A generic array of T is created for a varargs parameter” compiler warning?

前端 未结 8 1638
Happy的楠姐
Happy的楠姐 2020-11-28 04:31

This is a simplified version of the code in question, one generic class uses another class with generic type parameters and needs to pass one of the generic types to a metho

8条回答
  •  抹茶落季
    2020-11-28 05:07

    It is a very easy problem to solve: Use List!

    Arrays of reference type should be avoided.

    In the current version of Java (1.7), you can mark method with @SafeVargs which will remove the warning from the caller. Careful with that though, and you're still better off without legacy arrays.

    See also the Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods tech note.

提交回复
热议问题