10

我正在尝试使用 Kryo 序列化一些对象(自定义类: List> )的 List 列表。

list2D; // List<List<MyClass>> which is already produced.

Kryo k1 = new Kryo();
Output output = new Output(new FileOutputStream("filename.ser"));
k1.writeObject(output, (List<List<Myclass>>) list2D);
output.close();

到目前为止没问题,它写出没有错误的列表。但是当我尝试阅读它时:

Kryo k2 = new Kryo();
Input listRead = new Input(new FileInputStream("filename.ser"));
List<List<Myclass>> my2DList = (List<List<Myclass>>) k2.readObject(listRead,  List.class);

我收到此错误:

Exception in thread "main" com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): java.util.List

我怎么解决这个问题?

4

2 回答 2

6

回读对象时不能使用List.class,因为List它是一个接口。

k2.readObject(listRead,  ArrayList.class);
于 2014-02-16T10:32:27.113 回答
2

根据您的错误,您可能希望向您的类添加一个无参数构造函数:

 public class MyClass {

    public MyClass() {  // no-arg constructor

    }

    //Rest of your class..

 }
于 2013-01-22T12:23:19.077 回答