0

My first question is that should I serialize each individual object in the arrayList or should I serialize the whole arrayList because I know that ArrayLists are serializable.

Also, how would I send it from a server to a client. I'm thinking of using ObjectOutputStream on the server side. But I think my instructor wants each and every object to be serialized.

How would I serialize each object and then add it to the ArrayList. After adding it to the ArrayList, I'd need to send the ArrayList to client which after receiving it, will de-serialize each object and read it.

2 Answers 2

0

How would I serialize each object and then add it to the ArrayList.

You wouldn't. You would just add it to the ArrayList and serialize that.

After adding it to the ArrayList, I'd need to send the ArrayList to client which after receiving it, will de-serialize each object and read it.

No. The client will deserialize the ArrayList. Done.

3
  • You think you could maybe give me a clue on how to send a serialized ArrayList over TCP like what type of stream or should I should something else?
    – Faahmed
    Apr 24, 2013 at 1:30
  • @AnAlien Eh? You've already mentioned ObjectOutputStream yourself. That's the answer.
    – user207421
    Apr 24, 2013 at 1:40
  • Oh okay. So just add the objects to an arrayList like normal and then use the objectOutputStream to send the ArrayList, right?
    – Faahmed
    Apr 24, 2013 at 1:47
0

No matter how you achieve serializing the items, you'll also need to serialize whatever you send them in. I see no real difference between serializing a list of serializables vs. doing so individually. Then you'd still have to serialize the list itself.

If you are not required to use ObjectInputStream, just to serialize and send, consider using XML. You have a couple options - XMLEncoder/XMLDecoder (older) or JAXB (newer), for example. One advantage with XML serialization over ObjectInput/OutputStream is the data is textual and (relatively) human readable on-the-wire.

3
  • Thanks, but I think I need to stick with ObjectInputStream. I don't know if my teacher for recognize XML serialization and approve of it.
    – Faahmed
    Apr 24, 2013 at 1:31
  • "You'll also need to serialize whatever you send them in". No you won't. You only need to serialize the container.
    – user207421
    Apr 24, 2013 at 4:22
  • The OP specifically asked if he could just serialize contents. While I agree with you that all you need is to serialize the collection, the OP was contemplating item by item serialization. My point was that regardless of your efficiency - all at once or one by one, the fact remains the collection itself must be serialized to send outside the jvm. Apr 24, 2013 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.