Java 未检查或不安全操作消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16514148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java unchecked or unsafe operations message
提问by user2351151
I am getting the following error when compiling a Javaclass in BlueJ.
在BlueJ 中编译Java类时出现以下错误。
AuctionManager.java uses unchecked or unsafe operations.
This error is only displayed when the following deserialization code is in one of my functions:
仅当以下反序列化代码在我的函数之一中时才会显示此错误:
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(Filename));
ArrayList<AuctionItem> AuctionList = (ArrayList<AuctionItem>) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
Can I please have some information as to why this error is being displayed, and some help to use the deserialization code without the error.
我能否提供一些有关为什么会显示此错误的信息,以及一些使用反序列化代码而没有错误的帮助。
回答by zw324
First the message you get is not an error, is it a warning; it doesn't prevent you program from compiling, or running.
首先你得到的消息不是错误,是警告;它不会阻止您的程序编译或运行。
As for the source, it is this line:
至于来源,是这一行:
ArrayList<AuctionItem> AuctionList = (ArrayList<AuctionItem>) in.readObject();
ArrayList<AuctionItem> AuctionList = (ArrayList<AuctionItem>) in.readObject();
Since you are casting an object (readObject
returns an Object
) to a parameterized type (ArrayList<AuctionItem>
).
由于您将对象(readObject
返回一个Object
)转换为参数化类型(ArrayList<AuctionItem>
)。
回答by mkczyk
user2351151: You can do this for warning doesn't show:
user2351151:您可以执行此操作以防止警告未显示:
ArrayList tempList; // without parameterized type
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(Filename));
tempList = (ArrayList) in.readObject();
for (int i = 0; i < tempList.size(); i++) {
AuctionList.add(i, (AuctionItem)tempList.get(i)); // explict type conversion from Object to AuctionItem
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
You must rewrite ArrayList with explict type conversion (you can use temporary ArrayList without parameterized type).
您必须使用显式类型转换重写 ArrayList(您可以使用没有参数化类型的临时 ArrayList)。