-Xlint:在 netbeans 中未选中
清洁时在 NetBeans 中构建我的项目时,出现一条警告,提示“不安全操作”,因此我使用 -Xlint:unchecked 来查看这些操作,但我无法理解我做错了什么。这些是警告,然后是我的代码,谢谢!
UploadBean.java:40: warning: [unchecked] unchecked conversion
private final List fileList = Collections.synchronizedList(new ArrayList());
required: List<T>
found: ArrayList
where T is a type-variable:
T extends Object declared in method <T>synchronizedList(List<T>)
UploadBean.java:40: warning: [unchecked] unchecked method invocation: method synchronizedList in class Collections is applied to given types
private final List fileList = Collections.synchronizedList(new ArrayList());
required: List<T>
found: ArrayList
where T is a type-variable:
T extends Object declared in method <T>synchronizedList(List<T>)
UploadBean.java:97: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
fileList.add(fd);
where E is a type-variable:
E extends Object declared in interface List
3 warnings
代码
//This is line 40
private final List fileList = Collections.synchronizedList(new ArrayList());
//This is line 88
public void doUpload(FileEntryEvent e) {
FileEntry file = (FileEntry) e.getSource();
FileEntryResults result = file.getResults();
for (FileInfo fileInfo : result.getFiles()) {
if (fileInfo.isSaved()) {
FileDescription fd =
new FileDescription(
(FileInfo) fileInfo.clone(), getIdCounter());
synchronized (fileList) {
fileList.add(fd); //This is line 97
}
}
}
}
欢呼
when cleaning & building my project in NetBeans there's a warning that says "unsafe operations" so I use -Xlint:unchecked to see those operations but I cannot understand what am I doing wrong. These are the warnings and then my code thanks !
UploadBean.java:40: warning: [unchecked] unchecked conversion
private final List fileList = Collections.synchronizedList(new ArrayList());
required: List<T>
found: ArrayList
where T is a type-variable:
T extends Object declared in method <T>synchronizedList(List<T>)
UploadBean.java:40: warning: [unchecked] unchecked method invocation: method synchronizedList in class Collections is applied to given types
private final List fileList = Collections.synchronizedList(new ArrayList());
required: List<T>
found: ArrayList
where T is a type-variable:
T extends Object declared in method <T>synchronizedList(List<T>)
UploadBean.java:97: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
fileList.add(fd);
where E is a type-variable:
E extends Object declared in interface List
3 warnings
CODE
//This is line 40
private final List fileList = Collections.synchronizedList(new ArrayList());
//This is line 88
public void doUpload(FileEntryEvent e) {
FileEntry file = (FileEntry) e.getSource();
FileEntryResults result = file.getResults();
for (FileInfo fileInfo : result.getFiles()) {
if (fileInfo.isSaved()) {
FileDescription fd =
new FileDescription(
(FileInfo) fileInfo.clone(), getIdCounter());
synchronized (fileList) {
fileList.add(fd); //This is line 97
}
}
}
}
cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要了解 Java 泛型。旧的 1.4 样式仍然可以编译,但会出现警告(有些人认为是错误)。
由于您使用的类需要通用类型参数,因此需要将它们指定给编译器,如下所示:
请注意,使用泛型时,不再需要某些类型的转换。例如,在上面的示例中,
fileList.get(0)
将返回一个FileDescription
,无需进行显式转换。generics 参数指示
fileList
中存储的任何内容都必须“至少”是 FileDescription。编译器检查不可能将非 FileDescription 项放入列表中,并且输出代码实际上不执行任何运行时检查。因此,泛型实际上不会像其他语言中的类似技术那样遭受性能影响,但是编译器执行的“类型擦除”使得某些技术(例如泛型参数的运行时类型分析)变得不可能。尝试一下,您会喜欢它们的。
如果此代码是在泛型发布之前编写的,您可能需要使用向后兼容性标志(-source 1.4 -target 1.4)来编译它。
You need to learn about Java Generics. The old 1.4 style will still compile, but it will do so with warnings (considered errors by some).
Since the classes you are using expect Generic Type parameters, they need to be specified to the compiler, like so:
Note that with generics, some types of casting is no longer necessary. For example,
fileList.get(0)
will return aFileDescription
in the above example, without the need to do an explicit cast.The generics parameter indicates that whatever is stored within the
fileList
must be "at least" a FileDescription. The compiler checks that it is impossible to place non-FileDescription items in the list, and the output code actually doesn't do any run-time checks. Thus generics actually doesn't suffer performance hits like similar techniques in other languages, however the "type erasure" preformed by the compiler makes certain techinques like Run Time Type Analysis of a Generic parameter impossible.Try them out, you'll like them.
If this code was written prior to the release of Generics, you might want to compile it with backwards compatibility flags (-source 1.4 -target 1.4).
您必须在列表中使用类型,例如:
或者使用jdk7和钻石运算符,您可以使用:
所以在您的代码中使用:
You have to use type in your list like :
Or with jdk7 and diamond operator you can use :
So In your code use :