0

编译Java类时出现以下错误:BlueJJava未经检查或不安全操作消息

try { 
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(Filename)); 
    ArrayList<AuctionItem> AuctionList = (ArrayList<AuctionItem>) in.readObject(); 
    in.close(); 
} 
catch (Exception e) { 
    e.printStackTrace(); 
} 

我可以请有一些信息,为什么被显示此错误,并且一些帮助:

AuctionManager.java uses unchecked or unsafe operations. 

当下面的反序列化的代码是在我的一个功能时,才会显示此错误使用没有错误的反序列化代码。

回答

1

首先你得到的信息不是错误,是不是一个警告;它不会阻止您编译或运行程序。

至于源,它是这一行:

ArrayList<AuctionItem> AuctionList = (ArrayList<AuctionItem>) in.readObject();

由于正在铸造的对象(readObject返回Object)到参数化的类型(ArrayList<AuctionItem>)。

+0

什么是修复代码的最佳方法,以便警告不显示? – user2351151 2013-05-13 04:55:59

1

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(); 
} 

您必须重写用显式类型转换的ArrayList(你可以不使用参数化类型的ArrayList临时)。

相关问题