Skip to content

Latest commit

 

History

History
82 lines (52 loc) · 914 Bytes

File metadata and controls

82 lines (52 loc) · 914 Bytes

Arrays.copyOfRange()

What It Does

Creates a new array by copying elements from a given range.

Arrays.copyOfRange(arr, start, end);
  • start = included ✅
  • end = excluded ❌

Example

int[] arr = {1, 2, 4, 3, 5, 8};

int[] subArray = Arrays.copyOfRange(arr, 1, 3);
{2, 4}   // out put

How It Works

Arrays.copyOfRange(arr, 0, 3);

Copies:

arr[0]
arr[1]
arr[2]

Stops before:

arr[3]

Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Common Uses

  • Creating sub-arrays
  • Splitting arrays into parts
  • Range-based processing

Key Learning

Think:

for (int i = start; i < end; i++)

The end index is never included.


Related Problems