0

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

1

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
I donot know how to transform it. So can you give
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...".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.