RandomAccessSubList未序列化

问题描述:

我正在尝试获取List的子列表,但是我希望该子列表被序列化.我发现,当我们从ArrayList获取子列表时,该子列表未序列化.

I am trying to get a sublist of a List but I want the sublist to be serialized. I found out that when we get sublist from an ArrayList the sublist is not serialized.

要克服这一点,这就是我正在做的事情:

To overcome this, this is what I am doing:

ArrayList serializedSublist = new ArrayList();
//getQuestions() returns RandomAccessSubList
getQuestions().addAll(serializedSublist); 
//problem is in the line below. serializedSublist is empty.
getRequest().getSession().setAttribute("questionsForUser", serializedSublist);

问题是,尽管第3行getQuestions()返回了列表,但第5行中的serializedSubList为空.

Problem is that serializedSubList is empty in line 5, eventhough in line 3 getQuestions() returns a list back.

您正在向后添加它,不是吗?不应该吧

You're adding it backwards, no? Shouldn't it be

serializedSublist.addAll(getQuestions());

或者更好

ArrayList serializedSublist = new ArrayList(getQuestions());