I could think of these things,
Arrays.asList(byte[])convertsbyte[]toList<byte[]>,- looping through byte array and add each element to list
I was just wondering Is there any library function to do that?
Library Apache Commons Lang has ArrayUtils.toObject, which turns a primitive array to a typed object array:
int array[] = { 1, 2, 3 };
List<Integer> list = Arrays.asList(ArrayUtils.toObject(array));
List<Byte> byteList = Arrays.asList(ArrayUtils.toObject(bytes));As this post suggests: the guava Bytes class can help out:
byte[] bytes = ...
List<Byte> byteList = Bytes.asList(bytes);
For Byte[] instead of byte[] this would work:
Byte[] array = ....
List<Byte> list = Arrays.asList(array);
I think the simplest pure Java way, without additional libraries, is this:
private static List<Byte> convertBytesToList(byte[] bytes) {
final List<Byte> list = new ArrayList<>();
for (byte b : bytes) {
list.add(b);
}
return list;
}
But better check twice if you really need to convert from byteto Byte.
byte[] byteArray;
List<Byte> medianList=new ArrayList<>();
int median=0,count=0;
Path file=Paths.get("velocities.txt");
if(Files.exists(file)){
byteArray=Files.readAllBytes(file);
}
medianList.addAll(Arrays.asList(byteArray));
Arrays.asListnot a "library function"?