which collection to use?

J

jimgardener

hi
i am reading a .txt file and depending up on the characters in the
text file i need to build an array of byte values.At the beginning ,i
wouldn't know the size needed to declare the array.So i can't use
byte[] myarray=new byte[somesize] .Also i need to get the order of
elements in the array unchanged.I mean ,while building the array if i
put

myarray[i++]=(byte)0xff;
myarray[i++]=0x7e;
myarray=(byte)0x81;

i need the final array as containing 0xff,0x7e,0x81 and not in any
random order.
Since i can't use a byte[] ,i would like to know what i should use to
hold these byte values
thanks,
jim
 
T

Tom Anderson

i am reading a .txt file and depending up on the characters in the
text file i need to build an array of byte values.At the beginning ,i
wouldn't know the size needed to declare the array.So i can't use
byte[] myarray=new byte[somesize] .

Essentially, you want an automatically resizing list or buffer for bytes.
You have basically two choices.

The first is to use some kind of List, say an ArrayList, declared to hold
Byte objects, and rely on automatic boxing and unboxing:

List<Byte> buf = new ArrayList<byte>() ;
buf.add((byte)0xff) ;
buf.add((byte)0x7e) ;
buf.add((byte)0x81) ;
byte b = buf.get(1) ; // b is now 0x7e

This will work, and is easy, but it is grotesquely memory-inefficient - to
store one byte, you're probably using at least 16 bytes in the list and
wrapper.

There are implementations of the List interface which are specialised for
bytes, and so will be more memory-efficient. There are none in the
standard library, though.

Another option is to use a ByteArrayOutputStream. Here, you write bytes
into it, then when you're finished, you can convert it to a byte array.
It's not a general-purpose list, but it sounds like it would fit your
situation:

ByteArrayOutputStream out = new ByteArrayOutputStream() ;
out.write(0xff) ;
out.write(0x7e) ;
out.write(0x81) ;
byte[] buf = out.toByteArray() ;
byte b = buf[1] ; // b is now 0x7e
Also i need to get the order of elements in the array unchanged.I mean
,while building the array if i put

myarray[i++]=(byte)0xff;
myarray[i++]=0x7e;
myarray=(byte)0x81;

i need the final array as containing 0xff,0x7e,0x81 and not in any
random order.


Uh, yeah. You pretty much implied that as soon as you said 'array'!

tom
 
D

Daniel Pitts

jimgardener said:
hi
i am reading a .txt file and depending up on the characters in the
text file i need to build an array of byte values.At the beginning ,i
wouldn't know the size needed to declare the array.So i can't use
byte[] myarray=new byte[somesize] .Also i need to get the order of
elements in the array unchanged.I mean ,while building the array if i
put

myarray[i++]=(byte)0xff;
myarray[i++]=0x7e;
myarray=(byte)0x81;

i need the final array as containing 0xff,0x7e,0x81 and not in any
random order.
Since i can't use a byte[] ,i would like to know what i should use to
hold these byte values
thanks,
jim

How about

ByteArrayOutputStream out = new ByteArrayOutputStream();


out.write(new byte[] {2,3, 120, -31, 41 });
out.write(new byte[] {2,3, 120, -31, 41 });
out.write(new byte[] {2,3, 120, -31, 41 });
out.write(new byte[] {2,3, 120, -31, 41 });

byte[] output = out.toByteArray();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top