9

What is the most efficient way to do this?

3
  • 1
    -1 Because you are asking for the most efficient instead of the simplest, cleanest, easiest to understand way. Why are so many people wasting so much time on microoptimization? Commented Oct 14, 2009 at 12:21
  • 3
    starblue, "most efficient" might mean "easiest to write and maintain", which probably implies simplest and cleanest. The word efficient doesn't have to refer to performance. Commented Oct 14, 2009 at 12:37
  • 1
    +1 Because sometimes you have done your homework (profiling) and really need to do this optimization. Question is generally relevant, even not necessarily in OPs case. Commented Oct 14, 2009 at 12:37

3 Answers 3

16
byte[] byteArray = new byte[byteList.size()];
for (int index = 0; index < byteList.size(); index++) {
    byteArray[index] = byteList.get(index);
}

You may not like it but that’s about the only way to create a Genuine™ Array® of byte.

As pointed out in the comments, there are other ways. However, none of those ways gets around a) creating an array and b) assigning each element. This one uses an iterator.

byte[] byteArray = new byte[byteList.size()];
int index = 0;
for (byte b : byteList) {
    byteArray[index++] = b;
}
Sign up to request clarification or add additional context in comments.

2 Comments

there is not ONLY one way to program something! what about using an Iteraotr or an ListIterator?
You’re absolutely right. Added an Iterator-based example. :)
4

The toArray() method sounds like a good choice.

Update: Although, as folks have kindly pointed out, this works with "boxed" values. So a plain for-loop looks like a very good choice, too.

4 Comments

well its one way, but will produce a Byte[] which requires individual element unboxing.
All byte values are stored as immutable object (see today.java.net/pub/a/today/2005/03/24/autoboxing.html). So the unboxing is (probably) negligible in terms of performance. It would be interesting to measure though. Well, not interesting.
and you'll need unboxing anyway if you want (primitive) bytes
@Brian: and you also need to consider the space overhead. A large Byte[] will occupy at least 4 times the space of a byte[] for the same information content. Maybe more, depending on how the original Byte values were created.
2

Using Bytes.toArray(Collection<Byte>) (from Google's Guava library.)

Example:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.common.primitives.Bytes;

class Test {
    public static void main(String[] args) {
        List<Byte> byteList = new ArrayList<Byte>();
        byteList.add((byte) 1);
        byteList.add((byte) 2);
        byteList.add((byte) 3);
        byte[] byteArray = Bytes.toArray(byteList);
        System.out.println(Arrays.toString(byteArray));
    }
}

Or similarly, using PCJ:

import bak.pcj.Adapter;

// ...

byte[] byteArray = Adapter.asBytes(byteList).toArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.