可能重复: 从列表创建byte[]
我有单子。如何将字节列表子数组从startIndex获取到endIndex id列表?
发布于 2011-02-01 11:30:56
ArrayList<Byte> list = new ArrayList<Byte>();
ArrayList<Byte> subList = (ArrayList<Byte>) list.subList(fromIndex, toIndex); //(0,5)
Byte[] array = (Byte[]) subList.toArray();发布于 2011-02-01 11:32:44
既然最初的问题实际上是要求包含一个字节不是Byte[]的子列表,那么这里是这样的:
List<Byte> byteList = .... some pre-populated list
int start = 5;
int end = 10;
byte[] bytes = new byte[end-start]; // OP explicitly asks for byte[] (unless it's a typo)
for (int i = start; i < end; i++) {
bytes[i-start] = byteList.get(i).byteValue();
}发布于 2011-02-01 11:42:14
如果你需要byte[]
byte[] byteArray = ArrayUtils.toPrimitive(list.subList(startIndex, endIndex).toArray(new Byte[0]));ArrayUtils.toPrimitive
https://stackoverflow.com/questions/4861923
复制相似问题