Each time I am getting 4 byte[] data. I have a one byte [] container. I want to append this coming data at the end of the container. Are there time efficient way to do this ? I do not know how many 4 byte data will come so I do not know final size of the container
-
stackoverflow.com/questions/80476/…Andreas Dolk– Andreas Dolk2013-05-06 09:42:53 +00:00Commented May 6, 2013 at 9:42
Add a comment
|
1 Answer
Simply use ArrayList and don't care about this (ArrayList would take care of increasing array). You can always transform it to byte[] later on.
4 Comments
Theolodis
This is the way to go, if you know that you'll get a lot of data you should initialize the ArrayList with an appropriate size to gain efficiency. ArrayLists double their memory, when they reach the limit, so when initialized with only one field it will need to allocate new memory very often.
L.Butz
LinkedList might be better, because the insertion of new elements works faster than with ArrayList. But searching and accessing entries in the ArrayList is faster than with the LinkedList.
user2353516
I donot know how to transform it. So can you give
Michal Borek
The most efficient way: someArrayList.toArray(new Byte[someArrayList.size()]) the parameter of toArray is to return generic array. The size is given to reuse the array "new Byte...".