我正在尝试序列化加权图,我遇到了一些问题。
我有一个UndirectedGraph<T>
的实例,扩展了Graph<T>
。 Graph<T>
中仅有的两个字段是:
private Map<T,Vertex<T>> vertices;
private int edgeCount;
所以我在课程UndirectedGraph<T>
中有以下序列化:
private void readObject(ObjectInputStream inputStream) throws ClassNotFoundException, IOException{
inputStream.defaultReadObject();
Map<T,Vertex<T>> vertexList =(Map<T,Vertex<T>>)inputStream.readObject();
int edgeCount=(Integer)inputStream.readObject();
super.init(vertexList,edgeCount);
}
private void writeObject(ObjectOutputStream outputStream) throws IOException{
outputStream.defaultWriteObject();
outputStream.writeObject(super.getVertexList());
outputStream.writeObject(super.getNumberOfEdges());
}
在Graph<T>
中,相应的方法getVertexList
,getNumberOfEdges
和init
如下:
public int getNumberOfEdges(){
return edgeCount;
}
protected Map<T,Vertex<T>> getVertexList(){
return vertices;
}
protected void init(Map<T,Vertex<T>> newVertexList, int newEdgeCount){
if (vertices==null){
vertices=newVertexList;
edgeCount=newEdgeCount;
}
else{
throw new IllegalStateException("The graph is already initialized");
}
}
当我在main方法中运行它时出现异常:
try(
OutputStream file=new FileOutputStream("globalgraph.ser");
OutputStream buffer=new BufferedOutputStream(file);
ObjectOutput output=new ObjectOutputStream(buffer);
)
{
output.writeObject(globalGraph);
}
catch(IOException e){
System.err.println("IOException: " + e.getMessage());
}
我得到的例外是&#34; IOException:Vertex $ Edge&#34;,我发现它来自行outputStream.writeObject(super.getVertexList());
。除此之外,我不知道它来自哪里。 Edge
是Vertex<T>
和Vertex<T>
次要Serializable
的内部类,只是使用方法
private void readObject(ObjectInputStream inputStream) throws ClassNotFoundException, IOException{
inputStream.defaultReadObject();
}
private void writeObject(ObjectOutputStream outputStream) throws IOException{
outputStream.defaultWriteObject();
}
序列化。我已经看了一段时间,并且不知道是什么原因导致了这个例外。任何帮助表示赞赏。
编辑:添加顶点和边缘类
public class Vertex<T> implements Serializable{
private T label; //The data within the node
private ArrayList<Edge> edgeList;//List of edges leaving node
private boolean visited; //Denoting whether traversal algorithms have accessed
private Vertex<T> previousVertex; //Previous vertex for traversal algorithms
private double cost; //cost to get to this vertex from some origin vertex
private boolean userVisited;
public Vertex(T vertexLabel){
label=vertexLabel;
edgeList=new ArrayList<>();
visited=false;
previousVertex=null;
cost=0;
userVisited=false;
}
//Vertex methods
protected class Edge{
//the vertex the edge points to
private Vertex<T> endVertex;
//the weight of the edge from the vertex to endVertex
private double weight;
private int numberOfEdits;
protected Edge(Vertex<T> vertex, double edgeWeight){
endVertex=vertex;
weight=edgeWeight;
numberOfEdits=1;
}
//Edge methods
}
//Iterator inner classes, don't need to serialize these
//***********Serialization Methods************
private static final long serialVersionUID = 9993882109871L;
private void readObject(ObjectInputStream inputStream) throws ClassNotFoundException, IOException{
inputStream.defaultReadObject();
}
private void writeObject(ObjectOutputStream outputStream) throws IOException{
outputStream.defaultWriteObject();
}
}