Hul*_*ner 0 java function vector
如何在java函数中返回向量.我想反序列化从文件加载的向量并返回函数但我得到错误.这就是我目前拥有的代码.
private static Vector<Countries> loadOB(String sFname) throws ClassNotFoundException, IOException {
ObjectInputStream oStream = new ObjectInputStream(new FileInputStream(sFname));
Object object = oStream.readObject();
oStream.close();
return object;
}
Run Code Online (Sandbox Code Playgroud)
您需要将从文件中读取的对象强制转换为Vector:
private static Vector<Countries> loadOB(String sFname) throws ClassNotFoundException, IOException {
ObjectInputStream oStream = new ObjectInputStream(new FileInputStream(sFname));
try{
Object object = oStream.readObject();
if (object instanceof Vector)
return (Vector<Countries>) object;
throw new IllegalArgumentException("not a Vector in "+sFname);
}finally{
oStream.close();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,您无法检查它是否真的是一个国家的向量(没有逐个检查内容).
| 归档时间: |
|
| 查看次数: |
11988 次 |
| 最近记录: |