Introduction
The Arrays.copyOfRange()
method in Java is a utility method that creates a new array by copying a specified range from an existing array. This method is part of the java.util
package and provides a convenient way to extract subarrays from larger arrays. The method is overloaded to support various data types and custom array types.
Key Points:
- Range Copy: The method copies a specified range from the original array into a new array.
- Overloaded Methods: Supports different data types, including
boolean
,byte
,char
,double
,float
,int
,long
,short
, and generic object arrays. - Inclusive-Exclusive: The
from
index is inclusive, and theto
index is exclusive.
Syntax
The Arrays.copyOfRange()
method has several overloaded versions. Here are some of the common signatures:
boolean[] subArray = Arrays.copyOfRange(original, from, to);
byte[] subArray = Arrays.copyOfRange(original, from, to);
char[] subArray = Arrays.copyOfRange(original, from, to);
double[] subArray = Arrays.copyOfRange(original, from, to);
float[] subArray = Arrays.copyOfRange(original, from, to);
int[] subArray = Arrays.copyOfRange(original, from, to);
long[] subArray = Arrays.copyOfRange(original, from, to);
short[] subArray = Arrays.copyOfRange(original, from, to);
T[] subArray = Arrays.copyOfRange(original, from, to);
T[] subArray = Arrays.copyOfRange(original, from, to, Class<? extends T[]> newType);
- original: The original array to copy from.
- from: The starting index of the range to copy (inclusive).
- to: The ending index of the range to copy (exclusive).
- newType: The type of the new array (for the generic method).
Example: Using Arrays.copyOfRange()
Let’s explore how to use the Arrays.copyOfRange()
method with various examples.
Example 1: Copying a Range from a Boolean Array
import java.util.Arrays;
public class CopyOfRangeExample {
public static void main(String[] args) {
// Original boolean array
boolean[] original = {true, false, true, false, true};
// Copy a range from the array
boolean[] subArray = Arrays.copyOfRange(original, 1, 4);
// Print the subarray
System.out.println("Subarray: " + Arrays.toString(subArray));
}
}
Output:
Subarray: [false, true, false]
Explanation:
- Range Copy: The method copies elements from index
1
to3
(inclusive-exclusive) from the original array.
Example 2: Copying a Range from an Integer Array
import java.util.Arrays;
public class CopyOfRangeIntegers {
public static void main(String[] args) {
// Original integer array
int[] original = {10, 20, 30, 40, 50};
// Copy a range from the array
int[] subArray = Arrays.copyOfRange(original, 2, 5);
// Print the subarray
System.out.println("Subarray: " + Arrays.toString(subArray));
}
}
Output:
Subarray: [30, 40, 50]
Explanation:
- Range Copy: The method copies elements from index
2
to4
from the original array.
Example 3: Copying a Range from a Character Array
import java.util.Arrays;
public class CopyOfRangeCharacters {
public static void main(String[] args) {
// Original char array
char[] original = {'A', 'B', 'C', 'D', 'E'};
// Copy a range from the array
char[] subArray = Arrays.copyOfRange(original, 0, 3);
// Print the subarray
System.out.println("Subarray: " + Arrays.toString(subArray));
}
}
Output:
Subarray: [A, B, C]
Explanation:
- Range Copy: The method copies elements from index
0
to2
from the original array.
Example 4: Copying a Range from an Object Array
import java.util.Arrays;
public class CopyOfRangeObjects {
public static void main(String[] args) {
// Original array of strings
String[] original = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
// Copy a range from the array
String[] subArray = Arrays.copyOfRange(original, 1, 4);
// Print the subarray
System.out.println("Subarray: " + Arrays.toString(subArray));
}
}
Output:
Subarray: [Banana, Cherry, Date]
Explanation:
- Range Copy: The method copies elements from index
1
to3
from the original array.
Example 5: Copying and Changing Array Type
import java.util.Arrays;
public class CopyOfRangeWithType {
public static void main(String[] args) {
// Original array of integers
Integer[] original = {1, 2, 3, 4, 5};
// Copy a range from the array with a different type
Number[] subArray = Arrays.copyOfRange(original, 2, 5, Number[].class);
// Print the subarray
System.out.println("Subarray with type: " + Arrays.toString(subArray));
}
}
Output:
Subarray with type: [3, 4, 5]
Explanation:
- Changing Type: The method copies the specified range from the original array and allows specifying the type of the new array.
Real-World Use Case
In real-world applications, Arrays.copyOfRange()
can be used for extracting segments of data, implementing batch processing, or splitting data into smaller chunks.
Example: Splitting Data into Batches
Consider a scenario where you need to split an array of data into smaller batches for processing.
import java.util.Arrays;
public class DataBatching {
public static void main(String[] args) {
// Original array of data
int[] data = {10, 20, 30, 40, 50, 60, 70, 80};
// Process data in batches
int batchSize = 3;
for (int i = 0; i < data.length; i += batchSize) {
int end = Math.min(i + batchSize, data.length);
int[] batch = Arrays.copyOfRange(data, i, end);
// Print the current batch
System.out.println("Batch: " + Arrays.toString(batch));
}
}
}
Output:
Batch: [10, 20, 30]
Batch: [40, 50, 60]
Batch: [70, 80]
Explanation:
- Batch Processing: The method is used to extract subarrays of a specified size, allowing for efficient batch processing of data.
Conclusion
The Arrays.copyOfRange()
method in Java provides a convenient way to copy specific ranges from arrays. With its overloaded methods and support for various data types, it is useful for extracting subarrays and managing data segments.
Summary:
- Range Copy: Extracts specified ranges from original arrays into new arrays.
- Overloaded Methods: Supports different data types and custom array types.
- Use Cases: Suitable for data extraction, batch processing, and splitting data into smaller chunks.
- Inclusive-Exclusive: Uses inclusive-exclusive indexing for range specification.
By understanding and utilizing the Arrays.copyOfRange()
method, developers can efficiently manage subarray extraction and data segmentation in their Java applications.