我正在尝试PriorityQueue
在 scala (2.10) 中序列化一个可变对象,并且NotSerializableException
在将对象写入 ObjectOutputStream 时得到一个。我做了一个简单的测试用例:
import java.io.{ByteArrayOutputStream, ObjectOutputStream}
import scala.collection.mutable
object Test extends App {
val pq = new mutable.PriorityQueue[Int]()
val oos = new ObjectOutputStream(new ByteArrayOutputStream())
oos.writeObject(pq)
}
例外是
Exception in thread "main" java.io.NotSerializableException:scala.collection.mutable.PriorityQueue$ResizableArrayAccess
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at Test$.main(Test.scala:7)
似乎 PriorityQueue 应该是可序列化的,我能做些什么来解决这个问题吗?