• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList to Byte Array

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic