Reputation: 1129
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.
Upvotes: 2
Views: 13124
Reputation: 2322
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);
Upvotes: 13
Reputation: 23644
To get negative values working as well you could make a helper method for this:
public final class Slice {
private Slice() {}
public static <T> T[] copyOfRange(final T[] a, int start) {
return slice(a, start, a.length);
}
public static <T> T[] copyOfRange(final T[] a, int start, int end) {
if (start < 0) start = a.length + start;
if (end < 0) end = a.length + end;
return Arrays.copyOfRange(a, start, end);
}
}
If you want it to be really fast/ practical you'd also need to define this for each primitive type.
Upvotes: 2
Reputation: 8695
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}
Upvotes: 8
Reputation: 279
Java doesn't have in built method on array to do slice but there are classes which can help you with it.
Check this out : How to create a sub array from another array in Java?
Use copyOfRange method from java.util.Arrays class:
int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);
startIndex is the initial index of the range to be copied, inclusive. endIndex is the final index of the range to be copied, exclusive. (This index may lie outside the array)
Upvotes: 4
Reputation: 48287
There is no pythonic way like k[:-3] or similar
but you can use the very util Arrays class like:
Arrays.copyOfRange(k, 0, 1);
Upvotes: 2
Reputation: 1135
Doesn't a for loop work exactly like the slice operator in python.
This is a list in python:
>>>a = [1, 2, 3, 4, 5]
This is how the slice operator would work:
>>>a[1:4:2] # Specify the start index, end index and the increment
[2,4] # Output returned
A for loop in Java achieving the same functionality would work as follows:
for(int i=start_index; i<end_index; i+=increment)
System.out.print(a[i] + " ");
Upvotes: 1
Reputation: 39507
You can you System.arrayCopy()
method to do that:
For e.g.:
int[] arr = new int[100];
// copy from sarr - 11 to 20th elements to arr
System.arrayCopy(sarr,11,arr,0,10);
Upvotes: 1