【发布时间】:2015-03-14 04:31:11
【问题描述】:
我创建数组的行给了我一个Generic array creation 警告。
有什么好的方法来解决这个问题?
public class Foo<T> {
void someMethod() {
Point[] points = new Point[3];
}
class Point {
float x, y;
}
}
【问题讨论】:
-
static class Point {...。看起来你的内部类只是用来保存x和y,并不需要一个指向Foo<T>实例的隐藏指针。如果我是对的,那么它应该是嵌套类而不是内部类。 -
@ajb 这应该是一个答案(因为它是答案)。
-
有趣的是为什么
Point p = new Point()编译得很好,而Point[] points = new Points[3]没有。 -
@Pshemo 即使
List<Point> points = new ArrayList<Point>()编译良好,该限制仅适用于数组。
标签: java generics inner-classes