0

I have a byte array I want to set to a set X amount of bytes, but I want to initialize it first then assign bytes to it later, index by index.

What should I do? So far I've been just creating a byte array of X bytes, all just filled with FF to make things simple but I feel like this is not the best way.

Thanks

edit: Would the best way just be

byte[] bArr = new byte[X];

??

qol
  • 21
  • 3
  • 1
    Does this answer your question? [Fastest way to set all values of an array?](https://stackoverflow.com/questions/9128737/fastest-way-to-set-all-values-of-an-array) – Leo Leontev Sep 28 '20 at 20:41
  • Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – alexandrum Sep 28 '20 at 20:42
  • @Vikrant: There's no indication that the OP *wants* to increase in size. They're asking for a *fixed* size. – Jon Skeet Sep 28 '20 at 20:42
  • 1
    What you've got creates a byte array initialized with all values as 0, in the most idiomatic way. Unless you *want* the values all to be 0xff, I would just leave them as 0... – Jon Skeet Sep 28 '20 at 20:42
  • You are describing an array, so an array is going to be the best fit. If you have a higher level use case, there might be a better solution. – that other guy Sep 28 '20 at 20:44

0 Answers0