wood burning stoves 2.0*
The moose likes Java in General and the fly likes ArrayList to Byte Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList to Byte Array" Watch "ArrayList to Byte Array" New topic
Author

ArrayList to Byte Array

Vrishal Likhite
Greenhorn

Joined: Nov 21, 2005
Posts: 18
hello everyone
i had a difficulty in converting an arraylist into a byte array and again retrive it into an arraylist again.
please letme knw how to do so
if its short and possible for you send me a code synopsis

Thank you a lot
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
vrishal,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.


SCJP Tipline, etc.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
i had a difficulty in converting an arraylist into a byte array and again retrive it into an arraylist again.

The Collections API has methods to convert between ArrayList and an array of Object references. However, a byte[] is NOT and array of object references, it is a single object that can hold an array of byte primitive values.
You will have to write code to iterate through the ArrayList or byte[], converting values one at a time between object and primitive.
Bill
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If the objects in the list are Serializable, you can user an ObjectOutputStream plus a ByteArrayOutputStream to create the array, and an ObjectInputStream plus a ByteArrayInputStream to recreate the list (creating copies of all the contents, most likely, unless the class of the objects is written to prevent that). Is that what you're trying to do?


"I'm not back." - Bill Harding, Twister
Vrishal Likhite
Greenhorn

Joined: Nov 21, 2005
Posts: 18
yes i would like send a datagrampacket over a multicastsocket, but the datagrampackettakes byte array as parameter, so if i wanna send a arraylist then wat should i do?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, reading Corey's message would be a good place to start. We require a first and last name, not a single name. Thank you.
 
GeeCON Prague 2014
 
subject: ArrayList to Byte Array