如何返回向量java

如何返回向量java,java,function,vector,Java,Function,Vector,如何在java函数中返回向量。我想取消序列化从文件加载的向量并在函数中返回,但我得到了错误。这是我目前拥有的代码 private static Vector<Countries> loadOB(String sFname) throws ClassNotFoundException, IOException { ObjectInputStream oStream = new ObjectInputStream(new FileInputStream(sFnam

如何在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;
    }
private static Vector loadOB(字符串sFname)抛出ClassNotFoundException、IOException{
ObjectInputStream oStream=新ObjectInputStream(新文件输入流(sFname));
Object Object=oStream.readObject();
oStream.close();
返回对象;
}

这是一个猜测,但请尝试
返回(向量)对象

这是一个猜测,但请尝试返回(向量)对象

您需要将从文件读取的对象强制转换为向量:

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();
        }
     }
private static Vector loadOB(字符串sFname)抛出ClassNotFoundException、IOException{
ObjectInputStream oStream=新ObjectInputStream(新文件输入流(sFname));
试一试{
Object Object=oStream.readObject();
if(向量的对象实例)
返回(向量)对象;
抛出新的IllegalArgumentException(“不是“+sFname”中的向量);
}最后{
oStream.close();
}
}

请注意,您无法检查它是否真的是国家向量(除了逐个检查内容之外)。

您需要将从文件读取的对象强制转换为向量:

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();
        }
     }
private static Vector loadOB(字符串sFname)抛出ClassNotFoundException、IOException{
ObjectInputStream oStream=新ObjectInputStream(新文件输入流(sFname));
试一试{
Object Object=oStream.readObject();
if(向量的对象实例)
返回(向量)对象;
抛出新的IllegalArgumentException(“不是“+sFname”中的向量);
}最后{
oStream.close();
}
}

请注意,您无法检查它是否真的是一个国家的矢量(除了逐个检查内容之外)。

它适用于下的解决方案。它适用于下的解决方案。