In Java, you can use the Arrays.copyOfRange
method to create a new array that contains a subset of the elements from an existing array.
Here's an example of how to use the Arrays.copyOfRange
method to create a new array that contains a slice of an existing array:
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int[] slice = Arrays.copyOfRange(numbers, 3, 7);
In this example, we're creating a new array named slice
that contains a slice of the numbers
array. The copyOfRange
method takes three arguments: the source array, the start index (inclusive), and the end index (exclusive). The start index specifies the index of the first element that should be included in the slice, and the end index specifies the index of the element that should be excluded from the slice.
The slice
array will contain the elements from the numbers
array with indices 3, 4, 5, and 6 (elements 4, 5, 6, and 7 in the numbers
array).
You can find more information about the Arrays.copyOfRange
method and how to use it in Java in the Java documentation.