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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

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.

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
  • 1
    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. – Theolodis May 06 '13 at 09:32
  • 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. – L.Butz May 06 '13 at 09:32
  • I donot know how to transform it. So can you give – user2353516 May 06 '13 at 09:34
  • 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...". – Michal Borek May 06 '13 at 09:36