I have an byte array in Java and need to get parts of it. I wanted to work with Java arrays in the same way as I work with Python list, using slices. There is something similar to this in Java? Thank you in advance.
You can use Arrays' (https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Arrays.html) built-in Arrays#copyOfRange()
Arrays.copyOfRange(oldArray, startIndex, endIndex);
With plain arrays, no there is no equivalent.
With ArrayList, you can use array_list.subList(fromIndex, toIndex) to get a slice.
The slice is backed by the original ArrayList. Changes made to the slice will be reflected in the original. Non-structural changes made in the original will be reflected in the sublist; the results of structural modifications of the original list are undefined in the sublist.
byte[] byte_array = { 10, 20, 30, 40, 50 };
List<Byte> byte_list = Arrays.asList(byte_array);
List<Byte> slice = byte_list.subList(1,3); // {20, 30}
byte x = slice.get(1) ; // Returns 30
slice.set(0, 21);
// slice is now {21, 30} and byte_array is { 10, 21, 30, 40, 50}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With