解决 java.util.ArrayList$SubList notSerializable 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26568205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
resolve a java.util.ArrayList$SubList notSerializable Exception
提问by Othmane
I am using SubList function on an object of type List. The problem is that I am using RMI and because the java.util.ArrayList$SubList is implemented by a non-serializable class I got the Exception described above when I try to pass the resulting object to a remote function taking as an argument a List as well. I've seen that I should copy the resulting List to a new LinkedList or ArrayList and pass that.
我在 List 类型的对象上使用 SubList 函数。问题是我正在使用 RMI 并且因为 java.util.ArrayList$SubList 是由一个不可序列化的类实现的,所以当我尝试将结果对象传递给一个远程函数时,我得到了上面描述的异常,该函数将一个 List 作为参数以及。我已经看到我应该将结果列表复制到新的 LinkedList 或 ArrayList 并传递它。
Does anyone know a function that helps as to easily do that for this for example ?
有谁知道一个有助于轻松做到这一点的功能,例如?
List<String> list = originalList.subList(0, 10);
回答by aravindaM
It's because, List returned by subList() method is an instance of 'RandomAccessSubList' which is not serializable. Therefore you need to create a new ArrayList object from the list returned by the subList().
这是因为,subList() 方法返回的 List 是不可序列化的 'RandomAccessSubList' 实例。因此,您需要从 subList() 返回的列表中创建一个新的 ArrayList 对象。
ArrayList<String> list = new ArrayList<String>(originalList.subList(0, 10));
回答by Othmane
The solution was simply this code:
解决方案只是这个代码:
ArrayList<String> list = new ArrayList<String>();
list.addAll(originalList.subList(0, 10));