java - 令牌“sList”上的语法错误,此令牌后应为 VariableDeclaratorId ....?如何解决这个问题..?
问题描述
对象反序列化 sList无法解析为类型如何解决这个问题
public static ArrayList<Student> deSerialize(String filename) {
ArrayList<Student> sList = new ArrayList<Student>();
try {
FileInputStream fiStream = new FileInputStream(filename);
ObjectInputStream inStream = new ObjectInputStream(fiStream);
@SuppressWarnings("unchecked")
sList = (ArrayList<Student>) inStream.readObject();
fiStream.close();
inStream.close();
} catch (Exception e) {
System.out.println(e);
}
return sList;
}
解决方案
在 Java 中,您不能将注释 ( @...
) 放在随机的代码段上。
只需移动@SuppressWarnings("unchecked")
到方法声明之上:
@SuppressWarnings("unchecked")
public static ArrayList<Student> deSerialize(String filename) {
ArrayList<Student> sList = new ArrayList<Student>();
try {
FileInputStream fiStream = new FileInputStream(filename);
ObjectInputStream inStream = new ObjectInputStream(fiStream);
sList = (ArrayList<Student>) inStream.readObject();
fiStream.close();
inStream.close();
} catch (Exception e) {
System.out.println(e);
}
return sList;
}
确切地说,您可以放置的地方数量有限@SuppressWarnings
,它们都列在@Target
文档的注释中。
例如,因为它是一个局部变量声明,你可以 - 然而 - 写:
public static ArrayList<Student> deSerialize(String filename) {
@SuppressWarnings("unchecked")
ArrayList<Student> sList = new ArrayList<Student>();
}
在这种情况下,它是无用的,但合法的。
推荐阅读
- mongodb - MongoDB查询时间(%Y-%m-%d %H)小于当前时间
- ruby-on-rails - 根据其 slug 路由部分时出现未初始化的常量错误
- pandas - 使用 pandas 获取文本文件的子集
- concourse - 如何通过 API 获取 Concourse 或 fly CLI 的版本
- azure - “存储账户管理操作”和ClientThrottlingError
- flutter - Flutter 导航器阻止我的 admob 广告展示
- xamarin - 如何用两个按钮垂直填充整个屏幕
- hyperledger-fabric - 有关 org.hyperledger.composer.system.*** 作为 permisions.acl 文件中的资源的更多信息。超级账本作曲家
- javascript - 需要在 Typescript 中使用第三方 JS 对象
- python - 在顶层窗口中显示图形