![]() |
VOOZH | about |
Arrays copyOfRange() method is used to create a new array by copying a specified range of elements from an existing array. It provides a way to copy a subset of elements between two indices by creating a new array of the same type.
Below is a simple example that uses Arrays.copyOfRange() method to copy a specified range of elements from an existing array.
Copied Array: [20, 30]
Table of Content
public static int[] copyOfRange(int[] original_array, int from_index, int to_index)
Parameters:
Return Type:
Exceptions:
Now, let's understand the working of Arrays.copyOfRange() method with two examples.
Arrays.copyOfRange() with Integer ArrayThis method efficiently copies a specified range of elements from an array. This automatically handles out-of-range indices by filling missing elements with default values i.e. 0 for integers.
3 4 5 6 5 6 7 0 0 0
Arrays.copyOfRange() with Custom ObjectsThis method enables easy way to copy a subset of custom objects by reducing the need of manual iteration. This also supports type safety by specifying the target class type for object arrays.
40 geek4 50 geek5 20 geek2 30 geek3
copyOfRange(): This method copies a specific range of elements from the original array, defined by a start index (inclusive) and an end index (exclusive).