Creates a new array by copying elements from a given range.
Arrays.copyOfRange(arr, start, end);start= included ✅end= excluded ❌
int[] arr = {1, 2, 4, 3, 5, 8};
int[] subArray = Arrays.copyOfRange(arr, 1, 3);{2, 4} // out putArrays.copyOfRange(arr, 0, 3);Copies:
arr[0]
arr[1]
arr[2]Stops before:
arr[3]- Time Complexity:
O(n) - Space Complexity:
O(n)
- Creating sub-arrays
- Splitting arrays into parts
- Range-based processing
Think:
for (int i = start; i < end; i++)The end index is never included.