Amandeep Singh Amandeep Singh - 1 year ago 104
Java Question

Converting ArrayList to String and converted String back to ArrayList

I am in a situation where I need to send the arraylist in the network but I need to convert it in the form of string.

Now at the destination I want to convert this String to ArrayList to access the individual elements.

Please tell me how I can convert the converted String(from ArrayList) back to ArrayList.

Answer Source

ArrayList of what? If the elements are strings or integers - simply join them using some unique separator and split back to array on the other side.

If you want to send an ArrayList of arbitrary Serializable objects, you will have to serialize it, e.g. using Java serialization. However this will produce byte[] array rather than a String. If you are constrained to text protocol, you would then gonna have to use Base64 encoding or similar.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download