tags:

views:

124

answers:

1

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);

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

+3  A: 

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

serializedSublist.addAll(getQuestions());

or, better, yet

ArrayList serializedSublist = new ArrayList(getQuestions());
ChssPly76
To expand the excellent answer: here's what API says about `addAll()`: http://java.sun.com/javase/6/docs/api/java/util/List.html#addAll%28java.util.Collection%29 In the future keep in mind to consult the API docs first. They contains answers on this kind of questions/problems about Java behaviour.
BalusC
dope...ok that was a stupid question. thanks ..
Omnipresent