Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList to Byte Array

 
Vrishal Likhite
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vrishal Likhite
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bhagyasri tripuramallu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Yingst wrote: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?



can you please give one small example by using ObjectOutputStream plus ByteArrayOutputStream to convert arraylist to byte array
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic