如何返回 Vector java
如何在 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;
}
How do I return a vector in a java function. I want to unserialize a vector loaded from a file and return in a function but I get errors. This is what code I currently have.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将从文件中读取的对象转换为向量:
请注意,您无法检查它是否真的是国家向量(无法一一检查内容)。
You need to cast the object that you read from the file to Vector:
Note that you cannot check if it is really a Vector of Countries (short of checking the contents one by one).
这是一个疯狂的猜测,但请尝试
return (Vector) object;
This is a wild guess, but try
return (Vector<Countries>) object;