1

在BlueJ中编译Java类时出现以下错误。

AuctionManager.java uses unchecked or unsafe operations.

仅当以下反序列化代码位于我的某个函数中时,才会显示此错误:

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

我能否提供一些关于为什么显示此错误的信息,以及使用反序列化代码没有错误的一些帮助。

4

2 回答 2

1

user2351151: You can do this for warning doesn't show:

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).

于 2013-06-05T00:17:32.747 回答
1

首先你得到的消息不是错误,是警告吗?它不会阻止您的程序编译或运行。

至于来源,就是这一行:

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

由于您将对象(readObject返回一个Object)转换为参数化类型(ArrayList<AuctionItem>)。

于 2013-05-13T02:42:28.443 回答