我创建了一个由节点和边组成的图,如下所示:
public class Edge implements Serializable {
private final Node neighbor;
private final double weight;
public Edge(Node neighbor, double weight) {
this.neighbor = neighbor;
this.weight = weight;
}
public Node getNeighbor() {
return neighbor;
}
public double getWeight() {
return weight;
}
}
public class Node implements Serializable {
private final String name;
private final ArrayList<Edge> edges;
public Node(String name) {
this.name = name;
this.edges = new ArrayList<>();
}
public String getName() {
return name;
}
public ArrayList<Edge> getEdges() {
return edges;
}
public void addEdge(Edge edge) {
this.edges.add(edge);
}
}
public class Graph implements Serializable {
private final ArrayList<Node> nodes;
public Graph() {
nodes = new ArrayList<>();
}
public Graph(ArrayList<Node> nodes) {
this.nodes = nodes;
}
public ArrayList<Node> getNodes() {
return new ArrayList<>(nodes);
}
}
我正在尝试序列化和反序列化图形,如下所示:
public Graph deSerialize() {
Graph graph = new Graph();
try {
FileInputStream inStream = new FileInputStream(aFile);
ObjectInputStream oIS = new ObjectInputStream(inStream);
graph = (Graph) oIS.readObject();
oIS.close();
inStream.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return graph;
}
private void serialize(Graph graph) {
try {
FileOutputStream outStream = new FileOutputStream(aFile);
ObjectOutputStream oOS = new ObjectOutputStream(outStream);
oOS.writeObject(graph);
oOS.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
序列化工作正常(据我所知),但在反序列化过程中,会打印以下stacktrace,我无法找出原因:[updated]:(序列化时发生)
Exception in thread "main" java.lang.StackOverflowError
at java.base/java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
at java.base/java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:339)
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1135)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1553)
at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1510)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
at java.base/java.util.ArrayList.writeObject(ArrayList.java:897)
at java.base/jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[添加]:(反序列化时发生)
Exception in thread "main" java.lang.StackOverflowError
at java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3169)
at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1847)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2169)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1679)
at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2464)
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2358)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2196)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1679)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:493)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:451)
at java.base/java.util.ArrayList.readObject(ArrayList.java:929)
at java.base/jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
我尝试了一些不同的方法,包括序列化节点本身的arraylist以及其他方法,但我无法深入了解它的本质。有人能帮我修一下吗?谢谢您!
1条答案
按热度按时间ruoxqz4g1#
“就是这样。”
也许这不是很有帮助。
当你连载
Node
,Edge
s将被递归序列化。一Edge
包含Node
这样就可以递归地序列化。即使是一个适度的图,也可以得到一个公平的递归深度,并且每一级递归都会占用相当大的堆栈空间块。链表就是这种错误的典型例子。标准技术是对整个图形使用自定义的序列形式。例如,每当你遇到
Edge
你可以给它一个序列号(在一个Map<Edge,Node>
. 序列化Node
s与edges
transient
后面是一组Edge
身份证。跟着这个Edge
对象按顺序排列。反序列化时,添加Edge
我们回到Node
s。