The Arrays.copyOfRange
method is a convenient way to copy a range of elements from one array to another array in Java. It has the following syntax:
Arrays.copyOfRange(original, from, to);Soruce:www.lautturi.com
Here, original
is the source array, from
is the starting index of the range (inclusive), and to
is the ending index of the range (exclusive).
For example, to copy the elements of an int
array called original
from index 2 to index 4 (inclusive) to another int
array called copy
, you can use the following code:
int[] original = {1, 2, 3, 4, 5}; int[] copy = Arrays.copyOfRange(original, 2, 5);
This code creates a new int
array called copy
and copies the elements of the original
array from index 2 to index 4 (inclusive) to the copy
array using the Arrays.copyOfRange
method. The resulting copy
array will have the following elements: [3, 4, 5]
.
Note that the Arrays.copyOfRange
method creates a new array and copies the elements from the original array to the new array, so any changes made to the copy
array will not be reflected in the original
array.